Changeset 13280 for stable/HeuristicLab.Problems.GeneticProgramming
- Timestamp:
- 11/19/15 11:42:02 (9 years ago)
- Location:
- stable
- Files:
-
- 12 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 12921,12936-12938,12940,12947,13055-13058,13163,13267,13269
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Interpreter.cs
r12911 r13280 32 32 public int FoodEaten { get; private set; } 33 33 public BoolMatrix World { get; private set; } 34 35 34 public ISymbolicExpressionTree Expression { get; private set; } 36 35 36 public int ElapsedTime { get; set; } 37 37 38 public int ElapsedTime { get; set; }39 38 private int currentDirection; 40 39 private int currentAntLocationRow; -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Problem.cs
r12911 r13280 22 22 using System; 23 23 using System.Diagnostics.Contracts; 24 using System.Drawing.Text;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 38 37 [StorableClass] 39 38 public sealed class Problem : SymbolicExpressionTreeProblem, IStorableContent { 40 public string Filename { get; set; }41 39 42 40 #region constant for default world (Santa Fe) … … 104 102 } 105 103 104 #region item cloning and persistence 105 // persistence 106 [StorableConstructor] 107 private Problem(bool deserializing) : base(deserializing) { } 108 [StorableHook(HookType.AfterDeserialization)] 109 private void AfterDeserialization() { } 110 111 // cloning 112 private Problem(Problem original, Cloner cloner) : base(original, cloner) { } 113 public override IDeepCloneable Clone(Cloner cloner) { 114 return new Problem(this, cloner); 115 } 116 #endregion 117 106 118 public Problem() 107 119 : base() { … … 114 126 g.AddSymbols(new string[] { "IfFoodAhead", "Prog2" }, 2, 2); 115 127 g.AddSymbols(new string[] { "Prog3" }, 3, 3); 116 g.AddTerminalSymbols(new string[] { " Left", "Right", "Move" });128 g.AddTerminalSymbols(new string[] { "Move", "Left", "Right" }); 117 129 base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10); 118 130 } … … 137 149 } 138 150 139 // persistence140 [StorableConstructor]141 private Problem(bool deserializing) : base(deserializing) { }142 [StorableHook(HookType.AfterDeserialization)]143 private void AfterDeserialization() {144 }145 146 // cloning147 private Problem(Problem original, Cloner cloner)148 : base(original, cloner) {149 }150 public override IDeepCloneable Clone(Cloner cloner) {151 return new Problem(this, cloner);152 }153 154 151 #region helpers 155 152 private bool[,] ToBoolMatrix(char[][] ch) { -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Trail.cs
r12911 r13280 20 20 #endregion 21 21 22 using System;23 22 using System.Drawing; 24 23 using HeuristicLab.Common; … … 54 53 } 55 54 55 #region item cloning and persistence 56 56 [StorableConstructor] 57 57 private Solution(bool deserializing) : base(deserializing) { } … … 70 70 return new Solution(this, cloner); 71 71 } 72 #endregion 72 73 } 73 74 } -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs
r12937 r13280 39 39 40 40 #region parameter names 41 42 41 private const string ProblemDataParameterName = "ProblemData"; 43 44 42 #endregion 45 46 public event EventHandler ProblemDataChanged;47 43 48 44 #region Parameter Properties … … 52 48 get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; } 53 49 } 54 55 50 #endregion 56 51 57 52 #region Properties 58 59 53 public IRegressionProblemData ProblemData { 60 54 get { return ProblemDataParameter.Value; } … … 62 56 } 63 57 IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { get { return ProblemData; } } 58 #endregion 64 59 65 66 #endregion 60 public event EventHandler ProblemDataChanged; 67 61 68 62 public override bool Maximization { 69 63 get { return true; } 70 64 } 65 66 #region item cloning and persistence 67 // persistence 68 [StorableConstructor] 69 private Problem(bool deserializing) : base(deserializing) { } 70 [StorableHook(HookType.AfterDeserialization)] 71 private void AfterDeserialization() { 72 RegisterEventHandlers(); 73 } 74 75 // cloning 76 private Problem(Problem original, Cloner cloner) 77 : base(original, cloner) { 78 RegisterEventHandlers(); 79 } 80 public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); } 81 #endregion 71 82 72 83 public Problem() … … 103 114 } 104 115 105 106 116 private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) { 107 var eval = CreateEvalClosure(dataset, rows); 117 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval = 118 (left, right, f) => InterpretRec(left, dataset, rows).Zip(InterpretRec(right, dataset, rows), f); 108 119 109 120 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 division121 case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y); 122 case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y); 123 case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y); 124 case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division 114 125 default: { 115 126 double erc; … … 124 135 } 125 136 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 #region item cloning and persistence136 // persistence137 [StorableConstructor]138 private Problem(bool deserializing) : base(deserializing) { }139 140 [StorableHook(HookType.AfterDeserialization)]141 private void AfterDeserialization() {142 RegisterEventHandlers();143 }144 145 // cloning146 private Problem(Problem original, Cloner cloner)147 : base(original, cloner) {148 RegisterEventHandlers();149 }150 public override IDeepCloneable Clone(Cloner cloner) {151 return new Problem(this, cloner);152 }153 #endregion154 137 155 138 #region events 156 157 139 private void RegisterEventHandlers() { 158 140 ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged); … … 197 179 Encoding.Grammar = g; 198 180 } 199 200 181 #endregion 201 182 -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/EvenParityProblem.cs
r12938 r13280 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 … … 63 57 get { return true; } 64 58 } 59 60 #region item cloning and persistence 61 // persistence 62 [StorableConstructor] 63 private EvenParityProblem(bool deserializing) : base(deserializing) { } 64 [StorableHook(HookType.AfterDeserialization)] 65 private void AfterDeserialization() { 66 RegisterEventHandlers(); 67 } 68 69 // cloning 70 private EvenParityProblem(EvenParityProblem original, Cloner cloner) 71 : base(original, cloner) { 72 RegisterEventHandlers(); 73 } 74 public override IDeepCloneable Clone(Cloner cloner) { 75 return new EvenParityProblem(this, cloner); 76 } 77 #endregion 65 78 66 79 public EvenParityProblem() … … 84 97 85 98 Encoding.Grammar = g; 99 100 BestKnownQuality = Math.Pow(2, NumberOfBits); // this is a benchmark problem (the best achievable quality is known for a given number of bits) 86 101 } 87 102 … … 108 123 } 109 124 110 111 125 private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs) { 112 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2=126 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval = 113 127 (left, right, f) => InterpretRec(left, bs).Zip(InterpretRec(right, bs), f); 114 128 115 129 switch (node.Symbol.Name) { 116 case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);117 case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);118 case "NAND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));119 case "NOR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));130 case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); 131 case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); 132 case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y)); 133 case "NOR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y)); 120 134 default: { 121 135 byte bitPos; … … 131 145 } 132 146 133 #region item cloning and persistence134 // persistence135 [StorableConstructor]136 private EvenParityProblem(bool deserializing) : base(deserializing) { }137 138 [StorableHook(HookType.AfterDeserialization)]139 private void AfterDeserialization() {140 RegisterEventHandlers();141 }142 143 // cloning144 private EvenParityProblem(EvenParityProblem original, Cloner cloner)145 : base(original, cloner) {146 RegisterEventHandlers();147 }148 public override IDeepCloneable Clone(Cloner cloner) {149 return new EvenParityProblem(this, cloner);150 }151 #endregion152 153 147 #region events 154 155 148 private void RegisterEventHandlers() { 156 149 NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar(); -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs
r12938 r13280 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 … … 66 59 get { return true; } 67 60 } 61 62 #region item cloning and persistence 63 // persistence 64 [StorableConstructor] 65 private MultiplexerProblem(bool deserializing) : base(deserializing) { } 66 [StorableHook(HookType.AfterDeserialization)] 67 private void AfterDeserialization() { 68 RegisterEventHandlers(); 69 } 70 71 // cloning 72 private MultiplexerProblem(MultiplexerProblem original, Cloner cloner) 73 : base(original, cloner) { 74 RegisterEventHandlers(); 75 } 76 public override IDeepCloneable Clone(Cloner cloner) { 77 return new MultiplexerProblem(this, cloner); 78 } 79 #endregion 80 68 81 69 82 public MultiplexerProblem() … … 98 111 99 112 Encoding.Grammar = g; 113 114 BestKnownQuality = Math.Pow(2, NumberOfBits); // this is a benchmark problem (the best achievable quality is known for a given number of bits) 100 115 } 101 116 … … 103 118 public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { 104 119 if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero."); 105 if (NumberOfBits > 40) throw new NotSupportedException("Mupltiplexer does not support problems with number of bits > 37.");120 if (NumberOfBits > 37) throw new NotSupportedException("Multiplexer does not support problems with number of bits > 37."); 106 121 var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits)); 107 122 var addrBits = (int)Math.Log(NumberOfBits, 2); // largest power of two that fits into the number of bits … … 132 147 133 148 private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs, byte addrBits) { 134 Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> eval1=149 Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> unaryEval = 135 150 (child, f) => InterpretRec(child, bs, addrBits).Select(f); 136 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2=151 Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval = 137 152 (left, right, f) => InterpretRec(left, bs, addrBits).Zip(InterpretRec(right, bs, addrBits), f); 138 153 139 154 switch (node.Symbol.Name) { 140 case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);141 case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);142 case "NOT": return eval1(node.GetSubtree(0), (x) => !x);155 case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y); 156 case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y); 157 case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x); 143 158 case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits); 144 159 default: { … … 176 191 } 177 192 178 #region item cloning and persistence179 // persistence180 [StorableConstructor]181 private MultiplexerProblem(bool deserializing) : base(deserializing) { }182 183 [StorableHook(HookType.AfterDeserialization)]184 private void AfterDeserialization() {185 RegisterEventHandlers();186 }187 188 // cloning189 private MultiplexerProblem(MultiplexerProblem original, Cloner cloner)190 : base(original, cloner) {191 RegisterEventHandlers();192 }193 public override IDeepCloneable Clone(Cloner cloner) {194 return new MultiplexerProblem(this, cloner);195 }196 #endregion197 198 193 #region events 199 200 194 private void RegisterEventHandlers() { 201 195 NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar(); -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/HeuristicLab.Problems.GeneticProgramming-3.3.csproj
r12911 r13280 109 109 <Compile Include="ArtificialAnt\Problem.cs" /> 110 110 <Compile Include="ArtificialAnt\Trail.cs" /> 111 <Compile Include="BasicSymbolicRegression\Problem.cs" /> 112 <Compile Include="Boolean\MultiplexerProblem.cs" /> 113 <Compile Include="Boolean\EvenParityProblem.cs" /> 111 114 <Compile Include="LawnMower\Interpreter.cs" /> 112 115 <Compile Include="LawnMower\Problem.cs" /> … … 174 177 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project> 175 178 <Name>HeuristicLab.PluginInfrastructure-3.3</Name> 179 <Private>False</Private> 180 </ProjectReference> 181 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj"> 182 <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project> 183 <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name> 184 <Private>False</Private> 185 </ProjectReference> 186 <ProjectReference Include="..\..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj"> 187 <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project> 188 <Name>HeuristicLab.Problems.Instances-3.3</Name> 176 189 <Private>False</Private> 177 190 </ProjectReference> -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Interpreter.cs
r12911 r13280 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) { -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs
r12911 r13280 50 50 } 51 51 52 #region item cloning and persistence 52 53 [StorableConstructor] 53 protected Problem(bool deserializing) 54 : base(deserializing) { 54 protected Problem(bool deserializing) : base(deserializing) { } 55 [StorableHook(HookType.AfterDeserialization)] 56 private void AfterDeserialization() { } 57 58 protected Problem(Problem original, Cloner cloner) : base(original, cloner) { } 59 public override IDeepCloneable Clone(Cloner cloner) { 60 return new Problem(this, cloner); 55 61 } 56 protected Problem(Problem original, Cloner cloner) 57 : base(original, cloner) { 58 } 62 #endregion 63 59 64 public Problem() 60 65 : base() { … … 87 92 } 88 93 89 90 [StorableHook(HookType.AfterDeserialization)]91 private void AfterDeserialization() { }92 93 94 public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) { 94 95 var length = LawnLengthParameter.Value.Value; … … 105 106 return numberOfMowedCells; 106 107 } 107 108 public override IDeepCloneable Clone(Cloner cloner) {109 return new Problem(this, cloner);110 }111 108 } 112 109 } -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Solution.cs
r12911 r13280 37 37 public double Quality { get; private set; } 38 38 39 #region item cloning and persistence 39 40 [StorableConstructor] 40 41 private Solution(bool deserializing) : base(deserializing) { } 42 [StorableHook(HookType.AfterDeserialization)] 43 private void AfterDeserialization() { } 44 41 45 private Solution(Solution original, Cloner cloner) 42 46 : base(original, cloner) { … … 46 50 this.Quality = original.Quality; 47 51 } 52 public override IDeepCloneable Clone(Cloner cloner) { 53 return new Solution(this, cloner); 54 } 55 #endregion 48 56 49 57 public Solution(ISymbolicExpressionTree tree, int length, int width, double quality) … … 54 62 this.Quality = quality; 55 63 } 56 [StorableHook(HookType.AfterDeserialization)]57 private void AfterDeserialization() {58 }59 public override IDeepCloneable Clone(Cloner cloner) {60 return new Solution(this, cloner);61 }62 64 } 63 65 } -
stable/HeuristicLab.Problems.GeneticProgramming/3.3/Plugin.cs.frame
r13279 r13280 37 37 [PluginDependency("HeuristicLab.Parameters", "3.3")] 38 38 [PluginDependency("HeuristicLab.Persistence", "3.3")] 39 [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")] 40 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")] 39 41 [PluginDependency("HeuristicLab.Random", "3.3")] 40 42 public class HeuristicLabProblemsGeneticProgrammingPlugin : PluginBase {
Note: See TracChangeset
for help on using the changeset viewer.