/** * This method will take a candidate list and identify is there is redundant * individual in it. If yes, it will get rid of the redundant individuals. * After that, it will check if all the samples from this generation have * been visited in previous generation. If yes, it will retrieve the samples * from previous generations. */ public IList <Individual> UniqueSamples(IEvolutionState state, IList <Individual> candidates) { // first filter out the redundant sample with in the set of candidates HashSet <Individual> set = new HashSet <Individual>(); foreach (Individual i in candidates) { if (!set.Contains(i)) { set.Add(i); } } // now all the individual in candidates are unique with in the set candidates = new List <Individual>(set); // Sk will be the new population IList <Individual> sk = new List <Individual>(); // see if we have these individual in visted array before for (int i = 0; i < candidates.Count; ++i) { IntegerVectorIndividual individual = (IntegerVectorIndividual)candidates[i]; if (VisitedIndexMap.ContainsKey(individual)) { // we have this individual before, retrieve that int index = VisitedIndexMap[individual]; // get the original individual individual = (IntegerVectorIndividual)Visited[index]; } else { Visited.Add(individual); VisitedIndexMap[individual] = Visited.Count - 1; // We add the new individual into the CornerMap // NOTE: if the individual already, we still need to do this? // original code says yes, but it seems to be wrong // so we do this only the new individual is new for (int j = 0; j < GenomeSize; ++j) { // The individual is the content. The key is its // coordinate position Corners[j].Insert(individual.genome[j], individual); } } sk.Add(individual); } return(sk); }
public void IntegerVectorIndividualWriteAndRead() { // First we'll set up a Fitness for the Individual var rand = new MersenneTwisterFast(0); var f = new SimpleFitness(); f.SetFitness(null, float.MaxValue, true); const int n = 10; f.Trials = new List <double>(n); for (var i = 0; i < n; i++) { f.Trials.Add(rand.NextDouble()); } // Now we can create and initialize the Individual var ind = new IntegerVectorIndividual(); var ind2 = new IntegerVectorIndividual(); // We'll read back into this instance ind.Genome = new int[10]; // This is the set of genes for (var i = 0; i < 10; i++) { ind.genome[i] = rand.NextInt(); // Some random genes } ind.Fitness = f; ind.Evaluated = true; using (var ms = new MemoryStream()) { var writer = new BinaryWriter(ms); ind.WriteIndividual(null, writer); ms.Position = 0; var reader = new BinaryReader(ms); ind2.Fitness = new SimpleFitness(); ind2.ReadIndividual(null, reader); Assert.IsTrue(ind.Fitness.EquivalentTo(ind2.Fitness)); Assert.IsTrue(ind2.Fitness.IsIdeal); Assert.IsTrue(ind.Equals(ind2)); // Genetically equivalent for (var i = 0; i < 10; i++) { Assert.AreEqual(ind.genome[i], ind2.genome[i]); // check each gene } } }
/** * In DOVS, we provide the algorithm with a start individual from file, this * start individual is the start search point of the DOVS algorithm. We use * this start point to construct a hyperbox contains promising solutions, * and sample from this region, the number of sample is equal to parameter * "pop.subpop.X.size" in parameter files. * * However, due to redundant samples, we the individuals size may be * smaller than what have been specified in pop.subpop.X.size. */ public override Population InitialPopulation(IEvolutionState state, int thread) { Population p = base.InitialPopulation(state, thread); // make sure the each subpop only have one individual for (int i = 0; i < p.Subpops.Count; i++) { if (p.Subpops[i].Species is DOVSSpecies) { DOVSSpecies species = (DOVSSpecies)p.Subpops[i].Species; if (p.Subpops[i].Individuals.Count != 1) { state.Output.Fatal("contain more than one start point"); } // add the start point to the visited ArrayList species.Visited.Clear(); species.Visited.Add(p.Subpops[i].Individuals[0]); species.VisitedIndexMap[p.Subpops[i].Individuals[0]] = 0; species.OptimalIndex = 0; IntegerVectorIndividual ind = (IntegerVectorIndividual)species.Visited[species.OptimalIndex]; // For the visited solution, record its coordinate // positions in the multimap for (int j = 0; j < species.GenomeSize; ++j) { // The individual is the content. The key is its // coordinate position species.Corners[j].Insert(ind.genome[j], ind); } // update MPA species.UpdateMostPromisingArea(state); // sample from MPA int initialSize = p.Subpops[i].InitialSize; IList <Individual> candidates = species.MostPromisingAreaSamples(state, initialSize); // get unique candidates for evaluation, this is Sk in paper IList <Individual> uniqueCandidates = species.UniqueSamples(state, candidates); // update the individuals p.Subpops[i].Individuals = uniqueCandidates; } } return(p); }
/** * Given a list of individuals, it will find the one with highest fitness * value and retrieve its index in visited solution list. */ private int FindOptimalIndividual(IList <Individual> list) { double maximum = int.MinValue; IntegerVectorIndividual bestInd = null; for (int i = 0; i < list.Count; ++i) { IntegerVectorIndividual ind = (IntegerVectorIndividual)list[i]; if (((DOVSFitness)ind.Fitness).Mean > maximum) { maximum = ((DOVSFitness)ind.Fitness).Mean; bestInd = ind; } } return(VisitedIndexMap[bestInd]); }