private int amount; //The amount of entitys in this population. /// <summary> /// Constuctor. Creates the entitys in the population. /// </summary> /// <param name="world">The world the population lives in.</param> /// <param name="amount">The amount of entitys in this population.</param> public Population(SimulationWorld world, int amount) { this.amount = amount; this.world = world; entitys = new AIEntity[amount]; dead = new List <AIEntity>(); for (int i = 0; i < amount; i++) { entitys[i] = new AIEntity(world); } }
/// <summary> /// Breed the population in the world. /// </summary> /// <param name="world">The world the population lives in.</param> /// <param name="population">The population we want to breed.</param> public static void Breed(SimulationWorld world, Population population) { AIEntity[] entitys = population.GetEntitys(); //Bread the top half section to the lower half. int topAmount = (int)(population.GetAmount() * 0.5f); for (int i = 0; i < topAmount; i += 2) { Genome a = entitys[i].GetGenome(); Genome b = entitys[i + 1].GetGenome(); BreedGenome(world, a, b, i, entitys, population.GetAmount()); entitys[i] = new AIEntity(world, a.Copy()); entitys[i + 1] = new AIEntity(world, b.Copy()); } }
/// <summary> /// Mergesort 2 lists. /// </summary> /// <param name="entitys">The entity list to sort.</param> /// <param name="left">The left index.</param> /// <param name="mid">The middle index.</param> /// <param name="right">The right index.</param> public static void DoMerge(AIEntity[] entitys, int left, int mid, int right) { AIEntity[] temp = new AIEntity[entitys.Length]; int i, left_end, num_elements, tmp_pos; left_end = (mid - 1); tmp_pos = left; num_elements = (right - left + 1); while ((left <= left_end) && (mid <= right)) { if (entitys[left].GetFittingValue() >= entitys[mid].GetFittingValue()) { temp[tmp_pos++] = entitys[left++]; } else { temp[tmp_pos++] = entitys[mid++]; } } while (left <= left_end) { temp[tmp_pos++] = entitys[left++]; } while (mid <= right) { temp[tmp_pos++] = entitys[mid++]; } for (i = 0; i < num_elements; i++) { entitys[right] = temp[right]; right--; } }