/// <summary> /// Instantiate and get a random furniture /// returned furniture will be in a random position and in a random rotation /// </summary> /// <returns></returns> private Furniture GetRandomFurnitureGene() { int index = _random.Next(_validFurnitureObjectList.Length); Furniture randomFurniture = _validFurnitureObjectList[index].GetComponent <Furniture>(); // According to research paper, position should also be random during initialization var randomPosition = Furniture.GetRandomPossition(MaxXPos, MaxZPos); var randomRotation = Furniture.GetRandomRotation(); randomFurniture.FurnitiGameObject = Instantiate(_validFurnitureObjectList[index], randomPosition, randomRotation); return(randomFurniture); }
public void NewGeneration(int numNewchromosome = 0, bool crossoverNewchromosome = false) { int finalCount = Population.Count + numNewchromosome; if (finalCount <= 0) { return; } if (Population.Count > 0) { // Give fitness value to each chromosome of the population CalculateFitnessForAllChromosomeInThePopulation(); // Sort the population according to its fitness value. The most fittest // population will come on top Population.Sort(CompareChromosome); } // Clear all previously added new population for (int i = 0; i < newPopulation.Count; i++) { newPopulation[i].DestroyAllGenes();; } newPopulation.Clear(); // Determine new population set for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } if (i < Population.Count || crossoverNewchromosome) { // Fittest parents will be chosen for crossover Chromosome <T> parent1 = ChooseTheFittestParent(); Chromosome <T> parent2 = ChooseTheFittestParent(); Chromosome <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new Chromosome <T>(_maxGenes, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } // Randomize the rotation and position of each gene in the chromosome for (int i = 0; i < newPopulation.Count; i++) { if (newPopulation[i].Genes is Furniture[]) { var furnitures = newPopulation[i].Genes as Furniture[]; for (int j = 0; j < furnitures.Length; j++) { if (furnitures[j].FurnitiGameObject == null) { continue; } furnitures[j].FurnitiGameObject.transform.position = Furniture.GetRandomPossition(RoomController.Instance.MaxXPos, RoomController.Instance.MaxZPos); furnitures[j].FurnitiGameObject.transform.rotation = Furniture.GetRandomRotation(); } } } List <Chromosome <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }