Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs @ 17544

Last change on this file since 17544 was 17544, checked in by abeham, 4 years ago

#2521: worked on refactoring, worked a lot on binary encoding / problems

File size: 4.1 KB
RevLine 
[17229]1
2#region License Information
[11635]3/* HeuristicLab
[17226]4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11635]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#endregion
22
[11996]23using System.Linq;
[16814]24using HEAL.Attic;
[16692]25using HeuristicLab.Analysis;
[11635]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[17544]28using HeuristicLab.Data;
[11635]29using HeuristicLab.Optimization;
[16692]30using HeuristicLab.Optimization.Operators;
[17544]31using HeuristicLab.Parameters;
[11635]32
[16814]33namespace HeuristicLab.Encodings.BinaryVectorEncoding {
[16723]34  [StorableType("2F6FEB34-BD19-47AF-9484-7F48565C0C43")]
[16814]35  public abstract class BinaryVectorProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
[17522]36    [Storable] protected IResultParameter<ISingleObjectiveSolutionContext<BinaryVector>> BestResultParameter { get; private set; }
[17544]37    public IResultDefinition<ISingleObjectiveSolutionContext<BinaryVector>> BestResult => BestResultParameter;
38    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
[17517]39
[17544]40    public int Dimension {
[11987]41      get { return Encoding.Length; }
42      set { Encoding.Length = value; }
[11635]43    }
44
[11987]45    [StorableConstructor]
[16814]46    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
[11990]47    [StorableHook(HookType.AfterDeserialization)]
48    private void AfterDeserialization() {
49      RegisterEventHandlers();
50    }
[11635]51
[16814]52    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
[11990]53      : base(original, cloner) {
[17522]54      BestResultParameter = cloner.Clone(original.BestResultParameter);
[17544]55      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
[11990]56      RegisterEventHandlers();
57    }
58
[16948]59    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
60    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
61      EncodingParameter.ReadOnly = true;
[17522]62      Parameters.Add(BestResultParameter = new ResultParameter<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution."));
[17544]63      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
[16814]64
[16692]65      Operators.Add(new HammingSimilarityCalculator());
66      Operators.Add(new QualitySimilarityCalculator());
67      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
[16814]68
[16692]69      Parameterize();
[11990]70      RegisterEventHandlers();
71    }
72
[17522]73    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
74      var best = GetBest(solutionContexts);
75      var currentBest = BestResultParameter.ActualValue;
76      if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
77        BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
[11996]78    }
[11990]79
[16692]80    private void Parameterize() {
81      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
[16814]82        similarityCalculator.SolutionVariableName = Encoding.Name;
[16692]83        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
84      }
85    }
[11990]86
87    private void RegisterEventHandlers() {
[17544]88      Encoding.PropertyChanged += (sender, args) => {
89        if (args.PropertyName == nameof(Encoding.Length))
90          DimensionOnChanged();
91      };
[11990]92    }
93
[17544]94    protected virtual void DimensionOnChanged() { }
[11635]95  }
96}
Note: See TracBrowser for help on using the repository browser.