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.Linq;
|
---|
23 | using HEAL.Attic;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
33 | [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
|
---|
34 | public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
|
---|
35 | [Storable] protected IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; }
|
---|
36 | public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult => BestResultParameter;
|
---|
37 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
38 |
|
---|
39 | public int Dimension {
|
---|
40 | get { return DimensionRefParameter.Value.Value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
45 | [StorableHook(HookType.AfterDeserialization)]
|
---|
46 | private void AfterDeserialization() {
|
---|
47 | RegisterEventHandlers();
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | BestResultParameter = cloner.Clone(original.BestResultParameter);
|
---|
53 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
54 | RegisterEventHandlers();
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
|
---|
58 | protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
|
---|
59 | EncodingParameter.ReadOnly = true;
|
---|
60 | Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
|
---|
61 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
|
---|
62 |
|
---|
63 | Operators.Add(new HammingSimilarityCalculator());
|
---|
64 | Operators.Add(new QualitySimilarityCalculator());
|
---|
65 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
66 |
|
---|
67 | Parameterize();
|
---|
68 | RegisterEventHandlers();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
|
---|
72 | var best = GetBest(solutionContexts);
|
---|
73 | var currentBest = BestResultParameter.ActualValue;
|
---|
74 | if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
|
---|
75 | BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void Parameterize() {
|
---|
79 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
80 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
81 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void RegisterEventHandlers() {
|
---|
86 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected virtual void DimensionOnChanged() { }
|
---|
90 | }
|
---|
91 | }
|
---|