Changeset 13267
- Timestamp:
- 11/18/15 22:29:17 (9 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs
r12937 r13267 102 102 return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows); 103 103 } 104 105 104 106 105 private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) { 107 var eval = CreateEvalClosure(dataset, rows); 106 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval = 107 (left, right, f) => InterpretRec(left, dataset,rows).Zip(InterpretRec(right, dataset, rows), f); 108 108 109 109 switch (node.Symbol.Name) { 110 case "+": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);111 case "*": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);112 case "-": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);113 case "%": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division110 case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y); 111 case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y); 112 case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y); 113 case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division 114 114 default: { 115 115 double erc; … … 124 124 } 125 125 126 private Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> CreateEvalClosure(IDataset dataset, IEnumerable<int> rows) {127 // capture dataset and rows in scope128 return (a, b, f) => {129 var leftResult = InterpretRec(a, dataset, rows);130 var rightResult = InterpretRec(b, dataset, rows);131 return leftResult.Zip(rightResult, f);132 };133 }134 135 126 #region item cloning and persistence 136 127 // persistence -
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/EvenParityProblem.cs
r13163 r13267 38 38 39 39 #region parameter names 40 41 40 private const string NumberOfBitsParameterName = "NumberOfBits"; 42 43 41 #endregion 44 42 … … 47 45 get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfBitsParameterName]; } 48 46 } 49 50 47 #endregion 51 48 52 49 #region Properties 53 54 50 public int NumberOfBits { 55 51 get { return NumberOfBitsParameter.Value.Value; } 56 52 set { NumberOfBitsParameter.Value.Value = value; } 57 53 } 58 59 60 54 #endregion 61 55 … … 110 104 } 111 105 112 113 106 private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs) { 114 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2=107 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval = 115 108 (left, right, f) => InterpretRec(left, bs).Zip(InterpretRec(right, bs), f); 116 109 117 110 switch (node.Symbol.Name) { 118 case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);119 case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);120 case "NAND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));121 case "NOR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));111 case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); 112 case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); 113 case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y)); 114 case "NOR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y)); 122 115 default: { 123 116 byte bitPos; … … 154 147 155 148 #region events 156 157 149 private void RegisterEventHandlers() { 158 150 NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar(); -
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs
r13238 r13267 40 40 41 41 #region parameter names 42 43 42 private const string NumberOfBitsParameterName = "NumberOfBits"; 44 45 43 #endregion 46 44 47 45 #region Parameter Properties 48 49 46 public IFixedValueParameter<IntValue> NumberOfBitsParameter { 50 47 get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfBitsParameterName]; } 51 48 } 52 53 49 #endregion 54 50 55 51 #region Properties 56 57 52 public int NumberOfBits { 58 53 get { return NumberOfBitsParameter.Value.Value; } 59 54 set { NumberOfBitsParameter.Value.Value = value; } 60 55 } 61 62 63 56 #endregion 64 57 … … 105 98 public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { 106 99 if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero."); 107 if (NumberOfBits > 37) throw new NotSupportedException("Mu pltiplexer does not support problems with number of bits > 37.");100 if (NumberOfBits > 37) throw new NotSupportedException("Multiplexer does not support problems with number of bits > 37."); 108 101 var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits)); 109 102 var addrBits = (int)Math.Log(NumberOfBits, 2); // largest power of two that fits into the number of bits … … 134 127 135 128 private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs, byte addrBits) { 136 Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> eval1=129 Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> unaryEval = 137 130 (child, f) => InterpretRec(child, bs, addrBits).Select(f); 138 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2=131 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval = 139 132 (left, right, f) => InterpretRec(left, bs, addrBits).Zip(InterpretRec(right, bs, addrBits), f); 140 133 141 134 switch (node.Symbol.Name) { 142 case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);143 case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);144 case "NOT": return eval1(node.GetSubtree(0), (x) => !x);145 case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);135 case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); 136 case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); 137 case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x); 138 case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits); 146 139 default: { 147 140 if (node.Symbol.Name[0] == 'a') { … … 199 192 200 193 #region events 201 202 194 private void RegisterEventHandlers() { 203 195 NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar(); -
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Interpreter.cs
r12911 r13267 26 26 27 27 namespace HeuristicLab.Problems.GeneticProgramming.LawnMower { 28 public class Interpreter {28 public static class Interpreter { 29 29 private enum Heading { 30 30 South, … … 45 45 } 46 46 47 48 47 public static bool[,] EvaluateLawnMowerProgram(int length, int width, ISymbolicExpressionTree tree) { 49 50 48 bool[,] lawn = new bool[length, width]; 51 49 var mowerState = new MowerState(); … … 56 54 return lawn; 57 55 } 58 59 56 60 57 private static Tuple<int, int> EvaluateLawnMowerProgram(ISymbolicExpressionTreeNode node, MowerState mowerState, bool[,] lawn, IEnumerable<ISymbolicExpressionTreeNode> adfs) { -
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs
r12911 r13267 51 51 52 52 [StorableConstructor] 53 protected Problem(bool deserializing) 54 : base(deserializing) { 53 protected Problem(bool deserializing) : base(deserializing) { } 54 [StorableHook(HookType.AfterDeserialization)] 55 private void AfterDeserialization() { } 56 57 protected Problem(Problem original, Cloner cloner) : base(original, cloner) { } 58 public override IDeepCloneable Clone(Cloner cloner) { 59 return new Problem(this, cloner); 55 60 } 56 protected Problem(Problem original, Cloner cloner) 57 : base(original, cloner) { 58 } 61 59 62 public Problem() 60 63 : base() { … … 87 90 } 88 91 89 90 [StorableHook(HookType.AfterDeserialization)]91 private void AfterDeserialization() { }92 93 92 public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { 94 93 var length = LawnLengthParameter.Value.Value; … … 105 104 return numberOfMowedCells; 106 105 } 107 108 public override IDeepCloneable Clone(Cloner cloner) {109 return new Problem(this, cloner);110 }111 106 } 112 107 } -
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Solution.cs
r12911 r13267 39 39 [StorableConstructor] 40 40 private Solution(bool deserializing) : base(deserializing) { } 41 [StorableHook(HookType.AfterDeserialization)] 42 private void AfterDeserialization() { } 43 41 44 private Solution(Solution original, Cloner cloner) 42 45 : base(original, cloner) { … … 45 48 this.Tree = cloner.Clone(original.Tree); 46 49 this.Quality = original.Quality; 50 } 51 public override IDeepCloneable Clone(Cloner cloner) { 52 return new Solution(this, cloner); 47 53 } 48 54 … … 54 60 this.Quality = quality; 55 61 } 56 [StorableHook(HookType.AfterDeserialization)]57 private void AfterDeserialization() {58 }59 public override IDeepCloneable Clone(Cloner cloner) {60 return new Solution(this, cloner);61 }62 62 } 63 63 }
Note: See TracChangeset
for help on using the changeset viewer.