Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: worked on refactoring

  • add results to problem base classes
  • fix external evaluation problem
  • Add result descriptions
File size: 4.7 KB
RevLine 
[17567]1#region License Information
[11635]2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11635]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[17695]22using System;
[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> {
[17544]36    [Storable] protected ReferenceParameter<IntValue> DimensionRefParameter { get; private set; }
[17612]37    [Storable] public IResult<ISingleObjectiveSolutionContext<BinaryVector>> BestSolutionResult { get; private set; }
[17517]38
[17544]39    public int Dimension {
[17567]40      get { return DimensionRefParameter.Value.Value; }
[17570]41      protected set { DimensionRefParameter.Value.Value = value; }
[11635]42    }
43
[17747]44    protected ISingleObjectiveSolutionContext<BinaryVector> BestSolution {
45      get => BestSolutionResult.Value;
46      set => BestSolutionResult.Value = value;
47    }
48
[11987]49    [StorableConstructor]
[16814]50    protected BinaryVectorProblem(StorableConstructorFlag _) : base(_) { }
[11990]51    [StorableHook(HookType.AfterDeserialization)]
52    private void AfterDeserialization() {
53      RegisterEventHandlers();
54    }
[11635]55
[16814]56    protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner)
[11990]57      : base(original, cloner) {
[17544]58      DimensionRefParameter = cloner.Clone(original.DimensionRefParameter);
[17612]59      BestSolutionResult = cloner.Clone(original.BestSolutionResult);
[11990]60      RegisterEventHandlers();
61    }
62
[16948]63    protected BinaryVectorProblem() : this(new BinaryVectorEncoding() { Length = 10 }) { }
64    protected BinaryVectorProblem(BinaryVectorEncoding encoding) : base(encoding) {
65      EncodingParameter.ReadOnly = true;
[17695]66      EvaluatorParameter.ReadOnly = true;
[17544]67      Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the binary vector problem.", Encoding.LengthParameter));
[17747]68      Results.Add(BestSolutionResult = new Result<ISingleObjectiveSolutionContext<BinaryVector>>("Best Solution", "The best solution found so far."));
[16814]69
[16692]70      Operators.Add(new HammingSimilarityCalculator());
[17620]71      // TODO: These should be added in the SingleObjectiveProblem base class (if they were accessible from there)
[16692]72      Operators.Add(new QualitySimilarityCalculator());
73      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
[16814]74
[16692]75      Parameterize();
[11990]76      RegisterEventHandlers();
77    }
78
[17745]79    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, IRandom random) {
80      base.Analyze(solutionContexts, random);
[17522]81      var best = GetBest(solutionContexts);
[17612]82      if (BestSolution == null || IsBetter(best, BestSolution))
83        BestSolution = best.Clone() as SingleObjectiveSolutionContext<BinaryVector>;
[11996]84    }
[11990]85
[17695]86    protected override sealed void OnEvaluatorChanged() {
87      throw new InvalidOperationException("Evaluator may not change!");
88    }
89
90    protected override sealed void OnEncodingChanged() {
91      throw new InvalidOperationException("Encoding may not change!");
92    }
93
[17620]94    protected override void ParameterizeOperators() {
95      base.ParameterizeOperators();
96      Parameterize();
97    }
98
[16692]99    private void Parameterize() {
[17620]100      // TODO: this is done in base class as well (but operators are added at this level of the hierarchy)
[16692]101      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
[16814]102        similarityCalculator.SolutionVariableName = Encoding.Name;
[16692]103        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
104      }
105    }
[11990]106
107    private void RegisterEventHandlers() {
[17567]108      IntValueParameterChangeHandler.Create(DimensionRefParameter, DimensionOnChanged);
[11990]109    }
110
[17544]111    protected virtual void DimensionOnChanged() { }
[11635]112  }
113}
Note: See TracBrowser for help on using the repository browser.