1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.LawnMower {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("Best Lawn Mower Solution Analyzer", "Analyzer that stores the best lawn mower solution.")]
|
---|
34 | public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
|
---|
35 |
|
---|
36 | private const string QualityParameterName = "Quality";
|
---|
37 | private const string SymbolicExpressionTreeParameterName = "LawnMowerProgram";
|
---|
38 | private const string LawnWidthParameterName = "LawnWidth";
|
---|
39 | private const string LawnLengthParameterName = "LawnLength";
|
---|
40 | private const string BestSolutionParameterName = "Best solution";
|
---|
41 | private const string ResultsParameterName = "Results";
|
---|
42 |
|
---|
43 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
44 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
45 | }
|
---|
46 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
47 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<IntValue> LawnWidthParameter {
|
---|
50 | get { return (ILookupParameter<IntValue>)Parameters[LawnWidthParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<IntValue> LawnLengthParameter {
|
---|
53 | get { return (ILookupParameter<IntValue>)Parameters[LawnLengthParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<Solution> BestSolutionParameter {
|
---|
56 | get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ResultCollection> ResultParameter {
|
---|
59 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
64 | protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
|
---|
65 | : base(original, cloner) {
|
---|
66 | }
|
---|
67 |
|
---|
68 | public BestSolutionAnalyzer() {
|
---|
69 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the lawn mower program."));
|
---|
70 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The lawn mower program to evaluate represented as symbolic expression tree."));
|
---|
71 | Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best lawn mower solution."));
|
---|
72 | Parameters.Add(new LookupParameter<IntValue>(LawnWidthParameterName, "The width of the lawn to mow."));
|
---|
73 | Parameters.Add(new LookupParameter<IntValue>(LawnLengthParameterName, "The length of the lawn to mow."));
|
---|
74 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
|
---|
75 | }
|
---|
76 |
|
---|
77 | [StorableHook(HookType.AfterDeserialization)]
|
---|
78 | private void AfterDeserialization() {
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new BestSolutionAnalyzer(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IOperation Apply() {
|
---|
86 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
87 | var qualities = QualityParameter.ActualValue;
|
---|
88 |
|
---|
89 | // find max tree
|
---|
90 | double maxQuality = double.NegativeInfinity;
|
---|
91 | ISymbolicExpressionTree bestTree = null;
|
---|
92 | for (int i = 0; i < qualities.Length; i++) {
|
---|
93 | if (qualities[i].Value > maxQuality) {
|
---|
94 | maxQuality = qualities[i].Value;
|
---|
95 | bestTree = trees[i];
|
---|
96 | }
|
---|
97 | }
|
---|
98 | int length = LawnLengthParameter.ActualValue.Value;
|
---|
99 | int width = LawnWidthParameter.ActualValue.Value;
|
---|
100 | var bestSolution = new Solution(bestTree, length, width);
|
---|
101 | BestSolutionParameter.ActualValue = bestSolution;
|
---|
102 |
|
---|
103 | var resultCollection = ResultParameter.ActualValue;
|
---|
104 | if (!resultCollection.ContainsKey(BestSolutionParameterName)) {
|
---|
105 | resultCollection.Add(new Result(BestSolutionParameterName, "The best lawn mower solution", bestSolution));
|
---|
106 | } else {
|
---|
107 | resultCollection[BestSolutionParameterName].Value = bestSolution;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return base.Apply();
|
---|
111 | }
|
---|
112 | public bool EnabledByDefault {
|
---|
113 | get { return true; }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|