#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ArtificialAnt; using HeuristicLab.Problems.GrammaticalEvolution.Mappers; namespace HeuristicLab.Problems.GrammaticalEvolution { [Item("GEArtificialAntEvaluator", "Evaluates an artificial ant solution, implemented in Grammatical Evolution.")] [StorableClass] public class GEArtificialAntEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator, ISymbolicExpressionTreeGrammarBasedOperator { #region Parameter Properties public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } // genotype: public ILookupParameter IntegerVectorParameter { get { return (ILookupParameter)Parameters["IntegerVector"]; } } // phenotype: public ILookupParameter SymbolicExpressionTreeParameter { get { return (ILookupParameter)Parameters["SymbolicExpressionTree"]; } } public ILookupParameter WorldParameter { get { return (ILookupParameter)Parameters["World"]; } } public ILookupParameter MaxTimeStepsParameter { get { return (ILookupParameter)Parameters["MaxTimeSteps"]; } } public IValueLookupParameter SymbolicExpressionTreeGrammarParameter { get { return (IValueLookupParameter)Parameters["SymbolicExpressionTreeGrammar"]; } } // genotype-to-phenotype-mapper: public ILookupParameter GenotypeToPhenotypeMapperParameter { get { return (ILookupParameter)Parameters["GenotypeToPhenotypeMapper"]; } } #endregion [StorableConstructor] protected GEArtificialAntEvaluator(bool deserializing) : base(deserializing) { } protected GEArtificialAntEvaluator(GEArtificialAntEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new GEArtificialAntEvaluator(this, cloner); } public GEArtificialAntEvaluator() : base() { Parameters.Add(new LookupParameter("Quality", "The quality of the evaluated artificial ant solution.")); Parameters.Add(new LookupParameter("IntegerVector", "The artificial ant solution encoded as an integer vector genome.")); Parameters.Add(new LookupParameter("SymbolicExpressionTree", "The artificial ant solution encoded as a symbolic expression tree that should be evaluated")); Parameters.Add(new LookupParameter("World", "The world for the artificial ant with scattered food items.")); Parameters.Add(new LookupParameter("MaxTimeSteps", "The maximal number of time steps that the artificial ant should be simulated.")); Parameters.Add(new ValueLookupParameter("SymbolicExpressionTreeGrammar", "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); Parameters.Add(new LookupParameter("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).")); } public sealed override IOperation Apply() { SymbolicExpressionTree expression = GenotypeToPhenotypeMapperParameter.ActualValue.Map( SymbolicExpressionTreeGrammarParameter.ActualValue, IntegerVectorParameter.ActualValue ); SymbolicExpressionTreeParameter.ActualValue = expression; BoolMatrix world = WorldParameter.ActualValue; IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue; AntInterpreter interpreter = new AntInterpreter(); interpreter.MaxTimeSteps = maxTimeSteps.Value; interpreter.World = world; interpreter.Expression = expression; interpreter.Run(); QualityParameter.ActualValue = new DoubleValue(interpreter.FoodEaten); return null; } } }