Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs @ 15356

Last change on this file since 15356 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 3.7 KB
RevLine 
[13368]1#region License Information
[11956]2
[11635]3/* HeuristicLab
[11956]4 * Copyright (C) 2002-2015 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 */
[11956]21
[11635]22#endregion
23
[11990]24using System;
[11996]25using System.Linq;
[11635]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
[11987]29using HeuristicLab.Encodings.BinaryVectorEncoding;
[11635]30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
[11987]34namespace HeuristicLab.Problems.Binary {
[14711]35  [StorableType("0C1838BD-635D-4EF0-A049-DA1E0CEE7B31")]
[11987]36  public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
37    public virtual int Length {
38      get { return Encoding.Length; }
39      set { Encoding.Length = value; }
[11635]40    }
41
[11990]42    private IFixedValueParameter<IntValue> LengthParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
44    }
45
[11987]46    [StorableConstructor]
47    protected BinaryProblem(bool deserializing) : base(deserializing) { }
[11990]48    [StorableHook(HookType.AfterDeserialization)]
49    private void AfterDeserialization() {
50      RegisterEventHandlers();
51    }
[11635]52
[11990]53    protected BinaryProblem(BinaryProblem original, Cloner cloner)
54      : base(original, cloner) {
55      RegisterEventHandlers();
56    }
57
58    protected BinaryProblem()
59      : base() {
60      var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
61      Parameters.Add(lengthParameter);
62      Encoding.LengthParameter = lengthParameter;
63      RegisterEventHandlers();
64    }
65
[11987]66    public virtual bool IsBetter(double quality, double bestQuality) {
[11640]67      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
68    }
[11635]69
[11996]70    public abstract double Evaluate(BinaryVector vector, IRandom random);
[11987]71    public sealed override double Evaluate(Individual individual, IRandom random) {
72      return Evaluate(individual.BinaryVector(), random);
[11635]73    }
74
[11996]75    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
76      base.Analyze(individuals, qualities, results, random);
[12000]77      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
78      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
79
[11996]80      if (!results.ContainsKey("Best Solution")) {
81        results.Add(new Result("Best Solution", typeof(BinaryVector)));
82      }
[12000]83      results["Best Solution"].Value = (IItem)best.BinaryVector().Clone();
[11996]84    }
[11990]85
86    protected override void OnEncodingChanged() {
87      base.OnEncodingChanged();
88      Encoding.LengthParameter = LengthParameter;
89    }
90
91
92    private void RegisterEventHandlers() {
93      LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
94    }
95
96    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
[11635]97  }
98}
Note: See TracBrowser for help on using the repository browser.