Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs @ 17520

Last change on this file since 17520 was 17520, checked in by mkommend, 4 years ago

#2521: Removed parameter for problemData in IDataAnalysisProblem.

File size: 3.2 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  [StorableType("A1B9F4C8-5E29-493C-A483-2AC68453BC63")]
34  public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<SymbolicExpressionTreeEncoding, ISymbolicExpressionTree> {
35
36    // persistence
37    [StorableConstructor]
38    protected SymbolicExpressionTreeProblem(StorableConstructorFlag _) : base(_) { }
39    [StorableHook(HookType.AfterDeserialization)]
40    private void AfterDeserialization() { }
41
42
43    // cloning
44    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeProblem original, Cloner cloner)
45      : base(original, cloner) {
46    }
47
48    protected SymbolicExpressionTreeProblem() : this(new SymbolicExpressionTreeEncoding()) { }   
49    protected SymbolicExpressionTreeProblem(SymbolicExpressionTreeEncoding encoding)
50      : base(encoding) {
51      EncodingParameter.ReadOnly = true;
52    }
53
54    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results,
55      IRandom random) {
56      if (!results.ContainsKey("Best Solution Quality")) {
57        results.Add(new Result("Best Solution Quality", typeof(DoubleValue)));
58      }
59      if (!results.ContainsKey("Best Solution")) {
60        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
61      }
62
63      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
64
65      if (results["Best Solution Quality"].Value == null ||
66          IsBetter(bestQuality, ((DoubleValue)results["Best Solution Quality"].Value).Value)) {
67        var bestIdx = Array.IndexOf(qualities, bestQuality);
68        var bestClone = (IItem)trees[bestIdx].Clone();
69
70        results["Best Solution"].Value = bestClone;
71        results["Best Solution Quality"].Value = new DoubleValue(bestQuality);
72      }
73    }
74
75    protected override void OnEncodingChanged() {
76      base.OnEncodingChanged();
77      Parameterize();
78    }
79
80    private void Parameterize() {
81      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
82        similarityCalculator.SolutionVariableName = Encoding.Name;
83        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.