Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17544 was 17544, checked in by abeham, 4 years ago

#2521: worked on refactoring, worked a lot on binary encoding / problems

File size: 3.9 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.PermutationEncoding {
36  [StorableType("aceff7a2-0666-4055-b698-6ea3628713b6")]
37  public abstract class PermutationProblem : SingleObjectiveProblem<PermutationEncoding, Permutation> {
38    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
39    public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
40
41    public int Dimension {
42      get { return DimensionRefParameter.Value.Value; }
43      set { DimensionRefParameter.Value.Value = 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      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
56      RegisterEventHandlers();
57    }
58
59    protected PermutationProblem() : this(new PermutationEncoding() { Length = 10, Type = PermutationTypes.Absolute }) { }
60    protected PermutationProblem(PermutationEncoding encoding) : base(encoding) {
61      EncodingParameter.ReadOnly = true;
62      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the permutation problem.", Encoding.LengthParameter));
63
64      Operators.Add(new HammingSimilarityCalculator());
65      Operators.Add(new QualitySimilarityCalculator());
66      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
67
68      Parameterize();
69      RegisterEventHandlers();
70    }
71
72    public override void Analyze(Permutation[] permutations, double[] qualities, ResultCollection results, IRandom random) {
73      base.Analyze(permutations, qualities, results, random);
74      var best = GetBestSolution(permutations, qualities);
75      results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
76    }
77
78    protected override void OnEncodingChanged() {
79      base.OnEncodingChanged();
80      Parameterize();
81    }
82
83    private void Parameterize() {
84      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
85        similarityCalculator.SolutionVariableName = Encoding.Name;
86        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
87      }
88    }
89
90    private void RegisterEventHandlers() {
91      DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
92    }
93
94    private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
95      DimensionOnChanged();
96    }
97
98    protected virtual void DimensionOnChanged() { }
99  }
100}
Note: See TracBrowser for help on using the repository browser.