@include "Control.tz" @include "PatchGrid.tz" @include "Patch.tz" @include "GeneticAlgorithm.tz" Controller MyController. @define X_DIM 15. @define Y_DIM 15. @define Z_DIM 1. Control : MyController { + variables: ga (object). grid (object). + to init: ga = new MyGeneticAlgorithm. ga set-fitness-test-duration to 100. ga set-individual-class to "Organism". ga set-population-size to 25. ga set-mutation-percent to 25. ga set-generation-limit to 10. self add-dependency on ga. # since we're building a discrete model, we can set the # integration and iteration time-step to 1.0. self set-integration-step to 1.0. self set-iteration-step to 1.0. # choose a nicer background colour self set-background-color to (1,1,1). + to setup-test with i (object): grid = i get-grid. self add-dependency on grid. + to iterate: super iterate. } GeneticAlgorithm : MyGeneticAlgorithm { + to start-fitness-test of individual (object): controller setup-test with individual. + to end-fitness-test of individual (object): individual set-fitness to random[100]. } GeneticAlgorithmIndividual : Organism { + variables: genome (object). grid (object). + to init: self randomize. grid = (new PatchToroid init-with x-count X_DIM y-count Y_DIM z-count Z_DIM location (0,0,0) patch-size (.5,.5,.5) patch-class "Cell"). + to get-grid: return grid. + to get-genome: return genome. + to set-fitness to fitnessValue (double): fitness = fitnessValue. + to randomize: + to copy from other (object): + to mutate: } Patch : Cell { + variables: type (int). + to set-type to value (int): type = value. if type == 0: self set-color to (0.8, 0.8, 0.8). else if type == 1: self set-color to (0.5, 0.5, 0.5). else if type == 2: self set-color to (0.0, 0.0, 1.0). else if type == 3: self set-color to (1.0, 1.0, 1.0). else if type == 4: self set-color to (1.0, 0.0, 0.0). + to init-patch: self set-type to random[4]. + to be-born from mother (object): #print "being born". + to iterate: #if random[10000] <= 1: self set-type to random[4]. super iterate. }