Changeset 12915 for trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs
- Timestamp:
- 08/27/15 10:20:09 (9 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs
r12504 r12915 22 22 #endregion 23 23 24 using System; 25 using System.Collections.Generic; 24 using System.Diagnostics.Contracts; 26 25 using System.Linq; 27 26 using HeuristicLab.Common; … … 29 28 using HeuristicLab.Data; 30 29 using HeuristicLab.Encodings.IntegerVectorEncoding; 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;32 30 using HeuristicLab.Optimization; 33 31 using HeuristicLab.Parameters; 34 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 using HeuristicLab.PluginInfrastructure; 36 using HeuristicLab.Problems.ArtificialAnt; 37 using HeuristicLab.Problems.ArtificialAnt.Analyzers; 33 using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt; 38 34 using HeuristicLab.Problems.GrammaticalEvolution.Mappers; 39 35 … … 42 38 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)] 43 39 [StorableClass] 44 public sealed class GEArtificialAntProblem : SingleObjective HeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {40 public sealed class GEArtificialAntProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IStorableContent { 45 41 public string Filename { get; set; } 46 42 47 #region constant for default world (Santa Fe)48 private readonly bool[,] santaFeAntTrail = new bool[,] {49 {false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},50 {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},51 {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false},52 {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},53 {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},54 {false, false, false, true, true, true, true, false, true, true, true, true, true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false},55 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},56 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},57 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},58 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false},59 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},60 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},61 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},62 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},63 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, true, true, true, false, false, false},64 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false},65 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},66 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},67 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},68 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},69 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},70 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},71 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},72 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},73 {false, false, false, true, true, false, false, true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},74 {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},75 {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},76 {false, true, false, false, false, false, false, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},77 {false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},78 {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},79 {false, false, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false },80 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }81 };82 #endregion83 84 43 #region Parameter Properties 85 public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {86 get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }87 }88 public IValueParameter<IntValue> MaxExpressionLengthParameter {89 get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }90 }91 44 public IValueParameter<BoolMatrix> WorldParameter { 92 45 get { return (IValueParameter<BoolMatrix>)Parameters["World"]; } 93 46 } 94 public IValueParameter<IntValue> MaxTimeStepsParameter { 95 get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; } 96 } 97 public IValueParameter<IntMatrix> BoundsParameter { 98 get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; } 47 public IFixedValueParameter<IntValue> MaxTimeStepsParameter { 48 get { return (IFixedValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; } 99 49 } 100 50 public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter { … … 108 58 set { WorldParameter.Value = value; } 109 59 } 110 public IntValue MaxTimeSteps { 111 get { return MaxTimeStepsParameter.Value; } 112 set { MaxTimeStepsParameter.Value = value; } 113 } 114 public IntValue MaxExpressionLength { 115 get { return MaxExpressionLengthParameter.Value; } 116 set { MaxExpressionLengthParameter.Value = value; } 117 } 118 public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar { 119 get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; } 120 } 121 public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers { 122 get { return Operators.OfType<IAntTrailAnalyzer>(); } 123 } 124 public IntMatrix Bounds { 125 get { return BoundsParameter.Value; } 126 set { BoundsParameter.Value = value; } 60 public int MaxTimeSteps { 61 get { return MaxTimeStepsParameter.Value.Value; } 62 set { MaxTimeStepsParameter.Value.Value = value; } 127 63 } 128 64 #endregion … … 131 67 private GEArtificialAntProblem(bool deserializing) : base(deserializing) { } 132 68 [StorableHook(HookType.AfterDeserialization)] 133 private void AfterDeserialization() { 134 RegisterEventHandlers(); 69 private void AfterDeserialization() { } 70 71 public override bool Maximization { 72 get { return true; } 135 73 } 74 75 [Storable] 76 // parameters of the wrapped problem cannot be changed therefore it is not strictly necessary to clone and store it 77 private readonly HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem wrappedAntProblem; 136 78 137 79 private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner) 138 80 : base(original, cloner) { 139 RegisterEventHandlers();81 this.wrappedAntProblem = cloner.Clone(original.wrappedAntProblem); 140 82 } 141 83 … … 145 87 146 88 public GEArtificialAntProblem() 147 : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) { 148 BoolMatrix world = new BoolMatrix(santaFeAntTrail); 149 Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length).", new IntValue(30))); 150 Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar())); 151 Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world)); 152 Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600))); 153 IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } }); 154 Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m)); 89 : base() { 90 wrappedAntProblem = new HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem(); 91 Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", wrappedAntProblem.World)); 92 Parameters.Add(new FixedValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600))); 155 93 Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper())); 156 94 157 Maximization.Value = true; 158 MaximizationParameter.Hidden = true; 159 BestKnownQuality = new DoubleValue(89); 95 Encoding = new IntegerVectorEncoding(30) { Bounds = new IntMatrix(new int[,] { { 0, 100 } }) }; 160 96 161 SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector"; 162 Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree"; 163 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar"; 164 Evaluator.QualityParameter.ActualName = "FoodEaten"; 165 166 InitializeOperators(); 167 RegisterEventHandlers(); 97 BestKnownQuality = wrappedAntProblem.BestKnownQuality; 168 98 } 169 99 170 #region Events 171 protected override void OnSolutionCreatorChanged() { 172 base.OnSolutionCreatorChanged(); 100 public override double Evaluate(Individual individual, IRandom random) { 101 var vector = individual.IntegerVector(); 173 102 174 SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector"; 175 SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged); 103 var bounds = Encoding.Bounds; 104 var len = Encoding.Length; 105 var grammar = wrappedAntProblem.Encoding.Grammar; 106 var mapper = GenotypeToPhenotypeMapperParameter.Value; 176 107 177 ParameterizeAnalyzers(); 178 ParameterizeOperators(); 108 var tree = mapper.Map(random, bounds, len, grammar, vector); 109 110 Interpreter interpreter = new Interpreter(tree, World, MaxTimeSteps); 111 interpreter.Run(); 112 113 return interpreter.FoodEaten; 179 114 } 180 115 181 protected override void OnEvaluatorChanged() { 182 base.OnEvaluatorChanged(); 116 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 117 var bounds = Encoding.Bounds; 118 var len = Encoding.Length; 119 var grammar = wrappedAntProblem.Encoding.Grammar; 120 var mapper = GenotypeToPhenotypeMapperParameter.Value; 183 121 184 Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";185 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";186 Evaluator.QualityParameter.ActualName = "FoodEaten";122 var trees = individuals 123 .Select(ind => mapper.Map(random, bounds, len, grammar, ind.IntegerVector())) 124 .ToArray(); 187 125 188 Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged); 189 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged); 190 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 191 192 ParameterizeAnalyzers(); 193 ParameterizeOperators(); 126 wrappedAntProblem.Analyze(trees, qualities, results, random); 194 127 } 195 196 private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {197 ParameterizeAnalyzers();198 ParameterizeOperators();199 }200 201 private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {202 ParameterizeAnalyzers();203 ParameterizeOperators();204 }205 206 private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {207 ParameterizeAnalyzers();208 ParameterizeOperators();209 }210 211 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {212 ParameterizeAnalyzers();213 ParameterizeOperators();214 }215 #endregion216 217 #region Helpers218 private void RegisterEventHandlers() {219 SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);220 Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);221 Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);222 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);223 }224 225 private void InitializeOperators() {226 Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());227 Operators.Add(new BestAntTrailAnalyzer());228 Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());229 Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());230 ParameterizeAnalyzers();231 ParameterizeOperators();232 }233 234 private void ParameterizeAnalyzers() {235 foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {236 analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;237 analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;238 analyzer.WorldParameter.ActualName = WorldParameter.Name;239 analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;240 }241 foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {242 analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;243 }244 }245 246 private void ParameterizeOperators() {247 var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);248 249 foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {250 op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;251 }252 foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {253 op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;254 op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;255 op.WorldParameter.ActualName = WorldParameter.Name;256 }257 foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {258 op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;259 op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;260 }261 foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {262 op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;263 }264 foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {265 op.BoundsParameter.ActualName = BoundsParameter.Name;266 op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;267 }268 }269 #endregion270 128 } 271 129 }
Note: See TracChangeset
for help on using the changeset viewer.