Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.GrammaticalEvolution/3.3/ArtificialAnt/GEArtificialAntEvaluator.cs @ 18166

Last change on this file since 18166 was 12481, checked in by mkommend, 9 years ago

#2320: Fixed AfterDeserialization of GEArtifialAntEvaluator.

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (ILookupParameter<ISymbolicExpressionTree>)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<ISymbolicExpressionTree>("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    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      // BackwardsCompatibility3.3
100      #region Backwards compatible code, remove with 3.4
101
102      if (Parameters.ContainsKey("SymbolicExpressionTree") &&
103          Parameters["SymbolicExpressionTree"] is ILookupParameter<SymbolicExpressionTree>) {
104        var previousActualName = ((ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]).ActualName;
105        Parameters.Remove("SymbolicExpressionTree");
106        Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated", previousActualName));
107      }
108
109      #endregion
110
111    }
112
113    public sealed override IOperation Apply() {
114      SymbolicExpressionTree tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
115        RandomParameter.ActualValue,
116        BoundsParameter.ActualValue,
117        MaxExpressionLengthParameter.ActualValue.Value,
118        SymbolicExpressionTreeGrammarParameter.ActualValue,
119        IntegerVectorParameter.ActualValue
120      );
121      SymbolicExpressionTreeParameter.ActualValue = tree;
122      BoolMatrix world = WorldParameter.ActualValue;
123      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
124
125      AntInterpreter interpreter = new AntInterpreter();
126      interpreter.MaxTimeSteps = maxTimeSteps.Value;
127      interpreter.World = world;
128      interpreter.Expression = tree;
129      interpreter.Run();
130
131      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
132      return null;
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.