Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.GrammaticalEvolution/3.3/ArtificialAnt/GEArtificialAntEvaluator.cs @ 18242

Last change on this file since 18242 was 14712, checked in by gkronber, 8 years ago

#2520 added GUIDs for (almost) all interface types (probably still too many) also added newlines at end of all files

File size: 7.0 KB
RevLine 
[13368]1#region License Information
[10071]2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10071]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/>.
[10968]19 *
20 * Author: Sabine Winkler
[10071]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 {
[10974]37  [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution for grammatical evolution.")]
[14711]38  [StorableType("01A01AF8-3D9E-4444-A68F-67C87D25DFD9")]
[10071]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:
[12422]51    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
[10071]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    }
[10280]67    public ILookupParameter<IRandom> RandomParameter {
68      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
69    }
[10290]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    }
[10071]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."));
[12422]86      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated"));
[10071]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)."));
[10280]91      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator for the genotype creation and the genotype-to-phenotype mapping."));
[10290]92
93      Parameters.Add(new LookupParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created."));
[10328]94      Parameters.Add(new LookupParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length)."));
[10071]95    }
96
[12422]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>) {
[12481]104        var previousActualName = ((ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]).ActualName;
[12422]105        Parameters.Remove("SymbolicExpressionTree");
[12481]106        Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated", previousActualName));
[12422]107      }
108
109      #endregion
110
111    }
112
[10071]113    public sealed override IOperation Apply() {
[10280]114      SymbolicExpressionTree tree = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
115        RandomParameter.ActualValue,
[10290]116        BoundsParameter.ActualValue,
117        MaxExpressionLengthParameter.ActualValue.Value,
[10071]118        SymbolicExpressionTreeGrammarParameter.ActualValue,
119        IntegerVectorParameter.ActualValue
120      );
[10280]121      SymbolicExpressionTreeParameter.ActualValue = tree;
[10071]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;
[10280]128      interpreter.Expression = tree;
[10071]129      interpreter.Run();
130
131      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
132      return null;
133    }
134  }
[14712]135}
Note: See TracBrowser for help on using the repository browser.