Changeset 10068 for branches/GrammaticalEvolution
- Timestamp:
- 10/20/13 18:44:04 (11 years ago)
- Location:
- branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntEvaluator.cs
r10039 r10068 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Encodings.IntegerVectorEncoding; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Problems.ArtificialAnt;27 27 using HeuristicLab.Operators; 28 28 using HeuristicLab.Optimization; 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab. Encodings.IntegerVectorEncoding;31 using HeuristicLab.Problems.ArtificialAnt; 32 32 using HeuristicLab.Problems.GrammaticalEvolution.Mappers; 33 33 … … 38 38 ISingleObjectiveEvaluator, ISymbolicExpressionTreeGrammarBasedOperator { 39 39 40 40 #region Parameter Properties 41 41 public ILookupParameter<DoubleValue> QualityParameter { 42 42 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 43 43 } 44 45 44 // genotype: 45 public ILookupParameter<IntegerVector> IntegerVectorParameter { 46 46 get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; } 47 47 } 48 48 // phenotype: 49 49 public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter { 50 50 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } … … 56 56 get { return (ILookupParameter<IntValue>)Parameters["MaxTimeSteps"]; } 57 57 } 58 58 public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { 59 59 get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionTreeGrammar"]; } 60 60 } 61 62 61 // genotype-to-phenotype-mapper: 62 public ILookupParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter { 63 63 get { return (ILookupParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; } 64 64 } 65 66 65 #endregion 66 67 67 [StorableConstructor] 68 68 protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { } … … 81 81 82 82 public sealed override IOperation Apply() { 83 SymbolicExpressionTree expression = GenotypeToPhenotypeMapperParameter.ActualValue.Map 83 SymbolicExpressionTree expression = GenotypeToPhenotypeMapperParameter.ActualValue.Map( 84 84 SymbolicExpressionTreeGrammarParameter.ActualValue, 85 IntegerVectorParameter.ActualValue 85 IntegerVectorParameter.ActualValue 86 86 ); 87 87 SymbolicExpressionTreeParameter.ActualValue = expression; 88 88 BoolMatrix world = WorldParameter.ActualValue; 89 89 IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue; 90 90 91 91 AntInterpreter interpreter = new AntInterpreter(); 92 interpreter.MaxTimeSteps 93 interpreter.World 94 interpreter.Expression 92 interpreter.MaxTimeSteps = maxTimeSteps.Value; 93 interpreter.World = world; 94 interpreter.Expression = expression; 95 95 interpreter.Run(); 96 96 -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntProblem.cs
r10039 r10068 42 42 public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent { 43 43 public string Filename { get; set; } 44 44 45 45 #region constant for default world (Santa Fe) 46 46 private readonly bool[,] santaFeAntTrail = new bool[,] { … … 87 87 get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; } 88 88 } 89 public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {90 get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }91 }92 public IValueParameter<IntValue> MaxFunctionArgumentsParameter {93 get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }94 }95 89 public IValueParameter<BoolMatrix> WorldParameter { 96 90 get { return (IValueParameter<BoolMatrix>)Parameters["World"]; } … … 119 113 get { return MaxExpressionLengthParameter.Value; } 120 114 set { MaxExpressionLengthParameter.Value = value; } 121 }122 public IntValue MaxFunctionDefinitions {123 get { return MaxFunctionDefinitionsParameter.Value; }124 set { MaxFunctionDefinitionsParameter.Value = value; }125 }126 public IntValue MaxFunctionArguments {127 get { return MaxFunctionArgumentsParameter.Value; }128 set { MaxFunctionArgumentsParameter.Value = value; }129 115 } 130 116 public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar { … … 164 150 RegisterEventHandlers(); 165 151 } 152 166 153 public override IDeepCloneable Clone(Cloner cloner) { 167 154 return new GEArtificialAntProblem(this, cloner); 168 155 } 156 169 157 public GEArtificialAntProblem() 170 158 : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) { 171 159 BoolMatrix world = new BoolMatrix(santaFeAntTrail); 172 160 Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(30))); 173 Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));174 Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));175 161 Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar())); 176 162 Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world)); 177 163 Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600))); 178 IntMatrix m = new IntMatrix(new int[,] {{0,100}});164 IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } }); 179 165 Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m)); 180 166 Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper())); 181 167 182 168 Maximization.Value = true; 183 169 MaximizationParameter.Hidden = true; 184 170 BestKnownQuality = new DoubleValue(89); 185 171 186 172 SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector"; 187 173 Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree"; 188 174 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar"; 189 175 Evaluator.QualityParameter.ActualName = "FoodEaten"; 190 176 191 177 InitializeOperators(); 192 178 RegisterEventHandlers(); … … 196 182 protected override void OnSolutionCreatorChanged() { 197 183 base.OnSolutionCreatorChanged(); 198 184 199 185 SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector"; 200 186 SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged); 201 202 ParameterizeAnalyzers(); 203 ParameterizeOperators(); 204 } 205 187 188 ParameterizeAnalyzers(); 189 ParameterizeOperators(); 190 } 191 206 192 protected override void OnEvaluatorChanged() { 207 193 base.OnEvaluatorChanged(); 208 194 209 195 Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree"; 210 196 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar"; 211 197 Evaluator.QualityParameter.ActualName = "FoodEaten"; 212 198 213 199 Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged); 214 200 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged); 215 201 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 216 217 ParameterizeAnalyzers(); 218 ParameterizeOperators(); 219 } 220 202 203 ParameterizeAnalyzers(); 204 ParameterizeOperators(); 205 } 206 221 207 private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) { 222 208 ParameterizeAnalyzers(); 223 209 ParameterizeOperators(); 224 210 } 225 211 226 212 private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) { 227 213 ParameterizeAnalyzers(); 228 214 ParameterizeOperators(); 229 215 } 230 216 231 217 private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) { 232 218 ParameterizeAnalyzers(); 233 219 ParameterizeOperators(); 234 220 } 235 221 236 222 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 237 223 ParameterizeAnalyzers(); … … 242 228 #region Helpers 243 229 private void RegisterEventHandlers() { 244 SolutionCreator.IntegerVectorParameter.ActualNameChanged 245 Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged 230 SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged); 231 Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged); 246 232 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged); 247 233 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 248 MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);249 MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);250 MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);251 MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);252 }253 254 private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {255 ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;256 ParameterizeOperators();257 ParameterizeAnalyzers();258 }259 private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {260 ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;261 ParameterizeOperators();262 ParameterizeAnalyzers();263 234 } 264 235 -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/BreathFirstMapper.cs
r10039 r10068 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 34 33 [StorableClass] 35 34 public class BreathFirstMapper : GenotypeToPhenotypeMapper { 36 35 37 36 [StorableConstructor] 38 37 protected BreathFirstMapper(bool deserializing) : base(deserializing) { } … … 43 42 return new BreathFirstMapper(this, cloner); 44 43 } 45 46 44 45 47 46 /// <summary> 48 47 /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree). … … 54 53 public override SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar, 55 54 IntegerVector genotype) { 56 55 57 56 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 58 var rootNode 57 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 59 58 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 60 59 rootNode.AddSubtree(startNode); 61 60 tree.Root = rootNode; 62 61 63 62 // TODO 64 63 65 64 return tree; 66 65 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
r10039 r10068 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 34 33 [StorableClass] 35 34 public class DepthFirstMapper : GenotypeToPhenotypeMapper { 36 35 37 36 [StorableConstructor] 38 37 protected DepthFirstMapper(bool deserializing) : base(deserializing) { } … … 43 42 return new DepthFirstMapper(this, cloner); 44 43 } 45 46 44 45 47 46 /// <summary> 48 47 /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree). … … 54 53 public override SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar, 55 54 IntegerVector genotype) { 56 55 57 56 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 58 var rootNode 57 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 59 58 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 60 59 rootNode.AddSubtree(startNode); 61 60 tree.Root = rootNode; 62 63 int genotypeIndex 61 62 int genotypeIndex = 0; 64 63 int currSubtreeCount = 1; 65 64 66 65 MapDepthFirstRecursively(startNode, genotype, 67 66 grammar, genotype.Length, 68 67 ref genotypeIndex, ref currSubtreeCount); 69 68 70 69 return tree; 71 70 } 72 73 71 72 74 73 /// <summary> 75 74 /// Genotype-to-Phenotype mapper (recursive depth-first approach). … … 93 92 ref int genotypeIndex, 94 93 ref int currSubtreeCount) { 95 96 94 if (currSubtreeCount < maxSubtreeCount) { 95 97 96 var newNode = GetNewChildNode(currentNode, genotype, grammar, genotypeIndex); 98 97 99 98 if ((currSubtreeCount + newNode.Symbol.MaximumArity) > maxSubtreeCount) { 100 99 // TODO: maybe check, if there is any node, which fits in the tree yet … … 102 101 } else { 103 102 currentNode.AddSubtree(newNode); 104 105 106 107 108 MapDepthFirstRecursively(newNode, genotype, 109 110 111 103 genotypeIndex++; 104 currSubtreeCount += newNode.Symbol.MaximumArity; 105 106 while (newNode.Symbol.MaximumArity > newNode.SubtreeCount) { 107 MapDepthFirstRecursively(newNode, genotype, 108 grammar, maxSubtreeCount, 109 ref genotypeIndex, ref currSubtreeCount); 110 } 112 111 } 113 112 114 113 } else { 115 114 while (currentNode.Symbol.MaximumArity > currentNode.SubtreeCount) { -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/GenotypeToPhenotypeMapper.cs
r10039 r10068 34 34 /// </summary> 35 35 public abstract class GenotypeToPhenotypeMapper : IntegerVectorOperator, IGenotypeToPhenotypeMapper { 36 36 37 37 [StorableConstructor] 38 38 protected GenotypeToPhenotypeMapper(bool deserializing) : base(deserializing) { } 39 39 protected GenotypeToPhenotypeMapper(GenotypeToPhenotypeMapper original, Cloner cloner) : base(original, cloner) { } 40 40 protected GenotypeToPhenotypeMapper() : base() { } 41 41 42 42 public abstract SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar, 43 43 IntegerVector genotype); 44 44 45 45 /// <summary> 46 46 /// Randomly returns a terminal node for the given <paramref name="parentNode"/>. … … 51 51 /// <returns>randomly chosen terminal node with arity 0</returns> 52 52 protected ISymbolicExpressionTreeNode GetRandomTerminalNode(ISymbolicExpressionTreeNode parentNode, 53 ISymbolicExpressionGrammar 54 var possibleSymbolsList = from 55 where 56 where 53 ISymbolicExpressionGrammar grammar) { 54 var possibleSymbolsList = from s in grammar.GetAllowedChildSymbols(parentNode.Symbol) 55 where s.MaximumArity == 0 56 where s.MinimumArity == 0 57 57 select s; 58 58 // TODO: Check, if symbol list is empty (no terminal nodes found) - what should happen? 59 59 return possibleSymbolsList.SelectRandom(new MersenneTwister()).CreateTreeNode(); 60 60 } 61 62 61 62 63 63 /// <summary> 64 64 /// Returns a randomly chosen child node for the given <paramref name="parentNode"/>. … … 73 73 ISymbolicExpressionGrammar grammar, 74 74 int genotypeIndex) { 75 75 76 76 IEnumerable<ISymbol> symbolList = grammar.GetAllowedChildSymbols(parentNode.Symbol); 77 77 int prodRuleCount = symbolList.Count(); 78 78 int prodRuleIndex = genotype[genotypeIndex % genotype.Length] % prodRuleCount; 79 79 80 80 return symbolList.ElementAt(prodRuleIndex).CreateTreeNode(); 81 81 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/IGenotypeToPhenotypeMapper.cs
r10039 r10068 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Encodings.IntegerVectorEncoding; 24 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/PIGEMapper.cs
r10039 r10068 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 29 28 namespace HeuristicLab.Problems.GrammaticalEvolution { 30 29 /// <summary> 31 /// P IGEMapper30 /// Position Independent (PI) Grammatical Evolution Mapper 32 31 /// </summary> 33 32 [Item("PIGEMapper", "")] 34 33 [StorableClass] 35 34 public class PIGEMapper : GenotypeToPhenotypeMapper { 36 35 37 36 [StorableConstructor] 38 37 protected PIGEMapper(bool deserializing) : base(deserializing) { } … … 43 42 return new PIGEMapper(this, cloner); 44 43 } 45 46 44 45 47 46 /// <summary> 48 47 /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree). … … 54 53 public override SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar, 55 54 IntegerVector genotype) { 56 55 57 56 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 58 var rootNode 57 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 59 58 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 60 59 rootNode.AddSubtree(startNode); 61 60 tree.Root = rootNode; 62 61 63 62 // TODO 64 63 65 64 return tree; 66 65 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/RandomMapper.cs
r10039 r10068 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 34 33 [StorableClass] 35 34 public class RandomMapper : GenotypeToPhenotypeMapper { 36 35 37 36 [StorableConstructor] 38 37 protected RandomMapper(bool deserializing) : base(deserializing) { } … … 43 42 return new RandomMapper(this, cloner); 44 43 } 45 46 44 45 47 46 /// <summary> 48 47 /// Maps a genotype (an integer vector) to a phenotype (a symbolic expression tree). … … 54 53 public override SymbolicExpressionTree Map(ISymbolicExpressionGrammar grammar, 55 54 IntegerVector genotype) { 56 55 57 56 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 58 var rootNode 57 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 59 58 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 60 59 rootNode.AddSubtree(startNode); 61 60 tree.Root = rootNode; 62 61 63 62 // TODO 64 63 65 64 return tree; 66 65 }
Note: See TracChangeset
for help on using the changeset viewer.