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.RealVectorEncoding {
|
---|
36 | [StorableType("7860a955-af16-459a-9cd8-51667e06d38e")]
|
---|
37 | public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> {
|
---|
38 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
39 | [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
|
---|
40 |
|
---|
41 | public int Dimension {
|
---|
42 | get { return DimensionRefParameter.Value.Value; }
|
---|
43 | set { DimensionRefParameter.Value.Value = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public DoubleMatrix Bounds {
|
---|
47 | get { return BoundsRefParameter.Value; }
|
---|
48 | set { BoundsRefParameter.Value = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | protected RealVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
53 | [StorableHook(HookType.AfterDeserialization)]
|
---|
54 | private void AfterDeserialization() {
|
---|
55 | RegisterEventHandlers();
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected RealVectorProblem(RealVectorProblem original, Cloner cloner)
|
---|
59 | : base(original, cloner) {
|
---|
60 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
61 | BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
|
---|
62 | RegisterEventHandlers();
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected RealVectorProblem() : this(new RealVectorEncoding() { Length = 10 }) { }
|
---|
66 | protected RealVectorProblem(RealVectorEncoding encoding) : base(encoding) {
|
---|
67 | EncodingParameter.ReadOnly = true;
|
---|
68 | EvaluatorParameter.ReadOnly = true;
|
---|
69 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the real vector problem.", Encoding.LengthParameter));
|
---|
70 | Parameters.Add(BoundsRefParameter = new ReferenceParameter<DoubleMatrix>("Bounds", "The bounding box of the values.", Encoding.BoundsParameter));
|
---|
71 |
|
---|
72 | Operators.Add(new HammingSimilarityCalculator());
|
---|
73 |
|
---|
74 | // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
|
---|
75 | Operators.Add(new QualitySimilarityCalculator());
|
---|
76 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
77 |
|
---|
78 | Parameterize();
|
---|
79 | RegisterEventHandlers();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void Analyze(ISingleObjectiveSolutionContext<RealVector>[] solutionContexts, IRandom random) {
|
---|
83 | base.Analyze(solutionContexts, random);
|
---|
84 |
|
---|
85 | //TODO: reimplement code below using results directly
|
---|
86 |
|
---|
87 | //var best = GetBestSolution(vectors, qualities);
|
---|
88 |
|
---|
89 | //results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override sealed void OnEvaluatorChanged() {
|
---|
93 | throw new InvalidOperationException("Evaluator may not change!");
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected override sealed void OnEncodingChanged() {
|
---|
97 | throw new InvalidOperationException("Encoding may not change!");
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void ParameterizeOperators() {
|
---|
101 | base.ParameterizeOperators();
|
---|
102 | Parameterize();
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void Parameterize() {
|
---|
106 | // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
|
---|
107 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
108 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
109 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void RegisterEventHandlers() {
|
---|
114 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
115 | DoubleMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected virtual void DimensionOnChanged() { }
|
---|
119 |
|
---|
120 | protected virtual void BoundsOnChanged() { }
|
---|
121 | }
|
---|
122 | }
|
---|