Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/LinearLinkageProblem.cs @ 17745

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

#2971: Added first draft of results implementation and problem adaptation.

File size: 4.2 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Encodings.LinearLinkageEncoding {
36  [StorableType("fb4cfc7c-dc7c-4da6-843f-0dad7d3d7981")]
37  public abstract class LinearLinkageProblem : SingleObjectiveProblem<LinearLinkageEncoding, LinearLinkage> {
38    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
39
40    public int Dimension {
41      get { return DimensionRefParameter.Value.Value; }
42      set { DimensionRefParameter.Value.Value = value; }
43    }
44
45    [StorableConstructor]
46    protected LinearLinkageProblem(StorableConstructorFlag _) : base(_) { }
47    [StorableHook(HookType.AfterDeserialization)]
48    private void AfterDeserialization() {
49      RegisterEventHandlers();
50    }
51
52    protected LinearLinkageProblem(LinearLinkageProblem original, Cloner cloner)
53      : base(original, cloner) {
54      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
55      RegisterEventHandlers();
56    }
57
58    protected LinearLinkageProblem() : this(new LinearLinkageEncoding() { Length = 10 }) { }
59    protected LinearLinkageProblem(LinearLinkageEncoding encoding) : base(encoding) {
60      EncodingParameter.ReadOnly = true;
61      EvaluatorParameter.ReadOnly = true;
62      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the linear linkage problem.", Encoding.LengthParameter));
63
64      Operators.Add(new HammingSimilarityCalculator());
65      // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
66      Operators.Add(new QualitySimilarityCalculator());
67      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
68
69      Parameterize();
70      RegisterEventHandlers();
71    }
72
73    public override void Analyze(ISingleObjectiveSolutionContext<LinearLinkage>[] solutionContexts, IRandom random) {
74      base.Analyze(solutionContexts, random);
75
76      //TODO: reimplement code below using results directly
77
78      //var best = GetBestSolution(vectors, qualities);
79
80      //results.AddOrUpdateResult("Best Solution", (Item)best.Item1.Clone());
81    }
82
83    protected override sealed void OnEvaluatorChanged() {
84      throw new InvalidOperationException("Evaluator may not change!");
85    }
86
87    protected override sealed void OnEncodingChanged() {
88      throw new InvalidOperationException("Encoding may not change!");
89    }
90
91    protected override void ParameterizeOperators() {
92      base.ParameterizeOperators();
93      Parameterize();
94    }
95
96    private void Parameterize() {
97      // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
98      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
99        similarityCalculator.SolutionVariableName = Encoding.Name;
100        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
101      }
102    }
103
104    private void RegisterEventHandlers() {
105      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
106    }
107
108    protected virtual void DimensionOnChanged() { }
109  }
110}
Note: See TracBrowser for help on using the repository browser.