[16948] | 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 |
|
---|
[17695] | 24 | using System;
|
---|
[16948] | 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;
|
---|
[16948] | 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Optimization.Operators;
|
---|
[17544] | 33 | using HeuristicLab.Parameters;
|
---|
[16948] | 34 |
|
---|
| 35 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
| 36 | [StorableType("fb4cfc7c-dc7c-4da6-843f-0dad7d3d7981")]
|
---|
| 37 | public abstract class LinearLinkageProblem : SingleObjectiveProblem<LinearLinkageEncoding, LinearLinkage> {
|
---|
[17544] | 38 | [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
|
---|
[17747] | 39 | [Storable] public IResult<ISingleObjectiveSolutionContext<LinearLinkage>> BestSolutionResult { get; private set; }
|
---|
[17544] | 40 |
|
---|
| 41 | public int Dimension {
|
---|
| 42 | get { return DimensionRefParameter.Value.Value; }
|
---|
| 43 | set { DimensionRefParameter.Value.Value = value; }
|
---|
[16948] | 44 | }
|
---|
| 45 |
|
---|
[17747] | 46 | protected ISingleObjectiveSolutionContext<LinearLinkage> BestSolution {
|
---|
| 47 | get => BestSolutionResult.Value;
|
---|
| 48 | set => BestSolutionResult.Value = value;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[16948] | 51 | [StorableConstructor]
|
---|
| 52 | protected LinearLinkageProblem(StorableConstructorFlag _) : base(_) { }
|
---|
| 53 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 54 | private void AfterDeserialization() {
|
---|
| 55 | RegisterEventHandlers();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected LinearLinkageProblem(LinearLinkageProblem original, Cloner cloner)
|
---|
| 59 | : base(original, cloner) {
|
---|
[17544] | 60 | DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
|
---|
[17747] | 61 | BestSolutionResult = cloner.Clone(original.BestSolutionResult);
|
---|
[16948] | 62 | RegisterEventHandlers();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | protected LinearLinkageProblem() : this(new LinearLinkageEncoding() { Length = 10 }) { }
|
---|
| 66 | protected LinearLinkageProblem(LinearLinkageEncoding encoding) : base(encoding) {
|
---|
| 67 | EncodingParameter.ReadOnly = true;
|
---|
[17695] | 68 | EvaluatorParameter.ReadOnly = true;
|
---|
[17544] | 69 | Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the linear linkage problem.", Encoding.LengthParameter));
|
---|
[17747] | 70 | Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<LinearLinkage>>("Best Solution", "The best solution found so far."));
|
---|
[16948] | 71 |
|
---|
| 72 | Operators.Add(new HammingSimilarityCalculator());
|
---|
[17620] | 73 | // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
|
---|
[16948] | 74 | Operators.Add(new QualitySimilarityCalculator());
|
---|
| 75 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
| 76 |
|
---|
| 77 | Parameterize();
|
---|
| 78 | RegisterEventHandlers();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[17745] | 81 | public override void Analyze(ISingleObjectiveSolutionContext<LinearLinkage>[] solutionContexts, IRandom random) {
|
---|
| 82 | base.Analyze(solutionContexts, random);
|
---|
[17747] | 83 | var best = GetBest(solutionContexts);
|
---|
| 84 | if (BestSolution == null || IsBetter(best, BestSolution))
|
---|
| 85 | BestSolution = best.Clone() as SingleObjectiveSolutionContext<LinearLinkage>;
|
---|
[16948] | 86 | }
|
---|
| 87 |
|
---|
[17695] | 88 | protected override sealed void OnEvaluatorChanged() {
|
---|
| 89 | throw new InvalidOperationException("Evaluator may not change!");
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected override sealed void OnEncodingChanged() {
|
---|
| 93 | throw new InvalidOperationException("Encoding may not change!");
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[17620] | 96 | protected override void ParameterizeOperators() {
|
---|
| 97 | base.ParameterizeOperators();
|
---|
[16948] | 98 | Parameterize();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void Parameterize() {
|
---|
[17620] | 102 | // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
|
---|
[16948] | 103 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
| 104 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
| 105 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private void RegisterEventHandlers() {
|
---|
[17587] | 110 | IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
|
---|
[16948] | 111 | }
|
---|
| 112 |
|
---|
[17544] | 113 | protected virtual void DimensionOnChanged() { }
|
---|
[16948] | 114 | }
|
---|
| 115 | }
|
---|