[16876] | 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.Linq;
|
---|
| 25 | using HEAL.Attic;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[17544] | 29 | using HeuristicLab.Data;
|
---|
[16876] | 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Optimization.Operators;
|
---|
[17544] | 32 | using HeuristicLab.Parameters;
|
---|
[16876] | 33 |
|
---|
| 34 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
| 35 | [StorableType("c6081457-a3de-45ce-9f47-e0eb1c851bd2")]
|
---|
| 36 | public abstract class IntegerVectorProblem : SingleObjectiveProblem<IntegerVectorEncoding, IntegerVector> {
|
---|
[17522] | 37 | [Storable] protected IResultParameter<IntegerVector> BestResultParameter { get; private set; }
|
---|
[17544] | 38 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
| 39 | [Storable] protected ReferenceParameter<IntMatrix> BoundsRefParameter { get; private set; }
|
---|
[17522] | 40 |
|
---|
[17544] | 41 | public int Dimension {
|
---|
| 42 | get { return DimensionRefParameter.Value.Value; }
|
---|
[17587] | 43 | protected set { DimensionRefParameter.Value.Value = value; }
|
---|
[16876] | 44 | }
|
---|
| 45 |
|
---|
[17544] | 46 | public IntMatrix Bounds {
|
---|
| 47 | get { return BoundsRefParameter.Value; }
|
---|
[17587] | 48 | protected set { BoundsRefParameter.Value = value; }
|
---|
[17544] | 49 | }
|
---|
| 50 |
|
---|
[16876] | 51 | [StorableConstructor]
|
---|
| 52 | protected IntegerVectorProblem(StorableConstructorFlag _) : base(_) { }
|
---|
| 53 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 54 | private void AfterDeserialization() {
|
---|
| 55 | RegisterEventHandlers();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
|
---|
| 59 | : base(original, cloner) {
|
---|
[17522] | 60 | BestResultParameter = cloner.Clone(original.BestResultParameter);
|
---|
[17544] | 61 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
| 62 | BoundsRefParameter = cloner.Clone(original.BoundsRefParameter);
|
---|
[16876] | 63 | RegisterEventHandlers();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[16948] | 66 | protected IntegerVectorProblem() : this(new IntegerVectorEncoding() { Length = 10 }) { }
|
---|
| 67 | protected IntegerVectorProblem(IntegerVectorEncoding encoding) : base(encoding) {
|
---|
| 68 | EncodingParameter.ReadOnly = true;
|
---|
[17522] | 69 | Parameters.Add(BestResultParameter = new ResultParameter<IntegerVector>("Best Solution", "The best solution."));
|
---|
[17544] | 70 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the integer vector problem.", Encoding.LengthParameter));
|
---|
| 71 | Parameters.Add(BoundsRefParameter = new ReferenceParameter<IntMatrix>("Bounds", "The bounding box and step sizes of the values.", Encoding.BoundsParameter));
|
---|
[16876] | 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(IntegerVector[] vectors, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 82 | base.Analyze(vectors, qualities, results, random);
|
---|
| 83 | var best = GetBestSolution(vectors, qualities);
|
---|
[16876] | 84 |
|
---|
[16950] | 85 | results.AddOrUpdateResult("Best Solution", (IItem)best.Item1.Clone());
|
---|
[16876] | 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() {
|
---|
[17587] | 101 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
| 102 | IntMatrixParameterChangeHandler.Create(BoundsRefParameter, BoundsOnChanged);
|
---|
[16876] | 103 | }
|
---|
| 104 |
|
---|
[17544] | 105 | protected virtual void DimensionOnChanged() { }
|
---|
| 106 |
|
---|
| 107 | protected virtual void BoundsOnChanged() { }
|
---|
[16876] | 108 | }
|
---|
| 109 | }
|
---|