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;
|
---|
25 | using System.Linq;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Analysis;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 |
|
---|
35 | namespace 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 | [Storable] protected ReferenceParameter<EnumValue<PermutationTypes>> PermutationTypeRefParameter { get; private set; }
|
---|
40 | [Storable] public IResult<ISingleObjectiveSolutionContext<Permutation>> BestSolutionResult { get; private set; }
|
---|
41 |
|
---|
42 | public int Dimension {
|
---|
43 | get { return DimensionRefParameter.Value.Value; }
|
---|
44 | set { DimensionRefParameter.Value.Value = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public PermutationTypes Type {
|
---|
48 | get { return PermutationTypeRefParameter.Value.Value; }
|
---|
49 | set { PermutationTypeRefParameter.Value.Value = value; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected ISingleObjectiveSolutionContext<Permutation> BestSolution {
|
---|
53 | get => BestSolutionResult.Value;
|
---|
54 | set => BestSolutionResult.Value = value;
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | protected PermutationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
59 | [StorableHook(HookType.AfterDeserialization)]
|
---|
60 | private void AfterDeserialization() {
|
---|
61 | RegisterEventHandlers();
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected PermutationProblem(PermutationProblem original, Cloner cloner)
|
---|
65 | : base(original, cloner) {
|
---|
66 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
67 | PermutationTypeRefParameter = cloner.Clone(original.PermutationTypeRefParameter);
|
---|
68 | BestSolutionResult = cloner.Clone(original.BestSolutionResult);
|
---|
69 | RegisterEventHandlers();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected PermutationProblem() : this(new PermutationEncoding() { Length = 10, Type = PermutationTypes.Absolute }) { }
|
---|
73 | protected PermutationProblem(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(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<Permutation>>("Best Solution", "The best solution found so far."));
|
---|
79 |
|
---|
80 | Operators.Add(new HammingSimilarityCalculator());
|
---|
81 | // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
|
---|
82 | Operators.Add(new QualitySimilarityCalculator());
|
---|
83 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
84 |
|
---|
85 | Parameterize();
|
---|
86 | RegisterEventHandlers();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override void Analyze(ISingleObjectiveSolutionContext<Permutation>[] solutionContexts, IRandom random) {
|
---|
90 | base.Analyze(solutionContexts, random);
|
---|
91 | var best = GetBest(solutionContexts);
|
---|
92 | if (BestSolution == null || IsBetter(best, BestSolution))
|
---|
93 | BestSolution = best.Clone() as SingleObjectiveSolutionContext<Permutation>;
|
---|
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 | // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
|
---|
111 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
112 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
113 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void RegisterEventHandlers() {
|
---|
118 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
119 | EnumValueParameterChangeHandler<PermutationTypes>.Create(PermutationTypeRefParameter, TypeOnChanged);
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected virtual void DimensionOnChanged() { }
|
---|
123 |
|
---|
124 | protected virtual void TypeOnChanged() { }
|
---|
125 | }
|
---|
126 | }
|
---|