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 |
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
35 | [StorableType("aceff7a2-0666-4055-b698-6ea3628713b6")]
|
---|
36 | public abstract class PermutationProblem : SingleObjectiveProblem<PermutationEncoding, Permutation> {
|
---|
37 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
38 | [Storable] protected ReferenceParameter<EnumValue<PermutationTypes>> PermutationTypeRefParameter { get; private set; }
|
---|
39 |
|
---|
40 | public int Dimension {
|
---|
41 | get { return DimensionRefParameter.Value.Value; }
|
---|
42 | set { DimensionRefParameter.Value.Value = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public PermutationTypes Type {
|
---|
46 | get { return PermutationTypeRefParameter.Value.Value; }
|
---|
47 | set { PermutationTypeRefParameter.Value.Value = value; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected PermutationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
53 | private void AfterDeserialization() {
|
---|
54 | RegisterEventHandlers();
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected PermutationProblem(PermutationProblem original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
60 | PermutationTypeRefParameter = cloner.Clone(original.PermutationTypeRefParameter);
|
---|
61 | RegisterEventHandlers();
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected PermutationProblem() : this(new PermutationEncoding() { Length = 10, Type = PermutationTypes.Absolute }) { }
|
---|
65 | protected PermutationProblem(PermutationEncoding encoding) : base(encoding) {
|
---|
66 | EncodingParameter.ReadOnly = true;
|
---|
67 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the permutation problem.", Encoding.LengthParameter));
|
---|
68 | Parameters.Add(PermutationTypeRefParameter = new ReferenceParameter<EnumValue<PermutationTypes>>("Type", "The type of the permutation.", Encoding.PermutationTypeParameter));
|
---|
69 | Operators.Add(new HammingSimilarityCalculator());
|
---|
70 | Operators.Add(new QualitySimilarityCalculator());
|
---|
71 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
72 |
|
---|
73 | Parameterize();
|
---|
74 | RegisterEventHandlers();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override void Analyze(Permutation[] permutations, double[] qualities, ResultCollection results, IRandom random) {
|
---|
78 | base.Analyze(permutations, qualities, results, random);
|
---|
79 | var best = GetBestSolution(permutations, qualities);
|
---|
80 | results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override void OnEncodingChanged() {
|
---|
84 | base.OnEncodingChanged();
|
---|
85 | Parameterize();
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void Parameterize() {
|
---|
89 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
90 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
91 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void RegisterEventHandlers() {
|
---|
96 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
97 | EnumValueParameterChangeHandler<PermutationTypes>.Create(PermutationTypeRefParameter, TypeOnChanged);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected virtual void DimensionOnChanged() { }
|
---|
101 |
|
---|
102 | protected virtual void TypeOnChanged() { }
|
---|
103 | }
|
---|
104 | }
|
---|