1 |
|
---|
2 | #region License Information
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 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 | #endregion
|
---|
22 |
|
---|
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 IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; }
|
---|
37 | public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult => BestResultParameter;
|
---|
38 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
39 |
|
---|
40 | public int Dimension {
|
---|
41 | get { return Encoding.Length; }
|
---|
42 | set { Encoding.Length = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
47 | [StorableHook(HookType.AfterDeserialization)]
|
---|
48 | private void AfterDeserialization() {
|
---|
49 | RegisterEventHandlers();
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | BestResultParameter = cloner.Clone(original.BestResultParameter);
|
---|
55 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
56 | RegisterEventHandlers();
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
|
---|
60 | protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
|
---|
61 | EncodingParameter.ReadOnly = true;
|
---|
62 | Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
|
---|
63 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
|
---|
64 |
|
---|
65 | Operators.Add(new HammingSimilarityCalculator());
|
---|
66 | Operators.Add(new QualitySimilarityCalculator());
|
---|
67 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
68 |
|
---|
69 | Parameterize();
|
---|
70 | RegisterEventHandlers();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
|
---|
74 | var best = GetBest(solutionContexts);
|
---|
75 | var currentBest = BestResultParameter.ActualValue;
|
---|
76 | if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
|
---|
77 | BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void Parameterize() {
|
---|
81 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
82 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
83 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void RegisterEventHandlers() {
|
---|
88 | Encoding.PropertyChanged += (sender, args) => {
|
---|
89 | if (args.PropertyName == nameof(Encoding.Length))
|
---|
90 | DimensionOnChanged();
|
---|
91 | };
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected virtual void DimensionOnChanged() { }
|
---|
95 | }
|
---|
96 | }
|
---|