Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs @ 16813

Last change on this file since 16813 was 16813, checked in by mkommend, 5 years ago

#2521: Changed base ctor call in all GeneticProgrammingProblems.

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