Fourmis[] selection(Fourmis[] populationFourmis) { Fourmis[] f = new Fourmis[nbSelect]; for (int i = 0; i < nbSelect; i++) { f[i] = populationFourmis[i]; } return(f); }
public Fourmis mutate() { string[] label = { "if", "move", "right", "left", "prog2", "prog3" }; Fourmis f = new Fourmis(fDNA.clone()); Fourmis a = new Fourmis(new DNA(label[Random.Range(0, 6)])); a.getDNA().createRandom(5); f.fDNA.setDnaAt((Random.Range(0, f.fDNA.getSize())), (a.getDNA().clone())); f.energy = initEnergy; return(f); }
void sortLastList(Fourmis[] populationFourmis) { for (int i = nbPopulation - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (populationFourmis[j].getScore() < populationFourmis[j + 1].getScore()) { Fourmis tmp = populationFourmis[j + 1]; populationFourmis[j + 1] = populationFourmis[j]; populationFourmis[j] = tmp; } } } }
Fourmis[] initiatePopulation() { Fourmis[] populationFourmis = new Fourmis[nbPopulation]; for (int i = 0; i < nbPopulation; i++) { Fourmis a = new Fourmis(new DNA(label[Random.Range(0, 6)])); a.getDNA().createRandom(5); a.fullProg(); populationFourmis[i] = a; } return(populationFourmis); }
public Fourmis crossOver(Fourmis a) { Fourmis f = new Fourmis(fDNA.clone()); Fourmis f1 = new Fourmis(a.getDNA().clone()); int ind1 = (Random.Range(0, f.fDNA.getSize())); //int ind1 = (Random.Range(f.fDNA.getSize()/2, f.fDNA.getSize())); //int ind1 = (f.fDNA.getSize()-1); // int ind2 = (Random.Range(0, f1.getDNA().getSize())); int ind2 = (Random.Range(0, f1.getDNA().getSize())); f.fDNA.setDnaAt(ind1, (f1.fDNA.getDnaAt(ind2).clone())); f.energy = initEnergy; return(f); }
Fourmis[] crossOver(Fourmis[] populationFourmis, Fourmis[] selectedList) { Fourmis [] l = new Fourmis[nbPopulation]; for (int i = 0; i < 10; i++) { l[i] = (new Fourmis(selectedList[i].getDNA().clone())); } for (int i = 10; i < nbPopulation; i++) { l[i] = (selectedList[Random.Range(0, nbSelect)].crossOver(selectedList[Random.Range(0, nbSelect)])); } //sortList(l); return(l); }
void Start() { //Random.InitState(3); // Seed fonctionne pour 9 gene //Random.InitState(1); for (int i = 0; i < 32; i++) { for (int j = 0; j < 32; j++) { if (map[i, j] == 0) { GameObject newTile = Instantiate(mapTile); newTile.transform.position = new Vector3(i, j); Tiles[i, j] = newTile; } else { GameObject newTile = Instantiate(mapTile1); newTile.transform.position = new Vector3(i, j); Tiles[i, j] = newTile; } } } //List<Fourmis> populationFourmis = new List<Fourmis>(); Fourmis[] populationFourmis = new Fourmis[nbPopulation]; populationFourmis = initiatePopulation(); sortList(populationFourmis); for (int i = 0; i < nbGeneration; i++) { sortList(populationFourmis); populationFourmis = crossOver(populationFourmis, selection(populationFourmis)); mutate(populationFourmis); runPopulation(populationFourmis); } sortLastList(populationFourmis); for (int i = 0; i < nbPopulation; i++) { // Debug.Log(populationFourmis[i].getScore()); } pF = populationFourmis; /* * DNA f214 = new DNA("if"); * DNA f215 = new DNA("move"); * DNA f216 = new DNA("prog2"); * * DNA f217 = new DNA("right"); * DNA f218 = new DNA("if"); * DNA f219 = new DNA("move"); * DNA f220 = new DNA("prog3"); * DNA f221 = new DNA("left"); * DNA f222 = new DNA("left"); * DNA f223 = new DNA("if"); * DNA f224 = new DNA("move"); * DNA f225 = new DNA("prog2"); * DNA f226 = new DNA("right"); * DNA f227 = new DNA("move"); * * DNA[] tab = new DNA[2]; * tab[0] = f215; * tab[1] = f216; * * f214.setChild(tab); * * DNA[] tab1 = new DNA[2]; * tab1[0] = f217; * tab1[1] = f218; * * * f216.setChild(tab1); * * DNA[] tab2 = new DNA[2]; * tab2[0] = f219; * tab2[1] = f220; * * f218.setChild(tab2); * * DNA[] tab3 = new DNA[3]; * tab3[0] = f221; * tab3[1] = f222; * tab3[2] = f223; * * * f220.setChild(tab3); * * DNA[] tab4 = new DNA[2]; * tab4[0] = f224; * tab4[1] = f225; * * * f223.setChild(tab4); * * DNA[] tab5 = new DNA[2]; * tab5[0] = f226; * tab5[1] = f227; * * * f225.setChild(tab5); * * //pF.Add(new Fourmis(f214)); * //pF[0].fullProg(); * DNA f228 = new DNA("if"); * f228.createRandom(3); * * Fourmis f1 = new Fourmis(f214); * Fourmis f2 = new Fourmis(f228); * f1.crossOver(f2); */ }