Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/ArtificialAnt/GEArtificialAntEvaluator.cs @ 10879

Last change on this file since 10879 was 10328, checked in by sawinkle, 11 years ago

#2109:

  • Added method comments + refactoring.
  • Implemented RandomMapper.
  • Changed InitialTreeLength (genotype length) of Symbolic Regression problem from 25 to 30, equally to the Artificial Ant problem with also 30.
File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.ArtificialAnt;
32using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
33
34namespace HeuristicLab.Problems.GrammaticalEvolution {
35  [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution, implemented in Grammatical Evolution.")]
36  [StorableClass]
37  public class GEArtificialAntEvaluator : SingleSuccessorOperator,
38    ISingleObjectiveEvaluator, ISymbolicExpressionTreeGrammarBasedOperator {
39
40    #region Parameter Properties
41    public ILookupParameter<DoubleValue> QualityParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
43    }
44    // genotype:
45    public ILookupParameter<IntegerVector> IntegerVectorParameter {
46      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
47    }
48    // phenotype:
49    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
50      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
51    }
52    public ILookupParameter<BoolMatrix> WorldParameter {
53      get { return (ILookupParameter<BoolMatrix>)Parameters["World"]; }
54    }
55    public ILookupParameter<IntValue> MaxTimeStepsParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["MaxTimeSteps"]; }
57    }
58    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
59      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionTreeGrammar"]; }
60    }
61    // genotype-to-phenotype-mapper:
62    public ILookupParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
63      get { return (ILookupParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
64    }
65    public ILookupParameter<IRandom> RandomParameter {
66      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
67    }
68    public ILookupParameter<IntMatrix> BoundsParameter {
69      get { return (ILookupParameter<IntMatrix>)Parameters["Bounds"]; }
70    }
71    public ILookupParameter<IntValue> MaxExpressionLengthParameter {
72      get { return (ILookupParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { }
78    protected GEArtificialAntEvaluator(GEArtificialAntEvaluator original, Cloner cloner) : base(original, cloner) { }
79    public override IDeepCloneable Clone(Cloner cloner) { return new GEArtificialAntEvaluator(this, cloner); }
80    public GEArtificialAntEvaluator()
81      : base() {
82      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the evaluated artificial ant solution."));
83      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The artificial ant solution encoded as an integer vector genome."));
84      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated"));
85      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items."));
86      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated."));
87      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionTreeGrammar", "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
88      Parameters.Add(new LookupParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree)."));
89      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for the genotype creation and the genotype-to-phenotype mapping."));
90
91      Parameters.Add(new LookupParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created."));
92      Parameters.Add(new LookupParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length)."));
93    }
94
95    public sealed override IOperation Apply() {
96      SymbolicExpressionTree tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
97        RandomParameter.ActualValue,
98        BoundsParameter.ActualValue,
99        MaxExpressionLengthParameter.ActualValue.Value,
100        SymbolicExpressionTreeGrammarParameter.ActualValue,
101        IntegerVectorParameter.ActualValue
102      );
103      SymbolicExpressionTreeParameter.ActualValue = tree;
104      BoolMatrix world = WorldParameter.ActualValue;
105      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
106
107      AntInterpreter interpreter = new AntInterpreter();
108      interpreter.MaxTimeSteps = maxTimeSteps.Value;
109      interpreter.World = world;
110      interpreter.Expression = tree;
111      interpreter.Run();
112
113      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
114      return null;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.