Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/GEArtificialAntEvaluator.cs @ 10068

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

#2109:

  • Removed the parameters MaxFunctionDefinitions and MaxFunctionArguments from GEArtificialAntProblem.cs, because automatically defined functions (adf) won't be supported by the Grammatical Evolution implementation of the Artificial Ant problem.
  • Switched from SharpDevelop to Visual Studio 2012 and installed 'Productivity Power Tools 2012'. This extension includes the options 'Format Document on save' and 'Remove and Sort Usings on save', so that some usings were deleted, sorted and the formating changed slightly. Furthermore 'Visual Studio 2010 text editor settings.vssettings' were included.
  • Added new folders ArtificialAnt and Symbolic to separate the files for the ArtificialAnt problem and the Symbolic Regression problem (single objective).
File size: 5.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    #endregion
66
67    [StorableConstructor]
68    protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { }
69    protected GEArtificialAntEvaluator(GEArtificialAntEvaluator original, Cloner cloner) : base(original, cloner) { }
70    public override IDeepCloneable Clone(Cloner cloner) { return new GEArtificialAntEvaluator(this, cloner); }
71    public GEArtificialAntEvaluator()
72      : base() {
73      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the evaluated artificial ant solution."));
74      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The artificial ant solution encoded as an integer vector genome."));
75      Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated"));
76      Parameters.Add(new LookupParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items."));
77      Parameters.Add(new LookupParameter<IntValue>("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated."));
78      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>("SymbolicExpressionTreeGrammar", "The tree grammar that defines the correct syntax of symbolic expression trees that should be created."));
79      Parameters.Add(new LookupParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree)."));
80    }
81
82    public sealed override IOperation Apply() {
83      SymbolicExpressionTree expression = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
84        SymbolicExpressionTreeGrammarParameter.ActualValue,
85        IntegerVectorParameter.ActualValue
86      );
87      SymbolicExpressionTreeParameter.ActualValue = expression;
88      BoolMatrix world = WorldParameter.ActualValue;
89      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
90
91      AntInterpreter interpreter = new AntInterpreter();
92      interpreter.MaxTimeSteps = maxTimeSteps.Value;
93      interpreter.World = world;
94      interpreter.Expression = expression;
95      interpreter.Run();
96
97      QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten);
98      return null;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.