/// <summary> /// Method that returns a deep-copy clone of the current GP /// </summary> /// <returns>The deep-copy clone</returns> public virtual TGPProgram Clone() { TGPProgram clone = new TGPProgram(mOperatorSet, mVariableSet, mConstantSet, mPrimitiveSet); clone.Copy(this); return(clone); }
/// <summary> /// Method that performs deep copy of another GP /// </summary> /// <param name="rhs">The GP to copy</param> public virtual void Copy(TGPProgram rhs) { mDepth = rhs.mDepth; mLength = rhs.mLength; if (rhs.mRootNode != null) { mRootNode = rhs.mRootNode.Clone(); } }
/// <summary> /// Method that returns whether the current GP is better than another GP in quality /// </summary> /// <param name="rhs">The GP to compare with</param> /// <returns>True if the current GP is better</returns> public virtual bool IsBetterThan(TGPProgram rhs) { if (mDepth < rhs.Depth) { return(true); } else if (mDepth == rhs.Depth) { if (mLength < rhs.Length) { return(true); } else { return(false); } } else { return(false); } }
public void SubtreeCrossover(IGPSolution _rhs, int iMaxDepthForCrossOver, string method, object tag = null) { TGPSolution rhs = (TGPSolution)_rhs; int tree_count = mTrees.Count; for (int i = 0; i < tree_count; ++i) { if (tree_count > 1 && DistributionModel.GetUniform() < 0.5) { TGPProgram temp = rhs.mTrees[i]; rhs.mTrees[i] = mTrees[i]; mTrees[i] = temp; } else { mTrees[i].SubtreeCrossover(rhs.mTrees[i], iMaxDepthForCrossOver, method, tag); } } TrashFitness(); rhs.TrashFitness(); }
public void Analyze(List <S> solutions) { int[] primitive_count_array = new int[mPrimitives.Count]; int[] variable_count_array = new int[mVariables.Count]; double[] variable_fitness_array = new double[mVariables.Count]; for (int i = 0; i < primitive_count_array.Length; ++i) { primitive_count_array[i] = 0; } for (int i = 0; i < variable_fitness_array.Length; ++i) { variable_fitness_array[i] = 0; variable_count_array[i] = 0; } double average_fitness = 0; foreach (S solution in solutions) { average_fitness += FindSolutionFitness(solution); } if (solutions.Count > 0) { average_fitness /= solutions.Count; } foreach (S solution in solutions) { double fitness = FindSolutionFitness(solution); if (fitness < average_fitness) { fitness = 0; } for (int i = 0; i < solution.ProgramCount; ++i) { TGPProgram p = solution.FindProgramAt(i) as TGPProgram; List <TGPPrimitive> plist = p.FlattenPrimitives(); int variable_count = 0; foreach (TGPPrimitive primitive in plist) { if (IsVariable(primitive)) { variable_count++; } } double primitive_fitness = fitness; if (variable_count > 0) { primitive_fitness /= variable_count; } foreach (TGPPrimitive te in plist) { int id = mPrimitiveIdentifiers[te.Symbol]; primitive_count_array[id] += 1; if (mVariableIdentifiers.ContainsKey(te.Symbol)) { id = mVariableIdentifiers[te.Symbol]; variable_fitness_array[id] += primitive_fitness; variable_count_array[id] += 1; } } } } mPrimitiveCountMatrix.Add(primitive_count_array); mVariableCountMatrix.Add(variable_count_array); mFitnessMatrix.Add(variable_fitness_array); }
/// <summary> /// Method that implements the subtree crossover described in Section 2.4 of "A Field Guide to Genetic Programming" /// </summary> /// <param name="rhs">Another tree to be crossover with</param> /// <param name="iMaxDepthForCrossover">The maximum depth of the trees after the crossover</param> public virtual void SubtreeCrossover(TGPProgram rhs, int iMaxDepthForCrossover, string method, object tag = null) { if (method == CROSSOVER_SUBTREE_BIAS || method == CROSSVOER_SUBTREE_NO_BIAS) { bool bias = (method == CROSSOVER_SUBTREE_BIAS); int iMaxDepth1 = CalcDepth(); int iMaxDepth2 = rhs.CalcDepth(); TGPNode pCutPoint1 = null; TGPNode pCutPoint2 = null; bool is_crossover_performed = false; // Suppose that at the beginning both the current GP and the other GP do not violate max depth constraint // then try to see whether a crossover can be performed in such a way that after the crossover, both GP still have depth <= max depth if (iMaxDepth1 <= iMaxDepthForCrossover && iMaxDepth2 <= iMaxDepthForCrossover) { int max_trials = 50; int trials = 0; do { pCutPoint1 = FindRandomNode(bias); pCutPoint2 = rhs.FindRandomNode(bias); if (pCutPoint1 != null && pCutPoint2 != null) { pCutPoint1.Swap(pCutPoint2); iMaxDepth1 = CalcDepth(); iMaxDepth2 = rhs.CalcDepth(); if (iMaxDepth1 <= iMaxDepthForCrossover && iMaxDepth2 <= iMaxDepthForCrossover) //crossover is successful { is_crossover_performed = true; break; } else { pCutPoint1.Swap(pCutPoint2); // swap back so as to restore to the original GP trees if the crossover is not valid due to max depth violation } } trials++; } while (trials < max_trials); } // force at least one crossover even if the maximum depth is violated above so that this operator won't end up like a reproduction operator if (!is_crossover_performed) { pCutPoint1 = FindRandomNode(bias); pCutPoint2 = rhs.FindRandomNode(bias); if (pCutPoint1 != null && pCutPoint2 != null) { pCutPoint1.Swap(pCutPoint2); CalcLength(); rhs.CalcLength(); } } } }