/// <summary> /// Creates a new instance of the <see cref="CompoundExpression"/> class. /// </summary> /// <param name="operator">The operator use in the expression.</param> /// <param name="first">The first input to the operator.</param> /// <param name="second">The second input to the operator.</param> /// <param name="balanced">Whether the expression has been balanced.</param> private CompoundExpression(CompoundOperator @operator, IExpression first, IExpression second, bool balanced) { Operator = @operator; First = first; Second = second; _balanced = balanced; }
/// <summary> /// Creates a new instance of the <see cref="CompoundExpression"/> class. /// </summary> /// <param name="operator">The operator use in the expression.</param> /// <param name="first">The first input to the operator.</param> /// <param name="second">The second input to the operator.</param> /// <param name="fileName">The name of the file.</param> /// <param name="errorToken">The token to report any compiler errors to.</param> public CompoundExpression(CompoundOperator @operator, IExpression first, IExpression second, string fileName, IToken errorToken) { Operator = @operator; First = first; Second = second; FileName = fileName; ErrorToken = errorToken; }
/// <summary> /// Generates a balanced binary tree from a collection of values. /// </summary> /// <param name="chain">The list of values.</param> /// <param name="primaryOp">The operation to be used between adjacent values.</param> /// <param name="secondaryOp">The operation used on the RHS of rebalanced trees.</param> /// <returns>A compound expression combining all the values in the chain which is balanced.</returns> private static CompoundExpression Rebuild(List <IExpression> chain, CompoundOperator primaryOp, CompoundOperator secondaryOp) { // Get point to split chain at int midPos = (int)Math.Floor(chain.Count / 2d); // First IExpression first = midPos == 1 ? chain[0] : Rebuild(chain.GetRange(0, midPos), primaryOp, secondaryOp); // Second IExpression second = chain.Count == 2 ? chain[1] : Rebuild(chain.GetRange(midPos, chain.Count - midPos), secondaryOp, secondaryOp); // Combine return(new CompoundExpression(primaryOp, first, second, true)); }
/// <summary> /// Construct a NEAT (or HyperNEAT trainer. /// </summary> /// <param name="population">The population.</param> /// <param name="calculateScore">The score function.</param> /// <returns>The NEAT EA trainer.</returns> public static TrainEA ConstructNEATTrainer(NEATPopulation population, ICalculateScore calculateScore) { var result = new TrainEA(population, calculateScore) { Speciation = new OriginalNEATSpeciation() }; result.Selection = new TruncationSelection(result, 0.3); var weightMutation = new CompoundOperator(); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(0.02))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(1), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(2), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectFixed(3), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.1125, new NEATMutateWeights(new SelectProportion(0.02), new MutatePerturbLinkWeight(1))); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(1), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(2), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.03, new NEATMutateWeights(new SelectFixed(3), new MutateResetLinkWeight())); weightMutation.Components.Add( 0.01, new NEATMutateWeights(new SelectProportion(0.02), new MutateResetLinkWeight())); weightMutation.Components.FinalizeStructure(); result.ChampMutation = weightMutation; result.AddOperation(0.5, new NEATCrossover()); result.AddOperation(0.494, weightMutation); result.AddOperation(0.0005, new NEATMutateAddNode()); result.AddOperation(0.005, new NEATMutateAddLink()); result.AddOperation(0.0005, new NEATMutateRemoveLink()); result.Operators.FinalizeStructure(); if (population.IsHyperNEAT) { result.CODEC = new HyperNEATCODEC(); } else { result.CODEC = new NEATCODEC(); } return(result); }
/// <summary> /// Creates a new instance of the <see cref="Condition"/> class. /// </summary> /// <param name="expression">The expression of the condition.</param> /// <param name="comparisonOperator">The comparison operator, for use in switch statements only.</param> public Condition(IExpression expression, CompoundOperator comparisonOperator = CompoundOperator.Equal) { Expression = expression; ComparisonOperator = comparisonOperator; }