private Entity Tournament(Entity mate, int mateIndex) { // best fit int position = -1; double bestFitnes = double.PositiveInfinity; // good enough fit still in current culture int alrightPosition = -1; double alrightFitness = double.PositiveInfinity; for (int i = 0; i < Cfg.TournamentSize; ++i) { int index = R.NG.Next(Cfg.EntityCount); while (index == mateIndex) { index = R.NG.Next(Cfg.EntityCount); } double compatibility = mate.Compatibility(Entities[index]); if (Entities[index].Fitness <bestFitnes && compatibility> double.Epsilon && compatibility < 0.1) // if very compatible but not identical { bestFitnes = Entities[index].Fitness; position = index; } else if (Entities[index].Fitness < alrightFitness && compatibility < 0.25) // if somewhat compatible or identical { alrightFitness = Entities[index].Fitness; alrightPosition = index; } } if (position == -1) { var targetCulture = parentIncubator.RandomCulture(this); position = R.NG.Next(targetCulture.Entities.Count); var entity = targetCulture.Entities[position]; if (alrightFitness < entity.ComputeFitness(Cfg.FitnessFunction)) { return(Entities[alrightPosition]); } else { return(entity); } } else { return(Entities[position]); } }
private int Prey(Entity hunter, int parentIndex) { if (hunter == null) { return(-1); } if (hunter.Fitness < Champion.Fitness) // if this dethrones the current champion { return(0); } int position = -1; double worstFitness = hunter.Fitness; for (int i = 0; i < Cfg.TournamentSize; ++i) { int index = R.NG.Next(Cfg.EntityCount); while (index == parentIndex) { index = R.NG.Next(Cfg.EntityCount); } if (Entities[index].Fitness > hunter.Fitness && hunter.Compatibility(Entities[index]) < double.Epsilon) // if this is a better version of an existing structure { position = index; break; } else if (Entities[index].Fitness > worstFitness) { worstFitness = Entities[index].Fitness; position = index; } } if (position == -1) { if (hunter.Fitness < Entities[parentIndex].Fitness || (Entities[parentIndex].ChildCount > 2 && parentIndex != 0)) { position = parentIndex; } else { var targetCulture = parentIncubator.RandomCulture(this); targetCulture.AttemptImigration(hunter); } } return(position); }