Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorProblem.cs @ 17383

Last change on this file since 17383 was 16950, checked in by abeham, 6 years ago

#2521: Refactor Analyze method of encoding-specific problems, simplify Analyze of SO-TF

File size: 3.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.Optimization;
31using HeuristicLab.Optimization.Operators;
32
33namespace HeuristicLab.Encodings.IntegerVectorEncoding {
34  [StorableType("c6081457-a3de-45ce-9f47-e0eb1c851bd2")]
35  public abstract class IntegerVectorProblem : SingleObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
36    public int Length {
37      get { return Encoding.Length; }
38      set { Encoding.Length = value; }
39    }
40
41    [StorableConstructor]
42    protected IntegerVectorProblem(StorableConstructorFlag _) : base(_) { }
43    [StorableHook(HookType.AfterDeserialization)]
44    private void AfterDeserialization() {
45      RegisterEventHandlers();
46    }
47
48    protected IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
49      : base(original, cloner) {
50      RegisterEventHandlers();
51    }
52
53    protected IntegerVectorProblem() : this(new IntegerVectorEncoding() { Length = 10 }) { }
54    protected IntegerVectorProblem(IntegerVectorEncoding encoding) : base(encoding) {
55      EncodingParameter.ReadOnly = true;
56
57      Operators.Add(new HammingSimilarityCalculator());
58      Operators.Add(new QualitySimilarityCalculator());
59      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
60
61      Parameterize();
62      RegisterEventHandlers();
63    }
64
65    public override void Analyze(IntegerVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
66      base.Analyze(vectors, qualities, results, random);
67      var best = GetBestSolution(vectors, qualities);
68
69      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
70    }
71
72    protected override void OnEncodingChanged() {
73      base.OnEncodingChanged();
74      Parameterize();
75    }
76
77    private void Parameterize() {
78      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
79        similarityCalculator.SolutionVariableName = Encoding.Name;
80        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
81      }
82    }
83
84    private void RegisterEventHandlers() {
85      Encoding.LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
86    }
87
88    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
89  }
90}
Note: See TracBrowser for help on using the repository browser.