Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.LawnMower/3.3/Evaluator.cs @ 12416

Last change on this file since 12416 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.LawnMower {
32  [StorableClass]
33  [Item("Lawn Mower Evaluator", "Evaluator for the lawn mower demo GP problem.")]
34  public class Evaluator : InstrumentedOperator, ISingleObjectiveEvaluator {
35
36    private const string QualityParameterName = "Quality";
37    private const string LawnMowerProgramParameterName = "LawnMowerProgram";
38    private const string LawnWidthParameterName = "LawnWidth";
39    private const string LawnLengthParameterName = "LawnLength";
40
41    public ILookupParameter<DoubleValue> QualityParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
43    }
44    public ILookupParameter<ISymbolicExpressionTree> LawnMowerProgramParameter {
45      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[LawnMowerProgramParameterName]; }
46    }
47    public ILookupParameter<IntValue> LawnWidthParameter {
48      get { return (ILookupParameter<IntValue>)Parameters[LawnWidthParameterName]; }
49    }
50    public ILookupParameter<IntValue> LawnLengthParameter {
51      get { return (ILookupParameter<IntValue>)Parameters[LawnLengthParameterName]; }
52    }
53
54    [StorableConstructor]
55    protected Evaluator(bool deserializing) : base(deserializing) { }
56    protected Evaluator(Evaluator original, Cloner cloner)
57      : base(original, cloner) {
58    }
59
60    public Evaluator() {
61      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the lawn mower program."));
62      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(LawnMowerProgramParameterName, "The lawn mower program to evaluate represented as symbolic expression tree."));
63      Parameters.Add(new LookupParameter<IntValue>(LawnWidthParameterName, "The width of the lawn to mow."));
64      Parameters.Add(new LookupParameter<IntValue>(LawnLengthParameterName, "The length of the lawn to mow."));
65    }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new Evaluator(this, cloner);
73    }
74    public override IOperation InstrumentedApply() {
75      int length = LawnLengthParameter.ActualValue.Value;
76      int width = LawnWidthParameter.ActualValue.Value;
77      ISymbolicExpressionTree tree = LawnMowerProgramParameter.ActualValue;
78
79      bool[,] lawn = Interpreter.EvaluateLawnMowerProgram(length, width, tree);
80
81      // count number of squares that have been mowed
82      int numberOfMowedCells = 0;
83      for (int i = 0; i < length; i++)
84        for (int j = 0; j < width; j++)
85          if (lawn[i, j]) {
86            numberOfMowedCells++;
87          }
88
89      QualityParameter.ActualValue = new DoubleValue(numberOfMowedCells);
90      return base.InstrumentedApply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.