Free cookie consent management tool by TermsFeed Policy Generator

# Changeset 13267

Ignore:
Timestamp:
11/18/15 22:29:17 (8 years ago)
Message:

#2472: Minor code improvements in HL.Problems.GP (typos, code organization, code unification).

Location:
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3
Files:
6 edited

Unmodified
Removed
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs

 r12937 return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows); } private IEnumerable InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable rows) { var eval = CreateEvalClosure(dataset, rows); Func, IEnumerable> binaryEval = (left, right, f) => InterpretRec(left, dataset,rows).Zip(InterpretRec(right, dataset, rows), f); switch (node.Symbol.Name) { case "+": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y); case "*": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y); case "-": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y); case "%": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y); case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y); case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y); case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division default: { double erc; } private Func, IEnumerable> CreateEvalClosure(IDataset dataset, IEnumerable rows) { // capture dataset and rows in scope return (a, b, f) => { var leftResult = InterpretRec(a, dataset, rows); var rightResult = InterpretRec(b, dataset, rows); return leftResult.Zip(rightResult, f); }; } #region item cloning and persistence // persistence
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/EvenParityProblem.cs

 r13163 #region parameter names private const string NumberOfBitsParameterName = "NumberOfBits"; #endregion get { return (IFixedValueParameter)Parameters[NumberOfBitsParameterName]; } } #endregion #region Properties public int NumberOfBits { get { return NumberOfBitsParameter.Value.Value; } set { NumberOfBitsParameter.Value.Value = value; } } #endregion } private static IEnumerable InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable bs) { Func, IEnumerable> eval2 = Func, IEnumerable> binaryEval = (left, right, f) => InterpretRec(left, bs).Zip(InterpretRec(right, bs), f); switch (node.Symbol.Name) { case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); case "NAND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y)); case "NOR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y)); case "AND":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); case "OR":   return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y)); case "NOR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y)); default: { byte bitPos; #region events private void RegisterEventHandlers() { NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs

 r13238 #region parameter names private const string NumberOfBitsParameterName = "NumberOfBits"; #endregion #region Parameter Properties public IFixedValueParameter NumberOfBitsParameter { get { return (IFixedValueParameter)Parameters[NumberOfBitsParameterName]; } } #endregion #region Properties public int NumberOfBits { get { return NumberOfBitsParameter.Value.Value; } set { NumberOfBitsParameter.Value.Value = value; } } #endregion public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero."); if (NumberOfBits > 37) throw new NotSupportedException("Mupltiplexer does not support problems with number of bits > 37."); if (NumberOfBits > 37) throw new NotSupportedException("Multiplexer does not support problems with number of bits > 37."); var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits)); var addrBits = (int)Math.Log(NumberOfBits, 2); // largest power of two that fits into the number of bits private static IEnumerable InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable bs, byte addrBits) { Func, IEnumerable> eval1 = Func, IEnumerable> unaryEval = (child, f) => InterpretRec(child, bs, addrBits).Select(f); Func, IEnumerable> eval2 = Func, IEnumerable> binaryEval = (left, right, f) => InterpretRec(left, bs, addrBits).Zip(InterpretRec(right, bs, addrBits), f); switch (node.Symbol.Name) { case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); case "NOT": return eval1(node.GetSubtree(0), (x) => !x); case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits); case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); case "OR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x); case "IF":  return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits); default: { if (node.Symbol.Name[0] == 'a') { #region events private void RegisterEventHandlers() { NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Interpreter.cs

 r12911 namespace HeuristicLab.Problems.GeneticProgramming.LawnMower { public class Interpreter { public static class Interpreter { private enum Heading { South, } public static bool[,] EvaluateLawnMowerProgram(int length, int width, ISymbolicExpressionTree tree) { bool[,] lawn = new bool[length, width]; var mowerState = new MowerState(); return lawn; } private static Tuple EvaluateLawnMowerProgram(ISymbolicExpressionTreeNode node, MowerState mowerState, bool[,] lawn, IEnumerable adfs) {
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs

 r12911 [StorableConstructor] protected Problem(bool deserializing) : base(deserializing) { protected Problem(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } protected Problem(Problem original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); } protected Problem(Problem original, Cloner cloner) : base(original, cloner) { } public Problem() : base() { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { var length = LawnLengthParameter.Value.Value; return numberOfMowedCells; } public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); } } }
• ## trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Solution.cs

 r12911 [StorableConstructor] private Solution(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } private Solution(Solution original, Cloner cloner) : base(original, cloner) { this.Tree = cloner.Clone(original.Tree); this.Quality = original.Quality; } public override IDeepCloneable Clone(Cloner cloner) { return new Solution(this, cloner); } this.Quality = quality; } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } public override IDeepCloneable Clone(Cloner cloner) { return new Solution(this, cloner); } } }
Note: See TracChangeset for help on using the changeset viewer.