1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Analysis;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Optimization.Operators;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
34 | [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
|
---|
35 | public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
|
---|
36 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
37 | [Storable] public IResult<ISingleObjectiveSolutionContext<BinaryVector>> BestSolutionResult { get; private set; }
|
---|
38 |
|
---|
39 | private ISingleObjectiveSolutionContext<BinaryVector> BestSolution {
|
---|
40 | get => BestSolutionResult.Value;
|
---|
41 | set => BestSolutionResult.Value = value;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public int Dimension {
|
---|
45 | get { return DimensionRefParameter.Value.Value; }
|
---|
46 | protected set { DimensionRefParameter.Value.Value = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
51 | [StorableHook(HookType.AfterDeserialization)]
|
---|
52 | private void AfterDeserialization() {
|
---|
53 | RegisterEventHandlers();
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
|
---|
57 | : base(original, cloner) {
|
---|
58 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
59 | BestSolutionResult = cloner.Clone(original.BestSolutionResult);
|
---|
60 | RegisterEventHandlers();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
|
---|
64 | protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
|
---|
65 | EncodingParameter.ReadOnly = true;
|
---|
66 | EvaluatorParameter.ReadOnly = true;
|
---|
67 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
|
---|
68 | Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution"));
|
---|
69 |
|
---|
70 | Operators.Add(new HammingSimilarityCalculator());
|
---|
71 | // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
|
---|
72 | Operators.Add(new QualitySimilarityCalculator());
|
---|
73 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
74 |
|
---|
75 | Parameterize();
|
---|
76 | RegisterEventHandlers();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
|
---|
80 | base.Analyze(solutionContexts, results, random);
|
---|
81 | var best = GetBest(solutionContexts);
|
---|
82 | if (BestSolution == null || IsBetter(best, BestSolution))
|
---|
83 | BestSolution = best.Clone() as SingleObjectiveSolutionContext<BinaryVector>;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override sealed void OnEvaluatorChanged() {
|
---|
87 | throw new InvalidOperationException("Evaluator may not change!");
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override sealed void OnEncodingChanged() {
|
---|
91 | throw new InvalidOperationException("Encoding may not change!");
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected override void ParameterizeOperators() {
|
---|
95 | base.ParameterizeOperators();
|
---|
96 | Parameterize();
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void Parameterize() {
|
---|
100 | // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
|
---|
101 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
102 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
103 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void RegisterEventHandlers() {
|
---|
108 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected virtual void DimensionOnChanged() { }
|
---|
112 | }
|
---|
113 | }
|
---|