Jump to content

Sorting a 3d Array


QuimiQ
 Share

Recommended Posts

Hi guys

I've been using Autoit for small projects for a while now. For those performance usually wasn't ever a problem. For my current project however I work with 3d Arrays for the first time and really need a way to sort those efficiently.

Backgroundinformation:

This is my Array: $Species[$AnimalCount][$Offspring][$Skillset]

$Species[x][0][x] = Parents of $Offspring > 0

$Species[x][x][0] = Chance of Survival for specific Animal (calculatet using $Skillset > 0)

The size is is chosen by the user (via GUI)

What I need to do:

I basically want to breed my species and of course I only want to use the strongest/best animals for this (those with the highest chance of survival)

My approach;

The only way I can think of solving this is by using bubble sort but I'm sure there has to be a better way.

I allready checked the AutoIt Help file, and thought of using "_ArrayMax" but this, aswell as most of the _Array functions only work for 1d and 2d Arrays

Maybe you guys can help me find a solution for my problem or point me in the right direction.

Thanks in advance

Edited by QuimiQ
Link to comment
Share on other sites

I mean what is the size of your 3D array?

Your array has a height (y), width (x) and a deep (z).

For example if you create an array like this: 

Global $Species[10][20][50]

The height is 10, width is 20 and the deep is 50.

Why I want to know the size / dimension of the array is that maybe it can reduced to a 2d array.

 

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

it should totally be a 2D

#include <Array.au3>

Dim $aSpecies[1][3]

$aSpecies[0][0] = "Animal Count"
$aSpecies[0][1] = "Offspring"
$aSpecies[0][2] = "Skillset"

_ArrayDisplay($aSpecies)

_AnimalAdd("2 Deer" , "6" , "3")

_ArrayDisplay($aSpecies)

_AnimalAdd("3 Porcupines" , "8" , "5")

_ArrayDisplay($aSpecies)

_AnimalAdd("7 Rabbits" , "15" , "1")

_ArrayDisplay($aSpecies)

_ArraySort($aSpecies, 0, 1, 0, 2)

_ArrayDisplay($aSpecies)

Func _AnimalAdd($count , $offspring , $skillset)
_ArrayAdd($aSpecies , $count & "|" & $offspring & "|" & $skillset, 0)
EndFunc
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hey QuimiQ, 

Forgive me if i'm misreading your post, but I don't think your needs warrant the use of a 3D array, You would only need a Two dimensional one.

 

Something like this:

#include <Array.au3> ;Include the array functions (We're using _ArrayDisplay() in this example from Array.au3)

Global $Species[1][4] ;[index][Animal Count,Offspring,skillset] 

for $x = 1 to 10 ;Use a for loop to loop through data
ReDim $Species[$x + 1][4] ;Increase the size of the array
$Species[$x][0] = "Animal" & $x;Assign a Species Name 
$Species[$x][1] = "AnimalCount "  & $x;assign the AnimalCount
$Species[$x][2] = "OffspringCount " & $x;assign the Offspring
$Species[$x][3] = "SkillSetCount " & $x;assign the Skillset
Next

_ArrayDisplay($Species) ;display the array

Run that, And you should see how arrays are formatted.... A two dimensional array works much like a table in excel (Row's and columns)

Generally the first dimension is used for the index number, and the second dimension contains the information.... the easiest way to see how this works is to use _ArrayDisplay() to familiarise yourself with how the array is built.

Hope this helps

- Javi

EDIT : Forgot to mention, If you're still having difficulty afterwords, It may be worth reading through AutoIt's documentation on Arrays (Especially if you've never worked with them before!  ;) )

Edited by javiwhite

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Link to comment
Share on other sites

Would you say your data looks like this...

Species Count       Children        Skills

monkey
        1
                Joe
                            pick up sticks
                            peel bannanas
                Edna
                            sign language
                            fold socks
        2       
                Alan
                            basic math
                            sling shit
                Jeff
                            clean cage
                            solve maze
                            use rude implements
                Shirley         
                            solve maze
                George
                            none

 

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

first of all thank you all very much for your fast replys

about the 2 d array I don't think that's possible, I probbably didn't give enough background information:

We have one species (let's take dogs for example, german shepherds to be specific)

the user now chooses to use the following settings:

$AnimalCount = 10

$Offspring = 5 (should actually be named $MaxOffspring, but doesn't matter to much)

$Skillset = 3

We now have 10 different dogs, when they breed they can produce a max ammount of 5 puppys each time, and thei all have 3 Skills (lets take barking, biting and running) how good they are at each skill expressed in a number between 0 and 999

now lets just take a look at Dog 0 and Dog 1, they mate and produce 2 puppys

We now have the Dog Parent (which is the parent with the lower number)

Chance of survival = $Species[0][0][0] = 5 

Barking Skill = $Species[0][0][1] = 561

Biting Skill = $Species[0][0][2] = 657

Running Skill = $Species[0][0][3] = 357

Then we have puppy 1, the values for each of his skill are calculated by taking the average of his parents value +/- a random number (mutation)

Chance of survival = $Species[0][1][0] = 6 

Barking Skill = $Species[0][1][1] = 564

Biting Skill = $Species[0][1][2] = 444

Running Skill = $Species[0][1][3] = 333

 

The same thing goes for puppy 2

Chance of survival = $Species[0][2][0] = 2 

Barking Skill = $Species[0][2][1] = 657

Biting Skill = $Species[0][2][2] = 345

Running Skill = $Species[0][2][3] = 222

Now this was just the offspring of Dog 0 and Dog 1

Dog 2 and 3 will mate as well and produce up to 5 offspring

Same for Dog 4 and Dog 5 so on and so forth. Each individual Animal (whether parent or offspring) has it's own skill level for each skill. I think I need a 3 dimensional array for this.

 

Some additional information:

It's a bit complicated to explain (especially in english) which is why I dind't write this in the first place, but i'll try my best 'cause this may help you understand what I'm trying to do.

The goal of the project is to simulate the theory of evolution.

A species is generated with the same value in each skillset, so we have the exact same animal multiple times and they start to mate. The offspring can have small mutations for each skill-value (just like you are a bit different from your parents but still alike). then the envirement (which i allready programmed) changes so maybe now all animals which are let's say mor resistant to heat have a greater chance to survive. (so if the parents have a heatresistant child this would have a greater chance to survive and his offsprings original value for heat resistance of which it would then mutate slightly from would allready be higher )

So if each time I let only the strongest animals breed (survival of the fittest) I should get a completely different species after multiple years/breeding periods.

This explains also why it has to be performance friendly, if you want to calculate thousends of years for let's say 100 animals with max 10 Offspring and 50 skills each it could take quite some time.

Also to answer kylomas question: I did program the envirement and the change of the envirement and the function to create my species but nothing beyond that.

Edited by QuimiQ
Link to comment
Share on other sites

I still dont see the need for a 3rd dimension.  A small array within your array would suffice, here i am storing the skills array in column3, and calculating the average of the three skills and displaying it in column2 (then sorting off this column and breeding accordingly), the offspring in column1, and the animal number in column0.

#include <Array.au3>
Dim $aSkills[3]
Dim $aSpecies[1][4]

$aSpecies[0][0] = "Animal Count"
$aSpecies[0][1] = "Offspring"
$aSpecies[0][2] = "Skillset"


_AnimalAdd("Dog 1" , "6" , "561" , "657" , "357")

_ArrayDisplay($aSpecies , "DOG 1")
;~ _ArrayDisplay($aSkills , "DOG 1")

_AnimalAdd("Dog 2" , "15" , "657" , "345" , "222")


_ArrayDisplay($aSpecies , "DOG 2")
;~ _ArrayDisplay($aSkills  , "DOG 2")

_AnimalAdd("Dog 3" , "8" , "564" , "444" , "333")

_ArrayDisplay($aSpecies , "DOG 3")
;~ _ArrayDisplay($aSkills , "DOG 3")

_ArraySort($aSpecies, 1, 1, 0, 2) ; now sort and breed the top 2

_ArrayDisplay($aSpecies , "SORTED by SkillSet")

_Breed()

_ArrayDisplay($aSpecies)

Func _AnimalAdd($count , $offspring , $Bark , $Bite , $Run , $skillset = 0 )
    $skillset = ($Bark + $Bite + $Run) / 3
    $aSkills[0] = $Bark
    $aSkills[1] = $Bite
    $aSkills[2] = $Run
_ArrayAdd($aSpecies , $count & "|" & $offspring & "|" & $skillset & "|" & $aSkills, 0)
EndFunc

Func _Breed()
    _ArrayAdd($aSpecies , $aSpecies[1][0] & "+" & $aSpecies[2][0] & "|" & ($aSpecies[1][1] + $aSpecies[2][1]) / 2 & "|" & ($aSpecies[1][2] + $aSpecies[2][2]) / 2, 0)
EndFunc
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

 

I still dont see the need for a 3rd dimension.  A small array within your array would suffice, here i am storing the skills array in column3, and calculating the average of the three skills and displaying it in column2 (then sorting off this column and breeding accordingly), the offspring in column1, and the animal number in column0.

#include <Array.au3>
Dim $aSkills[3]
Dim $aSpecies[1][4]

$aSpecies[0][0] = "Animal Count"
$aSpecies[0][1] = "Offspring"
$aSpecies[0][2] = "Skillset"


_AnimalAdd("Dog 1" , "6" , "561" , "657" , "357")

_ArrayDisplay($aSpecies , "DOG 1")
;~ _ArrayDisplay($aSkills , "DOG 1")

_AnimalAdd("Dog 2" , "15" , "657" , "345" , "222")


_ArrayDisplay($aSpecies , "DOG 2")
;~ _ArrayDisplay($aSkills  , "DOG 2")

_AnimalAdd("Dog 3" , "8" , "564" , "444" , "333")

_ArrayDisplay($aSpecies , "DOG 3")
;~ _ArrayDisplay($aSkills , "DOG 3")

_ArraySort($aSpecies, 1, 1, 0, 2) ; now sort and breed the top 2

_ArrayDisplay($aSpecies , "SORTED by SkillSet")

_Breed()

_ArrayDisplay($aSpecies)

Func _AnimalAdd($count , $offspring , $Bark , $Bite , $Run , $skillset = 0 )
    $skillset = ($Bark + $Bite + $Run) / 3
    $aSkills[0] = $Bark
    $aSkills[1] = $Bite
    $aSkills[2] = $Run
_ArrayAdd($aSpecies , $count & "|" & $offspring & "|" & $skillset & "|" & $aSkills, 0)
EndFunc

Func _Breed()
    _ArrayAdd($aSpecies , $aSpecies[1][0] & "+" & $aSpecies[2][0] & "|" & ($aSpecies[1][1] + $aSpecies[2][1]) / 2 & "|" & ($aSpecies[1][2] + $aSpecies[2][2]) / 2, 0)
EndFunc

ok i understand what you mean... replacing the 3rd dimension with an identifier whether it is offspring or not this could work, i'll try that

thank you

Link to comment
Share on other sites

Hi QuimiQ
interesting problem.
One question please
You say you want to find  "(those with the highest chance of survival)",
Are those with the highest average value of the values of all the skills of the animal?
If so, you would like to sort the array based on the average of the values of the third dimension?
is it right?

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...