Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationProblem.cs @ 17230

Last change on this file since 17230 was 16950, checked in by abeham, 5 years ago

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

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