Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationMultiObjectiveProblem.cs

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

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
File size: 5.3 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.Parameters;
33
34namespace HeuristicLab.Encodings.PermutationEncoding {
35  [StorableType("7bc5215b-c181-40d0-a758-d7c19a356e18")]
36  public abstract class PermutationMultiObjectiveProblem : MultiObjectiveProblem<PermutationEncoding, Permutation> {
37    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
38    [Storable] protected ReferenceParameter<EnumValue<PermutationTypes>> PermutationTypeRefParameter { get; private set; }
39    [Storable] public IResult<ParetoFrontScatterPlot<Permutation>> BestParetoFrontResult { get; private set; }
40
41    public int Dimension {
42      get { return DimensionRefParameter.Value.Value; }
43      set { DimensionRefParameter.Value.Value = value; }
44    }
45
46    public PermutationTypes Type {
47      get { return PermutationTypeRefParameter.Value.Value; }
48      set { PermutationTypeRefParameter.Value.Value = value; }
49    }
50
51    protected ParetoFrontScatterPlot<Permutation> BestParetoFront {
52      get => BestParetoFrontResult.Value;
53      set => BestParetoFrontResult.Value = value;
54    }
55
56    [StorableConstructor]
57    protected PermutationMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
58    [StorableHook(HookType.AfterDeserialization)]
59    private void AfterDeserialization() {
60      RegisterEventHandlers();
61    }
62
63    protected PermutationMultiObjectiveProblem(PermutationMultiObjectiveProblem original, Cloner cloner)
64      : base(original, cloner) {
65      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
66      PermutationTypeRefParameter = cloner.Clone(original.PermutationTypeRefParameter);
67      BestParetoFrontResult = cloner.Clone(original.BestParetoFrontResult);
68
69      RegisterEventHandlers();
70    }
71
72    protected PermutationMultiObjectiveProblem() : this(new PermutationEncoding() { Length = 10, Type = PermutationTypes.Absolute }) { }
73    protected PermutationMultiObjectiveProblem(PermutationEncoding encoding) : base(encoding) {
74      EncodingParameter.ReadOnly = true;
75      EvaluatorParameter.ReadOnly = true;
76      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the permutation problem.", Encoding.LengthParameter));
77      Parameters.Add(PermutationTypeRefParameter = new ReferenceParameter<EnumValue<PermutationTypes>>("Type", "The type of the permutation.", Encoding.PermutationTypeParameter));
78      Results.Add(BestParetoFrontResult = new Result<ParetoFrontScatterPlot<Permutation>>("Best Pareto Front", "The best Pareto front found so far."));
79
80      Operators.Add(new HammingSimilarityCalculator());
81      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
82
83      Parameterize();
84      RegisterEventHandlers();
85    }
86
87    public override void Analyze(Permutation[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
88      base.Analyze(individuals, qualities, results, random);
89
90      var fronts = DominationCalculator.CalculateAllParetoFrontsIndices(individuals, qualities, Maximization);
91      var plot = new ParetoFrontScatterPlot<Permutation>(fronts, individuals, qualities, Objectives, BestKnownFront);
92
93      BestParetoFront = plot;
94    }
95
96    protected override sealed void OnEvaluatorChanged() {
97      throw new InvalidOperationException("Evaluator may not change!");
98    }
99
100    protected override sealed void OnEncodingChanged() {
101      throw new InvalidOperationException("Encoding may not change!");
102    }
103
104    protected override void ParameterizeOperators() {
105      base.ParameterizeOperators();
106      Parameterize();
107    }
108
109    private void Parameterize() {
110      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
111        similarityCalculator.SolutionVariableName = Encoding.Name;
112        similarityCalculator.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
113      }
114    }
115
116    private void RegisterEventHandlers() {
117      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
118      EnumValueParameterChangeHandler<PermutationTypes>.Create(PermutationTypeRefParameter, TypeOnChanged);
119    }
120
121    protected virtual void DimensionOnChanged() { }
122
123    protected virtual void TypeOnChanged() { }
124  }
125}
Note: See TracBrowser for help on using the repository browser.