Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.3/ArtificialAnt/GEArtificialAntEvaluator.cs @ 11300

Last change on this file since 11300 was 10975, checked in by gkronber, 10 years ago

#2109 copied grammatical evolution plugin from feature development branch into the trunk

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 * Author: Sabine Winkler
21 */
22#endregion
23
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.ArtificialAnt;
34using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
35
36namespace HeuristicLab.Problems.GrammaticalEvolution {
37  [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution for grammatical evolution.")]
38  [StorableClass]
39  public class GEArtificialAntEvaluator : SingleSuccessorOperator,
40    ISingleObjectiveEvaluator, ISymbolicExpressionTreeGrammarBasedOperator {
41
42    #region Parameter Properties
43    public ILookupParameter<DoubleValue> QualityParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46    // genotype:
47    public ILookupParameter<IntegerVector> IntegerVectorParameter {
48      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
49    }
50    // phenotype:
51    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
53    }
54    public ILookupParameter<BoolMatrix> WorldParameter {
55      get { return (ILookupParameter<BoolMatrix>)Parameters["World"]; }
56    }
57    public ILookupParameter<IntValue> MaxTimeStepsParameter {
58      get { return (ILookupParameter<IntValue>)Parameters["MaxTimeSteps"]; }
59    }
60    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
61      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters["SymbolicExpressionTreeGrammar"]; }
62    }
63    // genotype-to-phenotype-mapper:
64    public ILookupParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
65      get { return (ILookupParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
66    }
67    public ILookupParameter<IRandom> RandomParameter {
68      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
69    }
70    public ILookupParameter<IntMatrix> BoundsParameter {
71      get { return (ILookupParameter<IntMatrix>)Parameters["Bounds"]; }
72    }
73    public ILookupParameter<IntValue> MaxExpressionLengthParameter {
74      get { return (ILookupParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
75    }
76    #endregion
77
78    [StorableConstructor]
79    protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { }
80    protected GEArtificialAntEvaluator(GEArtificialAntEvaluator original, Cloner cloner) : base(original, cloner) { }
81    public override IDeepCloneable Clone(Cloner cloner) { return new GEArtificialAntEvaluator(this, cloner); }
82    public GEArtificialAntEvaluator()
83      : base() {
84      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the evaluated artificial ant solution."));
85      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The artificial ant solution encoded as an integer vector genome."));
86      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated"));
87      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items."));
88      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated."));
89      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionTreeGrammar", "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
90      Parameters.Add(new LookupParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree)."));
91      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for the genotype creation and the genotype-to-phenotype mapping."));
92
93      Parameters.Add(new LookupParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created."));
94      Parameters.Add(new LookupParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length)."));
95    }
96
97    public sealed override IOperation Apply() {
98      SymbolicExpressionTree tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
99        RandomParameter.ActualValue,
100        BoundsParameter.ActualValue,
101        MaxExpressionLengthParameter.ActualValue.Value,
102        SymbolicExpressionTreeGrammarParameter.ActualValue,
103        IntegerVectorParameter.ActualValue
104      );
105      SymbolicExpressionTreeParameter.ActualValue = tree;
106      BoolMatrix world = WorldParameter.ActualValue;
107      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
108
109      AntInterpreter interpreter = new AntInterpreter();
110      interpreter.MaxTimeSteps = maxTimeSteps.Value;
111      interpreter.World = world;
112      interpreter.Expression = tree;
113      interpreter.Run();
114
115      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
116      return null;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.