Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Added cancellation token to evaluate function of problems.

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