public void ShouldMutateIndividual() { var sut = new Mutator <int>(new RandonInvididualGenesPoint(3)); var mutated = sut.Mutate(new List <int[]>() { new int[] { 1, 2, 3, 4 } }, 1); IndividualUtils.AssertIndividualEqual(new int[] { 3, 2, 1, 4 }, mutated.First()); }
public void ShouldGetParentBIfSize0() { var sut = new DavisOrderCrossOver <int>(new DefinedPointSelector(new int[] { 2, 0 })); var parents = new List <int[]> { new [] { 1, 2, 3, 4, 5, 6 }, new [] { 3, 6, 5, 2, 1, 4 }, }; var offspring = sut.Cross(parents); IndividualUtils.AssertIndividualEqual(offspring.First(), new [] { 3, 6, 5, 2, 1, 4 }); }
// public override void assignWeaponToTarget(GameObject[] Weapons, GameObject[] Targets) { if (Targets.Length <= 0) { return; } //if (running) // return; Population population = new Population(50, Weapons, Targets, true); float maxRedInit = population.getFittest().getFitNess(); float currentRed = maxRedInit; float precRed = 0.0f; bool optimum = false; int nIter = 0; m_InitialFitness = maxRedInit; //m_CurrentFitness = m_InitialFitness; ////////////// iteration ///////////// //population = evolvePopulation(population, Targets.Length); m_CurrentFitness = population.getFittest().getFitNess(); //m_CurrentFitness = 0.0f; /*while ( currentRed >= precRed) * { * precRed = currentRed; * population = evolvePopulation(population,Targets.Length); * currentRed = population.getFittest().getFitNess(); * nIter++; * m_CurrentTimeExecution -= Time.deltaTime; ; * if (m_CurrentTimeExecution <=0.0f) * { * m_CurrentTimeExecution = m_TimeExecution; * break; * } * }*/ //m_CurrentFitness = currentRed; //if (nIter <= 5) // optimum = true; //if (optimum) IndividualUtils.assignWeaponToTarget(population.getFittest(), Weapons, Targets); running = true; }
public void assignWeaponToTarget(GameObject[] Weapons, GameObject[] Targets, Team team) { if (Targets.Length <= 0) { return; } //if (running) // return; Population population = new Population(50, Weapons, Targets, true, team); float maxRedInit = population.getFittest().getFitNess(); float currentRed = maxRedInit; float precRed = 0.0f; bool optimum = false; int nIter = 0; m_InitialFitness = maxRedInit; m_CurrentFitness = population.getFittest().getFitNess(); IndividualUtils.assignWeaponToTarget(population.getFittest(), Weapons, Targets); running = true; }
// Calculate inidividuals fittness by comparing it to our candidate solution public static float getFitness(Individual individual, GameObject[] Weapons, GameObject[] Targets) { float fitness = 1.0f; float[] H = new float[Targets.Length]; float[] hl = new float[Targets.Length]; FitnessCalculator.getIndividualHealth(individual, Targets, ref H, ref hl); // Loop through our individuals genes and compare them to our cadidates for (int i = 0; i < individual.getSize(); i++) { GameObject ally = Weapons[i]; TargetSystem targetSystem = ally.GetComponent <TargetSystem>(); float dmg = ally.GetComponent <TankShooting>().damage; float minValue = 100000; int allocated = -1; // target index int indTarget = individual.getSolution(i); GameObject ennemy = Targets[indTarget]; if (hl[indTarget] <= 0.0f) { continue; } float reduct = 1 - (hl[indTarget] - dmg) / hl[indTarget]; // update health left hl[indTarget] -= dmg; if (hl[indTarget] <= 0.0f) { hl[indTarget] = 0.0f; } // add reduction to fitness // fitness+=reduct; } for (int i = 0; i < Targets.Length; i++) { if (!IndividualUtils.isTargetExitsInSolution(i, individual)) { continue; } float reduction = 1.0f - (hl[i] / H[i]); fitness *= reduction; } //for (int i = 0; i < individual.getSize(); i++) //{ // int indTarget = individual.getSolution(i); // GameObject ennemy = Targets[indTarget]; // if (hl[indTarget] <= 0.0f) // continue; // float reduction = 1.0f - (hl[i] / H[i]); // fitness *= reduction; //} /*if (fitness < 0.000001f) * fitness = 0.000001f;*/ return(fitness); }