[16949] | 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;
|
---|
[17544] | 30 | using HeuristicLab.Data;
|
---|
[16949] | 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Optimization.Operators;
|
---|
[17544] | 33 | using HeuristicLab.Parameters;
|
---|
[16949] | 34 |
|
---|
| 35 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
| 36 | [StorableType("7860a955-af16-459a-9cd8-51667e06d38e")]
|
---|
| 37 | public abstract class RealVectorProblem : SingleObjectiveProblem<RealVectorEncoding, RealVector> {
|
---|
[17544] | 38 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
| 39 | public IValueParameter<IntValue> DimensionParameter => DimensionRefParameter;
|
---|
| 40 | [Storable] protected ReferenceParameter<DoubleMatrix> BoundsRefParameter { get; private set; }
|
---|
| 41 | public IValueParameter<DoubleMatrix> BoundsParameter => BoundsRefParameter;
|
---|
| 42 |
|
---|
| 43 | public int Dimension {
|
---|
| 44 | get { return DimensionRefParameter.Value.Value; }
|
---|
| 45 | set { DimensionRefParameter.Value.Value = value; }
|
---|
[16949] | 46 | }
|
---|
| 47 |
|
---|
[17544] | 48 | public DoubleMatrix Bounds {
|
---|
| 49 | get { return BoundsRefParameter.Value; }
|
---|
| 50 | set { BoundsParameter.Value = value; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[16949] | 53 | [StorableConstructor]
|
---|
| 54 | protected RealVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
| 55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 56 | private void AfterDeserialization() {
|
---|
| 57 | RegisterEventHandlers();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected RealVectorProblem(RealVectorProblem original, Cloner cloner)
|
---|
| 61 | : base(original, cloner) {
|
---|
[17544] | 62 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
| 63 | BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
|
---|
[16949] | 64 | RegisterEventHandlers();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected RealVectorProblem() : this(new RealVectorEncoding() { Length = 10 }) { }
|
---|
| 68 | protected RealVectorProblem(RealVectorEncoding encoding) : base(encoding) {
|
---|
| 69 | EncodingParameter.ReadOnly = true;
|
---|
[17544] | 70 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the real vector problem.", Encoding.LengthParameter));
|
---|
| 71 | Parameters.Add(BoundsRefParameter = new ReferenceParameter<DoubleMatrix>("Bounds", "The bounding box of the values.", Encoding.BoundsParameter));
|
---|
[16949] | 72 |
|
---|
| 73 | Operators.Add(new HammingSimilarityCalculator());
|
---|
| 74 | Operators.Add(new QualitySimilarityCalculator());
|
---|
| 75 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
| 76 |
|
---|
| 77 | Parameterize();
|
---|
| 78 | RegisterEventHandlers();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[16950] | 81 | public override void Analyze(RealVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 82 | base.Analyze(vectors, qualities, results, random);
|
---|
| 83 | var best = GetBestSolution(vectors, qualities);
|
---|
[16949] | 84 |
|
---|
[16950] | 85 | results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
|
---|
[16949] | 86 | }
|
---|
| 87 |
|
---|
| 88 | protected override void OnEncodingChanged() {
|
---|
| 89 | base.OnEncodingChanged();
|
---|
| 90 | Parameterize();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | private void Parameterize() {
|
---|
| 94 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
| 95 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
| 96 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private void RegisterEventHandlers() {
|
---|
[17544] | 101 | DimensionRefParameter.Value.ValueChanged += DimensionParameter_Value_ValueChanged;
|
---|
| 102 | BoundsRefParameter.ValueChanged += BoundsParameter_ValueChanged;
|
---|
[16949] | 103 | }
|
---|
| 104 |
|
---|
[17544] | 105 | private void DimensionParameter_Value_ValueChanged(object sender, EventArgs e) {
|
---|
| 106 | DimensionOnChanged();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 110 | BoundsOnChanged();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | protected virtual void DimensionOnChanged() { }
|
---|
| 114 |
|
---|
| 115 | protected virtual void BoundsOnChanged() { }
|
---|
[16949] | 116 | }
|
---|
| 117 | }
|
---|