[10071] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.ArtificialAnt;
|
---|
| 32 | using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
|
---|
| 33 |
|
---|
| 34 | namespace 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 | } |
---|