Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs @ 11990

Last change on this file since 11990 was 11990, checked in by mkommend, 9 years ago

#2282: Added best known quality parameter to single-objective basic problem and new one-max problem.

File size: 3.0 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Binary {
34  [StorableClass]
35  public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
36    public virtual int Length {
37      get { return Encoding.Length; }
38      set { Encoding.Length = value; }
39    }
40
41    private IFixedValueParameter<IntValue> LengthParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
43    }
44
45    [StorableConstructor]
46    protected BinaryProblem(bool deserializing) : base(deserializing) { }
47    [StorableHook(HookType.AfterDeserialization)]
48    private void AfterDeserialization() {
49      RegisterEventHandlers();
50    }
51
52    protected BinaryProblem(BinaryProblem original, Cloner cloner)
53      : base(original, cloner) {
54      RegisterEventHandlers();
55    }
56
57    protected BinaryProblem()
58      : base() {
59      var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
60      Parameters.Add(lengthParameter);
61      Encoding.LengthParameter = lengthParameter;
62      RegisterEventHandlers();
63    }
64
65    public virtual bool IsBetter(double quality, double bestQuality) {
66      return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality);
67    }
68
69    public sealed override double Evaluate(Individual individual, IRandom random) {
70      return Evaluate(individual.BinaryVector(), random);
71    }
72
73    public abstract double Evaluate(BinaryVector vector, IRandom random);
74
75    protected override void OnEncodingChanged() {
76      base.OnEncodingChanged();
77      Encoding.LengthParameter = LengthParameter;
78    }
79
80
81    private void RegisterEventHandlers() {
82      LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
83    }
84
85    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
86  }
87}
Note: See TracBrowser for help on using the repository browser.