public virtual LGPProgram Clone() { LGPProgram clone = new LGPProgram(mPop, mOperatorSet.Clone()); clone.Copy(this); return(clone); }
public virtual LGPProgram CreateProgram(int size, LGPEnvironment env) { LGPProgram program = new LGPProgram(this, mOperatorSet.Clone()); program.Create(size); return(program); }
public virtual void BreedInitialPopulation() { Setup(); int iPopulationSize = mConfig.PopulationSize; mPopInitInstructionFactory.Initialize(this); EvaluateFitness(); mGlobalBestProgram = FindFittestProgramInCurrentGeneration().Clone(); }
public static void Shuffle(IList <LGPProgram> list) { Random rng = new Random(); int n = list.Count; while (n > 1) { n--; int k = rng.Next(n + 1); LGPProgram value = list[k]; list[k] = list[n]; list[n] = value; } }
public void Replace(LGPProgram weak_program_in_current_pop, LGPProgram child_program) { //bool found = false; for (int i = 0; i < mPrograms.Count; ++i) { if (mPrograms[i] == weak_program_in_current_pop) { //found = true; mPrograms[i] = child_program; } } //if (found == false) //{ // throw new Exception(); //} }
public void Copy(LGPProgram rhs) { mSetup = rhs.mSetup; mFitness = rhs.mFitness; mObjectiveValue = rhs.mObjectiveValue; mIsFitnessValid = rhs.mIsFitnessValid; mRegisterSet = rhs.mRegisterSet.Clone(); mOperatorSet = rhs.mOperatorSet.Clone(); mConstantSet = rhs.mConstantSet.Clone(); mPop = rhs.mPop; for (int i = 0; i < rhs.mInstructions.Count; ++i) { mInstructions.Add(rhs.mInstructions[i].Clone()); mInstructions[i].Program = this; } }
public bool IsBetterThan(LGPProgram rhs) { return(mFitness > rhs.Fitness); }
public void AddProgram(LGPProgram lgp) { mPrograms.Add(lgp); }
public virtual void InitializeProgramRegisters(LGPProgram lgp, LGPFitnessCase fitness_case) { mRegInitInstructionFactory.InitializeRegisters(lgp.RegisterSet, lgp.ConstantSet, fitness_case); }
public virtual void Evolve() { int iPopSize = this.mConfig.PopulationSize; int program_count = 0; while (program_count < iPopSize) { KeyValuePair <LGPProgram, LGPProgram> tournament_winners = new KeyValuePair <LGPProgram, LGPProgram>(); KeyValuePair <LGPProgram, LGPProgram> tournament_losers = new KeyValuePair <LGPProgram, LGPProgram>(); mReproductionSelectionInstructionFactory.Select(this, ref tournament_winners, ref tournament_losers); LGPProgram tp1 = tournament_winners.Key.Clone(); LGPProgram tp2 = tournament_winners.Value.Clone(); double r = DistributionModel.GetUniform(); if (r < this.mConfig.CrossoverRate) { mCrossoverInstructionFactory.Crossover(this, tp1, tp2); } r = DistributionModel.GetUniform(); if (r < this.mConfig.MacroMutationRate) { mMutationInstructionFactory.Mutate(this, tp1); } r = DistributionModel.GetUniform(); if (r < this.mConfig.MacroMutationRate) { mMutationInstructionFactory.Mutate(this, tp2); } r = DistributionModel.GetUniform(); if (r < this.mConfig.MicroMutationRate) { tp1.MicroMutate(mGaussian); } r = DistributionModel.GetUniform(); if (r < this.mConfig.MicroMutationRate) { tp2.MicroMutate(mGaussian); } if (!tp1.IsFitnessValid) { tp1.EvaluateFitness(); } if (!tp2.IsFitnessValid) { tp2.EvaluateFitness(); } if (tp1.IsBetterThan(tp2)) { if (tp1.IsBetterThan(mGlobalBestProgram)) { mGlobalBestProgram = tp1.Clone(); } } else { if (tp2.IsBetterThan(mGlobalBestProgram)) { mGlobalBestProgram = tp2.Clone(); } } LGPProgram loser1 = mSurvivalInstructionFactory.Compete(this, tournament_losers.Key, tp1); // this method returns the pointer to the loser in the competition for survival; LGPProgram loser2 = mSurvivalInstructionFactory.Compete(this, tournament_losers.Value, tp2); if (loser1 == tournament_losers.Key) { ++program_count; } if (loser2 == tournament_losers.Value) { ++program_count; } } mCurrentGeneration++; }
public LGPInstruction(LGPProgram prog) { mProgram = prog; }