public static Tower ConstructTower1(Tower tow) { Tower retour=(Tower)tow.MemberwiseClone (); retour.fitness = 0; mutation (retour); return retour; }
public static Tower ConstructTower2(Tower parent1,Tower parent2) { int total = 50; int i = 0; Tower retour=(Tower)parent1.MemberwiseClone (); List<Arme> temp = new List<Arme> (parent1.weapons); parent1.weapons.Clear (); int random = 0; while (total>0&&temp.Count>0) { int decrement=0; random=UnityEngine.Random.Range(0,temp.Count); if(temp[random].taille==(int)taille.grand){ decrement+=10; }else{ decrement+=5; } if(temp[random].Type==(int)type.laser){ }else if (temp[random].Type==(int)type.minigun){ }else{ } total-=decrement; if(total>=0){ retour.weapons.Add(temp[random]); } temp.RemoveAt(random); i++; } total = 50; temp = new List<Arme> (parent2.weapons); while (total>0&&temp.Count>0) { int decrement=0; random=UnityEngine.Random.Range(0,temp.Count); if(temp[random].taille==(int)taille.grand){ decrement+=10; }else{ decrement+=5; } if(temp[random].Type==(int)type.laser){ }else if (temp[random].Type==(int)type.minigun){ }else{ } if(retour.weapons.Exists(ar=>ar.pos.x==temp[random].pos.x&&ar.pos.y==temp[random].pos.y&&ar.pos.z==temp[random].pos.z)){ decrement=0; } total-=decrement; if(total>=0&&decrement!=0){ retour.weapons.Add(temp[random]); } temp.RemoveAt(random); } retour.fitness = 0; mutation (retour); return retour; }
public static void mutation(Tower tow) { foreach (Arme ar in tow.weapons) { if (UnityEngine.Random.Range (0f, 1f) < Population.mutation) { int face=0; if(ar.rotation.Equals(Quaternion.Euler(0,0,0))){ face=0; }else if(ar.rotation.Equals(Quaternion.Euler(0,180,0))){ face=1; }else if(ar.rotation.Equals(Quaternion.Euler(0,-90,0))){ face=2; }else if(ar.rotation.Equals(Quaternion.Euler(0,90,0))){ face=3; } int x=0; int y=0; for(int i=0;i<3;i++){ for (int j=0;j<4;j++){ if(ar.pos.x==position_fixe[face][i,j].x&&ar.pos.y==position_fixe[face][i,j].y&&ar.pos.z==position_fixe[face][i,j].z){ x=i; y=j; } } } int count=0; do{ int rand=UnityEngine.Random.Range(0,4); if(rand==0){ x-=1; if(x<0){ x=2; face+=2; if(face>3){ face=face%3; } } }else if(rand==1){ x+=1; if(x>2){ x=0; face -=2; if(face<0){ face+=4; } } }else if(rand==2){ y+=1; if(y>3){ y=0; } }else if (rand ==3){ y-=1; if(y<0){ y=3; } } count++; }while(tow.position[face][x,y]&& count<10); } if(UnityEngine.Random.Range(0f,1f)<Population.mutation){ int rand; if((int)ar.Type==1){ rand=UnityEngine.Random.Range(2,4); }else if((int)ar.Type==3){ rand=UnityEngine.Random.Range (1,3); }else{ double test=UnityEngine.Random.Range(0f,1f); if(test<0.5f){ rand=1; }else{ rand=3; } } ar.Type=rand; } if(UnityEngine.Random.Range(0f,1f)<Population.mutation){ if(ar.taille==(int)taille.petit){ ar.taille=(int)taille.grand; }else{ ar.taille=(int)taille.petit; } } } }
public static void mutation(Tower tow) { foreach (Arme ar in tow.weapons) { if (UnityEngine.Random.Range(0f, 1f) < Population.mutation) { int face = 0; if (ar.rotation.Equals(Quaternion.Euler(0, 0, 0))) { face = 0; } else if (ar.rotation.Equals(Quaternion.Euler(0, 180, 0))) { face = 1; } else if (ar.rotation.Equals(Quaternion.Euler(0, -90, 0))) { face = 2; } else if (ar.rotation.Equals(Quaternion.Euler(0, 90, 0))) { face = 3; } int x = 0; int y = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (ar.pos.x == position_fixe[face][i, j].x && ar.pos.y == position_fixe[face][i, j].y && ar.pos.z == position_fixe[face][i, j].z) { x = i; y = j; } } } int count = 0; do { int rand = UnityEngine.Random.Range(0, 4); if (rand == 0) { x -= 1; if (x < 0) { x = 2; face += 2; if (face > 3) { face = face % 3; } } } else if (rand == 1) { x += 1; if (x > 2) { x = 0; face -= 2; if (face < 0) { face += 4; } } } else if (rand == 2) { y += 1; if (y > 3) { y = 0; } } else if (rand == 3) { y -= 1; if (y < 0) { y = 3; } } count++; }while(tow.position[face][x, y] && count < 10); } if (UnityEngine.Random.Range(0f, 1f) < Population.mutation) { int rand; if ((int)ar.Type == 1) { rand = UnityEngine.Random.Range(2, 4); } else if ((int)ar.Type == 3) { rand = UnityEngine.Random.Range(1, 3); } else { double test = UnityEngine.Random.Range(0f, 1f); if (test < 0.5f) { rand = 1; } else { rand = 3; } } ar.Type = rand; } if (UnityEngine.Random.Range(0f, 1f) < Population.mutation) { if (ar.taille == (int)taille.petit) { ar.taille = (int)taille.grand; } else { ar.taille = (int)taille.petit; } } } }
public static Tower ConstructTower2(Tower parent1, Tower parent2) { int total = 50; int i = 0; Tower retour = (Tower)parent1.MemberwiseClone(); List <Arme> temp = new List <Arme> (parent1.weapons); parent1.weapons.Clear(); int random = 0; while (total > 0 && temp.Count > 0) { int decrement = 0; random = UnityEngine.Random.Range(0, temp.Count); if (temp[random].taille == (int)taille.grand) { decrement += 10; } else { decrement += 5; } if (temp[random].Type == (int)type.laser) { } else if (temp[random].Type == (int)type.minigun) { } else { } total -= decrement; if (total >= 0) { retour.weapons.Add(temp[random]); } temp.RemoveAt(random); i++; } total = 50; temp = new List <Arme> (parent2.weapons); while (total > 0 && temp.Count > 0) { int decrement = 0; random = UnityEngine.Random.Range(0, temp.Count); if (temp[random].taille == (int)taille.grand) { decrement += 10; } else { decrement += 5; } if (temp[random].Type == (int)type.laser) { } else if (temp[random].Type == (int)type.minigun) { } else { } if (retour.weapons.Exists(ar => ar.pos.x == temp[random].pos.x && ar.pos.y == temp[random].pos.y && ar.pos.z == temp[random].pos.z)) { decrement = 0; } total -= decrement; if (total >= 0 && decrement != 0) { retour.weapons.Add(temp[random]); } temp.RemoveAt(random); } retour.fitness = 0; mutation(retour); return(retour); }
public void reproduction() { List <int> index = new List <int> (); for (int i = 0; i < (int)Math.Round((double)(nbtour / 2)); i++) { index.Add(i); } Shuffle(index); Tower parent1; Tower parent2; UnityEngine.Random.seed = 213494651; for (int i = 0; i < nbtour - 1; i++) { if (index.Count <= 1) { int randint = UnityEngine.Random.Range(0, (int)Math.Round((double)(nbtour / 2))); parent1 = individuSelec[randint]; int randint2 = UnityEngine.Random.Range(0, (int)Math.Round((double)(nbtour / 2))); while (randint == randint2) { randint2 = UnityEngine.Random.Range(0, (int)Math.Round((double)(nbtour / 2))); if ((int)Math.Round((double)(nbtour / 2)) == 2) { if (randint == 0) { randint2 = 1; } else { randint2 = 0; } } } parent2 = individuSelec[randint2]; } else { int randint = UnityEngine.Random.Range(0, index.Count); parent1 = individuSelec[index[randint]]; int randint2 = UnityEngine.Random.Range(0, index.Count); while (randint == randint2) { randint2 = UnityEngine.Random.Range(0, index.Count); if (index.Count == 2) { if (randint == 0) { randint2 = 1; } else { randint2 = 0; } } } parent2 = individuSelec[index[randint2]]; index.Remove(randint); } float random = UnityEngine.Random.Range(0f, 1f); if (random < 0.75) { futPop[i] = Tower.ConstructTower2(parent1, parent2); } else { futPop[i] = Tower.ConstructTower1(parent1); } } }