예제 #1
0
        private void DiscreteRecombination(ref ESSolution offspring)
        {
            int index1, index2;
            int index = 0;

            GenerateTwoIndeces(out index1, out index2);

            ESSolution parent1 = this.parentPopulation[index1];
            ESSolution parent2 = this.parentPopulation[index2];

            offspring = parent1.MakeDeepCopy();

            foreach (var variable in base.context.Variables)
            {
                if (base.random.Next(0, 2) != 0)
                {
                    base.modelBuilder.TryAssign(variable, parent2.currentModel.GetValue(variable.Term));
                    offspring.SetStdDev(index, parent2.GetStdDev(index));
                }
                index++;
            }

            offspring.UpdateModel(base.modelBuilder.ToArithmeticModel());
        }
예제 #2
0
        private TryGetModelResult Evolve(out IArithmeticModel model)
        {
            TryGetModelResult result = TryGetModelResult.None;

            //SafeDebugger.Break();

            CreateInitialPopulation();

            EvaluateIntermediatePopulation();

            for (int i = 0; i < this.populationSize; i++)
            {
                this.parentPopulation[i] = this.intermediatePopulation[i].MakeDeepCopy();
            }

            while (!Terminate(out result, out model))
            {
                ClearIntermediatePopulation();

                if (this.populationSize > 1 && this.recombination != RecombinationStrategy.None &&
                    this.parentPopulation.Length > 1)
                {
                    ESSolution offspring = null;

                    for (int i = 0; i < this.offspringPopulationSize; i++)
                    {
                        Recombine(ref offspring);

                        if (offspring != null)
                        {
                            Mutate(ref offspring);

                            this.intermediatePopulation.Add(offspring.MakeDeepCopy());
                            offspring.Dispose();
                            offspring = null;
                        }
                    }
                }
                else
                {
                    ESSolution offspring = null;
                    int        poolIndex = 0;
                    for (int i = 0; i < this.offspringPopulationSize; i++)
                    {
                        if (poolIndex >= this.parentPopulation.Length)
                        {
                            poolIndex = 0;
                        }

                        offspring = this.parentPopulation[poolIndex].MakeDeepCopy();
                        Mutate(ref offspring);
                        this.intermediatePopulation.Add(offspring.MakeDeepCopy());
                        offspring.Dispose();
                        offspring = null;
                        poolIndex++;
                    }
                }

                EvaluateIntermediatePopulation();

                //selection
                if (!this.selectFromOffspringOnly)
                {
                    for (int i = 0; i < this.populationSize; i++)
                    {
                        this.intermediatePopulation.Add(this.parentPopulation[i].MakeDeepCopy());
                    }
                }

                this.intermediatePopulation.Sort();

                for (int i = 0; i < this.populationSize && i < this.intermediatePopulation.Count; i++)
                {
                    this.parentPopulation[i].Dispose();
                    this.parentPopulation[i] = this.intermediatePopulation[i].MakeDeepCopy();
                }
            }

            return(result);
        }