Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs @ 16692

Last change on this file since 16692 was 16692, checked in by abeham, 6 years ago

#2521: merged trunk changes up to r15681 into branch (removal of trunk/sources)

File size: 4.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2018 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
24using System;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Problems.Binary {
37  [StorableClass]
38  public abstract class BinaryProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
39    public virtual int Length {
40      get { return Encoding.Length; }
41      set { Encoding.Length = value; }
42    }
43
44    private IFixedValueParameter<IntValue> LengthParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
46    }
47
48    [StorableConstructor]
49    protected BinaryProblem(bool deserializing) : base(deserializing) { }
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      RegisterEventHandlers();
53    }
54
55    protected BinaryProblem(BinaryProblem original, Cloner cloner)
56      : base(original, cloner) {
57      RegisterEventHandlers();
58    }
59
60    protected BinaryProblem()
61      : base() {
62      var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
63      Parameters.Add(lengthParameter);
64      Encoding.LengthParameter = lengthParameter;
65      Operators.Add(new HammingSimilarityCalculator());
66      Operators.Add(new QualitySimilarityCalculator());
67      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
68      Parameterize();
69      RegisterEventHandlers();
70    }
71
72    public override void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
73      base.Analyze(individuals, qualities, results, random);
74      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
75      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
76
77      if (!results.ContainsKey("Best Solution")) {
78        results.Add(new Result("Best Solution", typeof(BinaryVector)));
79      }
80      results["Best Solution"].Value = (IItem)best.Clone();
81    }
82
83    protected override void OnEncodingChanged() {
84      base.OnEncodingChanged();
85      Encoding.LengthParameter = LengthParameter;
86      Parameterize();
87    }
88
89    private void Parameterize() {
90      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
91        // TODO: BinaryVectorParameter is no more part of ISolutionCreator<BinaryVector>
92        similarityCalculator.SolutionVariableName = ((IBinaryVectorSolutionOperator)Encoding.SolutionCreator).BinaryVectorParameter.ActualName;
93        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
94      }
95    }
96
97    private void RegisterEventHandlers() {
98      LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
99    }
100
101    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
102  }
103}
Note: See TracBrowser for help on using the repository browser.