source: trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs @ 13238

Last change on this file since 13238 was 13238, checked in by gkronber, 4 years ago

#2454: added acronyms where appropriate

File size: 5.7 KB
RevLine 
[10071]1#region License Information
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
[12915]24using System.Diagnostics.Contracts;
[10071]25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12915]33using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
[10071]34using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
[13233]35using HeuristicLab.Random;
[10071]36
37namespace HeuristicLab.Problems.GrammaticalEvolution {
[13238]38  [Item("Grammatical Evolution Artificial Ant Problem (GE)", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
[12504]39  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
[10071]40  [StorableClass]
[12915]41  public sealed class GEArtificialAntProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IStorableContent {
[10071]42    public string Filename { get; set; }
43
44    #region Parameter Properties
45    public IValueParameter<BoolMatrix> WorldParameter {
46      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
47    }
[12915]48    public IFixedValueParameter<IntValue> MaxTimeStepsParameter {
49      get { return (IFixedValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
[10071]50    }
51    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
52      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
53    }
54    #endregion
55
56    #region Properties
57    public BoolMatrix World {
58      get { return WorldParameter.Value; }
59      set { WorldParameter.Value = value; }
60    }
[12915]61    public int MaxTimeSteps {
62      get { return MaxTimeStepsParameter.Value.Value; }
63      set { MaxTimeStepsParameter.Value.Value = value; }
[10071]64    }
65    #endregion
66
67    [StorableConstructor]
68    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
69    [StorableHook(HookType.AfterDeserialization)]
[12915]70    private void AfterDeserialization() { }
71
72    public override bool Maximization {
73      get { return true; }
[10071]74    }
75
[12915]76    [Storable]
77    // parameters of the wrapped problem cannot be changed therefore it is not strictly necessary to clone and store it
78    private readonly HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem wrappedAntProblem;
79
[10071]80    private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
81      : base(original, cloner) {
[12915]82      this.wrappedAntProblem = cloner.Clone(original.wrappedAntProblem);
[10071]83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new GEArtificialAntProblem(this, cloner);
87    }
88
89    public GEArtificialAntProblem()
[12915]90      : base() {
91      wrappedAntProblem = new HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem();
92      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", wrappedAntProblem.World));
93      Parameters.Add(new FixedValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
[10071]94      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
95
[12915]96      Encoding = new IntegerVectorEncoding(30) { Bounds = new IntMatrix(new int[,] { { 0, 100 } }) };
[10071]97
[12915]98      BestKnownQuality = wrappedAntProblem.BestKnownQuality;
[10071]99    }
100
[12915]101    public override double Evaluate(Individual individual, IRandom random) {
102      var vector = individual.IntegerVector();
[10071]103
[12915]104      var bounds = Encoding.Bounds;
105      var len = Encoding.Length;
106      var grammar = wrappedAntProblem.Encoding.Grammar;
107      var mapper = GenotypeToPhenotypeMapperParameter.Value;
[10071]108
[13233]109      // Evaluate might be called concurrently therefore access to random has to be synchronized.
110      // However, results depend on the order of execution. Therefore, results might be different for the same seed when using the parallel engine.
111      IRandom fastRand;
112      lock (random) {
113        fastRand = new FastRandom(random.Next());
114      }
115      var tree = mapper.Map(fastRand, bounds, len, grammar, vector);
[10071]116
[12915]117      Interpreter interpreter = new Interpreter(tree, World, MaxTimeSteps);
118      interpreter.Run();
[10071]119
[12915]120      return interpreter.FoodEaten;
[10071]121    }
122
[12915]123    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
124      var bounds = Encoding.Bounds;
125      var len = Encoding.Length;
126      var grammar = wrappedAntProblem.Encoding.Grammar;
127      var mapper = GenotypeToPhenotypeMapperParameter.Value;
[10071]128
[12915]129      var trees = individuals
130        .Select(ind => mapper.Map(random, bounds, len, grammar, ind.IntegerVector()))
131        .ToArray();
[10071]132
[12915]133      wrappedAntProblem.Analyze(trees, qualities, results, random);
[10071]134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.