Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs @ 16724

Last change on this file since 16724 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 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.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.Binary {
35  [StorableClass]
36  public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
37    public virtual int Length {
38      get { return Encoding.Length; }
39      set { Encoding.Length = value; }
40    }
41
42    private IFixedValueParameter<IntValue> LengthParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
44    }
45
46    [StorableConstructor]
47    protected BinaryProblem(bool deserializing) : base(deserializing) { }
48    [StorableHook(HookType.AfterDeserialization)]
49    private void AfterDeserialization() {
50      RegisterEventHandlers();
51    }
52
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
66    public virtual bool IsBetter(double quality, double bestQuality) {
67      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
68    }
69
70    public abstract double Evaluate(BinaryVector vector, IRandom random);
71    public sealed override double Evaluate(Individual individual, IRandom random) {
72      return Evaluate(individual.BinaryVector(), random);
73    }
74
75    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
76      base.Analyze(individuals, qualities, results, random);
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
80      if (!results.ContainsKey("Best Solution")) {
81        results.Add(new Result("Best Solution", typeof(BinaryVector)));
82      }
83      results["Best Solution"].Value = (IItem)best.BinaryVector().Clone();
84    }
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) { }
97  }
98}
Note: See TracBrowser for help on using the repository browser.