Padol Posted January 21, 2016 Posted January 21, 2016 Hi dude, I'm new to the forum but I'm an autoit achivment since i was 11, i love it's Windows Api automation, also i know c and a little of java (English too alittle, sorry). tonight i will want to know how to genetic programming, then i think, can autoit help me? i wrote a little script that simulate a tiny game. Beta version of autoit 3.15 "with maps101!" there was N character on a 50 x 50 grid and 1 point of the grid contain a food! every time an individue( or character) try to go over the grid (then more or less than 50 / 0) they receive a penality in the fitness (or fittness i don't know D; ) else when go on top of the food increase the fitness of 1, if a character go under a fitnessences of -8 the algorithm replace the individue with a new set of instruction (the instruction for the input output) input output theory IMHO the genetic algorith is nothing else than a bruteforce, a bruteforce of instruction, if i'm wrong, please tell me the correct way to do that... grid: 1,1|2,1|3,1|4,1 1,2 |2,2|3,2|4,2 1,3|2,3|3,3|4,3 1,4|2,4|3,4|4,4 the food are at random position food at 2,1 character at 3,3 a little mind map here: A: food.x < pos.x : pos.x + 1 ret 1 food.x < pos.x : pos.x - 1 ret 2 B: food.x > pos.x : pos.x + 1 ret 1 food.x > pos.x : pos.x - 1 ret 2 C: food.y > pos.y : pos.y + 1 ret 1 food.y > pos.y : pos.y - 1 ret 2 D: food.y < pos.y : pos.y + 1 ret 1 food.y < pos.y : pos.y - 1 ret 2 DNA structure like: |A2|B1|C2|D2 Explanation: look at the map if A2 the individue subtract is position by 1 thinking this is the right way to gointo the food every 1000 cicles the program take 3 random individues the individue with the lowest feed valure goes replaced by the soon of the other in other words: dna MOM |---A----|---A----|---A----|----A----| dna DAD |---B----|---B----|---B----|----B----| dna SOON |----A----|----B----|----A----|----B----| remember that DNA should like this : A2B1C2D2 this structure is elaborated by the algoritm to product the movement a function representate it with the widow and the flashy mini button *-* the programs is realy hugly, now i write some comments, lets me write expandcollapse popup#include <Array.au3> GUICreate("prova", 500, 500) Global $grid[50][50] Global $LifeColor = 0x000000 For $i = 0 To 49 For $x = 0 To 49 $grid[$i][$x] = GUICtrlCreateButton("", $i * 10, $x * 10, 10, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) Next Next GUISetState(@SW_SHOW) Global $thread1[] Global $thread2[] $thread1["x"] = 0 $thread1["y"] = 0 $thread2["x"] = 0 $thread2["y"] = 0 ;~ AdlibRegister("noscriptblock",1) ;~ sleep(500) ;~ AdlibRegister("noscriptblock2",1) ;~ sleep (500) ;~ AdlibRegister("noscriptblock2",5) ;~ $pipe=-1 ;~ do ;~ if ;~ until $pipe=1 Global $food[] ;~ _makeFood() Func noscriptblock() Local $x, $y $x = $thread1.x $y = $thread1.y GUICtrlSetBkColor($grid[$x][$y], 0x000000) $thread1.x += 1 If $thread1.x > 49 Then $thread1.x = 0 $thread1.y += 1 EndIf If $thread1.y > 49 Then AdlibUnRegister("noscriptblock") EndIf EndFunc ;==>noscriptblock Func noscriptblock2() Local $x, $y $x = $thread2.x $y = $thread2.y GUICtrlSetBkColor($grid[$x][$y], 0xFFFFFF) $thread2.x += 1 If $thread2.x > 49 Then $thread2.x = 0 $thread2.y += 1 EndIf If $thread2.y > 49 Then AdlibUnRegister("noscriptblock2") EndIf EndFunc ;==>noscriptblock2 Func _FirstGenerationArr($N) Local $x, $y Local $retArr[51][4] $retArr[0][0] = $N For $i = 1 To $N $x = Random(0, 49, 1) $y = Random(0, 49, 1) $retArr[$i][0] = $x $retArr[$i][1] = $y $retArr[$i][2] = "" $retArr[$i][3] = 0 Next Return $retArr EndFunc ;==>_FirstGenerationArr Func _displayLife(ByRef $life) ;This Function is use to display the life on to the grid ;~ _ArrayDisplay($life) Local $N, $x, $y $N = $life[0][0] For $i = 1 To $N $x = $life[$i][0] $y = $life[$i][1] GUICtrlSetBkColor($grid[$x][$y], $LifeColor) Next EndFunc ;==>_displayLife ;~ $food=$grid[30][30] ;~ fittest ogni volta che si mangia si aumenta di valore 1 ;~ si accoppiano gli individui che hanno ottenuto più valore ;func prepare to the first generation Func _StructFirstGen(ByRef $life) Local $N, $x, $y $N = $life[0][0] For $i = 1 To $N Next EndFunc ;==>_StructFirstGen Func _individueToLife(ByRef $individue, ByRef $life) $life[$individue.n][0] = $individue.x $life[$individue.n][1] = $individue.y $life[$individue.n][2] = $individue.code $life[$individue.n][3] = $individue.fitt Return $life EndFunc ;==>_individueToLife Func _lifeToIndividue($N, ByRef $life) Local $individue[] $individue.x = $life[$N][0] $individue.y = $life[$N][1] Local $code $code = $life[$N][2] If $code = "" Then $individue.code = individuePrep() Else $individue.code = $code EndIf $individue.fitt = $life[$N][3] $individue.n = $N Return $individue EndFunc ;==>_lifeToIndividue Func individuePrep() Local $code $code = "|" & "A" & Random(1, 2, 1) & "|" & "B" & Random(1, 2, 1) & "|" & "C" & Random(1, 2, 1) & "|" & "D" & Random(1, 2, 1) Return $code EndFunc ;==>individuePrep Func _individueInOut(ByRef $individue) Local $code $code = StringSplit($individue.code, "|", 2) For $i = 0 To UBound($code) - 1 Switch $code[$i] Case "A1" If $food.x < $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "A2" If $food.x < $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "B1" If $food.x > $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "B2" If $food.x > $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "C1" If $food.y < $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "C2" If $food.y < $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) ;~ msgbox(0,"",$individue.code) EndIf Case "D1" If $food.y > $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "D2" If $food.y > $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf EndSwitch Next Return $individue EndFunc ;==>_individueInOut Func outofmem(ByRef $individue) If $individue.x > 49 Then $individue.x = 49 $individue.fitt -= 1 EndIf If $individue.y > 49 Then $individue.y = 49 $individue.fitt -= 1 EndIf If $individue.x < 0 Then $individue.x = 0 $individue.fitt -= 1 EndIf If $individue.y < 0 Then $individue.y = 0 $individue.fitt -= 1 EndIf Return $individue EndFunc ;==>outofmem Func _lookforsomeeat(ByRef $individue) If $individue.x = $food.x Then If $individue.y = $food.y Then $individue.fitt += 1 _makeFood() EndIf EndIf Return $individue EndFunc ;==>_lookforsomeeat Func _makeFood() $food.x = Random(0, 49, 1) $food.y = Random(0, 49, 1) GUICtrlSetBkColor($grid[$food.x][$food.y], 0x00FF00) EndFunc ;==>_makeFood ;==> Func _Main() _makeFood() $firsLife = _FirstGenerationArr(10) $life = $firsLife While GUIGetMsg() <> -3 For $rt = 0 To 1000 _displayLife($life) Local $N, $persona $N = $life[0][0] For $tio = 1 To $N ;~ sleep(120) $persona = _lifeToIndividue($tio, $life) $persona = _individueInOut($persona) $persona = _lookforsomeeat($persona) ;~ _displayLife($life) If $persona.fitt < (-10) Then $persona = _replaceIndividue($persona) EndIf $life = _individueToLife($persona, $life) Next Next _procreation($life) WEnd EndFunc ;==>_Main Func _replaceIndividue(ByRef $individue, $randomEvolution = 1, $code = 0, $code1 = 0) If $code = 0 Then $code = individuePrep() GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x = Random(0, 49, 1) $individue.y = Random(0, 49, 1) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) $individue.fitt = 0 ;~ local $randomEvolution=Random(0,1,1) If $randomEvolution Then $individue.code = $code Else $individue.code = _codeChain($code, $code1) EndIf Return $individue EndFunc ;==>_replaceIndividue Func _procreation(ByRef $life) Local $fitt[3] Local $random[3] Local $f Do $random[0] = Random(1, $life[0][0], 1) $random[1] = Random(1, $life[0][0], 1) $random[2] = Random(1, $life[0][0], 1) Until $random[0] <> $random[1] And $random[1] <> $random[2] And $random[0] <> $random[2] $first = _lifeToIndividue($random[0], $life) $second = _lifeToIndividue($random[1], $life) $third = _lifeToIndividue($random[2], $life) If $first.fitt < $second.fitt And $first.fitt < $third.fitt Then $first = _replaceIndividue($first, $second.code, $third.code) EndIf If $second.fitt < $first.fitt And $second.fitt < $third.fitt Then $second = _replaceIndividue($second, $first.code, $third.code) EndIf If $third.fitt < $first.fitt And $third.fitt < $second.fitt Then $third = _replaceIndividue($third, $first.code, $second.code) EndIf $life = _individueToLife($first, $life) $life = _individueToLife($second, $life) $life = _individueToLife($third, $life) Return $life EndFunc ;==>_procreation Func _codeChain($code, $code1) Local $mom, $dad, $retgen $mom = StringSplit($code, "|", 2) $dad = StringSplit($code1, "|", 2) $retgen = "|" & $mom[0] & "|" & $dad[1] & "|" & $mom[2] & "|" & $dad[3] Return $retgen EndFunc ;==>_codeChain _Main() While GUIGetMsg() <> -3 WEnd Exit the program actually works but don't give me the really essence... of the genetic algorithm... this is a bruteforce, can really this work in every situations? I hope some masters can give me the force
Padol Posted January 21, 2016 Author Posted January 21, 2016 expandcollapse popup#include <Array.au3> GUICreate("prova", 500, 500) ;creating the gui Global $grid[50][50] ;the grid Global $LifeColor = 0x000000 For $i = 0 To 49 For $x = 0 To 49 $grid[$i][$x] = GUICtrlCreateButton("", $i * 10, $x * 10, 10, 10) ;reticle of control button :P GUICtrlSetBkColor(-1, 0xFFFFFF) Next Next GUISetState(@SW_SHOW); else no windows Global $thread1[] Global $thread2[] $thread1["x"] = 0 $thread1["y"] = 0; some try with maps101 , first time for me... $thread2["x"] = 0; and a little trick to simulate multihread operation, in a single threaded program $thread2["y"] = 0 ;~ AdlibRegister("noscriptblock",1) ;~ sleep(500) ;~ AdlibRegister("noscriptblock2",1) ;~ sleep (500) ;~ AdlibRegister("noscriptblock2",5) ;~ $pipe=-1 ;~ do ;~ if ;~ until $pipe=1 Global $food[]; without this i cant inizialize maps101 .x .y ;~ _makeFood() Func noscriptblock();try to a for loop witout script blocking a sort of semaphore Local $x, $y $x = $thread1.x $y = $thread1.y GUICtrlSetBkColor($grid[$x][$y], 0x000000) $thread1.x += 1 If $thread1.x > 49 Then $thread1.x = 0 $thread1.y += 1 EndIf If $thread1.y > 49 Then AdlibUnRegister("noscriptblock") EndIf EndFunc ;==>noscriptblock Func noscriptblock2();the second instance of the emulated thread Local $x, $y $x = $thread2.x $y = $thread2.y GUICtrlSetBkColor($grid[$x][$y], 0xFFFFFF) $thread2.x += 1 If $thread2.x > 49 Then $thread2.x = 0 $thread2.y += 1 EndIf If $thread2.y > 49 Then AdlibUnRegister("noscriptblock2") EndIf EndFunc ;==>noscriptblock2 Func _FirstGenerationArr($N);realizing the primordial life in the grid Local $x, $y ;, this script is optimized to non go out of memory, Local $retArr[51][4] ;each individue stacks are maintaned for all the execution time $retArr[0][0] = $N For $i = 1 To $N $x = Random(0, 49, 1) $y = Random(0, 49, 1) $retArr[$i][0] = $x $retArr[$i][1] = $y $retArr[$i][2] = "" $retArr[$i][3] = 0 Next Return $retArr EndFunc ;==>_FirstGenerationArr Func _displayLife(ByRef $life) ;This Function is use to display the life on to the grid ;~ _ArrayDisplay($life) Local $N, $x, $y $N = $life[0][0] For $i = 1 To $N $x = $life[$i][0] $y = $life[$i][1] GUICtrlSetBkColor($grid[$x][$y], $LifeColor) Next EndFunc ;==>_displayLife ;~ $food=$grid[30][30] ;~ fittest ogni volta che si mangia si aumenta di valore 1 ;~ every time that feeding up the fittest goes up by 1 ;~ si accoppiano gli individui che hanno ottenuto più valore ;~ more fitness mean more children ;func prepare to the first generation Func _StructFirstGen(ByRef $life);never used function useless Local $N, $x, $y $N = $life[0][0] For $i = 1 To $N Next EndFunc ;==>_StructFirstGen useless Func _individueToLife(ByRef $individue, ByRef $life);a simple structure that transform the single individual data $life[$individue.n][0] = $individue.x ;to an array of wolrds element $life[$individue.n][1] = $individue.y $life[$individue.n][2] = $individue.code $life[$individue.n][3] = $individue.fitt Return $life EndFunc ;==>_individueToLife Func _lifeToIndividue($N, ByRef $life);the inverse structure, now create a maps101 Local $individue[] ;that operate on a single individue from the data $individue.x = $life[$N][0] ;recovered from the world elements array $individue.y = $life[$N][1] Local $code $code = $life[$N][2] If $code = "" Then ;if the individue dosen't have a genetic algorithm then a random one wich creates $individue.code = individuePrep();this func make the magic happens it make the "CODE" "DNA" Else $individue.code = $code EndIf $individue.fitt = $life[$N][3] $individue.n = $N Return $individue EndFunc ;==>_lifeToIndividue Func individuePrep();DNA fabric! Local $code $code = "|" & "A" & Random(1, 2, 1) & "|" & "B" & Random(1, 2, 1) & "|" & "C" & Random(1, 2, 1) & "|" & "D" & Random(1, 2, 1) Return $code EndFunc ;==>individuePrep Func _individueInOut(ByRef $individue);the brain of the situation, the hand of god, this make the output by the input and the code of the DNA Local $code $code = StringSplit($individue.code, "|", 2);watching for code on and try randomly every possibility to risolve the problem.. (every cicle not here and not in one call) For $i = 0 To UBound($code) - 1 Switch $code[$i] Case "A1" If $food.x < $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x += 1 $individue = outofmem($individue); this watch if the individues goes out of the greed and depenalize them GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "A2" If $food.x < $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x -= 1 $individue = outofmem($individue);if the individue goes down to a penality of 10 will be replaced by other GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "B1" If $food.x > $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "B2" If $food.x > $individue.x Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "C1" If $food.y < $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "C2" If $food.y < $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) ;~ msgbox(0,"",$individue.code) EndIf Case "D1" If $food.y > $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y += 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf Case "D2" If $food.y > $individue.y Then GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.y -= 1 $individue = outofmem($individue) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) EndIf EndSwitch Next Return $individue EndFunc ;==>_individueInOut Func outofmem(ByRef $individue) If $individue.x > 49 Then $individue.x = 49 $individue.fitt -= 1 EndIf If $individue.y > 49 Then $individue.y = 49 $individue.fitt -= 1 EndIf If $individue.x < 0 Then $individue.x = 0 $individue.fitt -= 1 EndIf If $individue.y < 0 Then $individue.y = 0 $individue.fitt -= 1 EndIf Return $individue EndFunc ;==>outofmem Func _lookforsomeeat(ByRef $individue);this func look if some individue drops on a food and increase her fitt value If $individue.x = $food.x Then If $individue.y = $food.y Then $individue.fitt += 1 _makeFood();then a new random food was created EndIf EndIf Return $individue EndFunc ;==>_lookforsomeeat Func _makeFood();make a food $food.x = Random(0, 49, 1) $food.y = Random(0, 49, 1) GUICtrlSetBkColor($grid[$food.x][$food.y], 0x00FF00) EndFunc ;==>_makeFood ;==> Func _Main() ;i can't know how autoit handle the main function... it works every time every where... _makeFood() $firsLife = _FirstGenerationArr(10);preparation of individues 10 exactly in a wolrd like struct $life = $firsLife While GUIGetMsg() <> -3 ;not script optimized For $rt = 0 To 1000;the for loop dosent watch if the gui receive the exit message _displayLife($life);the world was rendered Local $N, $persona $N = $life[0][0] For $tio = 1 To $N ;~ sleep(120) $persona = _lifeToIndividue($tio, $life);world array to individue struct (use maps101) $persona = _individueInOut($persona) ;the brain of the individue that want the food $persona = _lookforsomeeat($persona) ;the world look for how eat the food and give +1 ;~ _displayLife($life) If $persona.fitt < (-10) Then $persona = _replaceIndividue($persona);if the fitness goes down to -10 a new full random individue was realized EndIf $life = _individueToLife($persona, $life);then the individue struct go trasformed back to a world array Next Next _procreation($life);every 1000 cicles the procreation appens WEnd EndFunc ;==>_Main Func _replaceIndividue(ByRef $individue, $randomEvolution = 1, $code = 0, $code1 = 0);replace individue randomly or with codeChain If $code = 0 Then $code = individuePrep() GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0xFFFFFF) $individue.x = Random(0, 49, 1) $individue.y = Random(0, 49, 1) GUICtrlSetBkColor($grid[$individue.x][$individue.y], 0x000000) $individue.fitt = 0 ;~ local $randomEvolution=Random(0,1,1) If $randomEvolution Then $individue.code = $code Else $individue.code = _codeChain($code, $code1);code chain is the reproduction algorithm EndIf Return $individue EndFunc ;==>_replaceIndividue Func _procreation(ByRef $life);this is the tournament, 3 individues fight to procreation, the lowest die, and the strongest make half misc babe Local $fitt[3] Local $random[3] Local $f Do $random[0] = Random(1, $life[0][0], 1) $random[1] = Random(1, $life[0][0], 1) $random[2] = Random(1, $life[0][0], 1) Until $random[0] <> $random[1] And $random[1] <> $random[2] And $random[0] <> $random[2] $first = _lifeToIndividue($random[0], $life) $second = _lifeToIndividue($random[1], $life) $third = _lifeToIndividue($random[2], $life) If $first.fitt < $second.fitt And $first.fitt < $third.fitt Then $first = _replaceIndividue($first, $second.code, $third.code) EndIf If $second.fitt < $first.fitt And $second.fitt < $third.fitt Then $second = _replaceIndividue($second, $first.code, $third.code) EndIf If $third.fitt < $first.fitt And $third.fitt < $second.fitt Then $third = _replaceIndividue($third, $first.code, $second.code) EndIf $life = _individueToLife($first, $life) $life = _individueToLife($second, $life) $life = _individueToLife($third, $life) Return $life EndFunc ;==>_procreation Func _codeChain($code, $code1);a quarter from MOM a quarter from DAD etc. etc. Local $mom, $dad, $retgen $mom = StringSplit($code, "|", 2) $dad = StringSplit($code1, "|", 2) $retgen = "|" & $mom[0] & "|" & $dad[1] & "|" & $mom[2] & "|" & $dad[3] Return $retgen EndFunc ;==>_codeChain _Main();the script init x'D While GUIGetMsg() <> -3 ;~ a fucking useless while here WEnd Exit with some comments on
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now