Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs @ 15008

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

#2526: Updated year of copyrights in license headers

File size: 3.6 KB
RevLine 
[11635]1#region License Information
[11956]2
[11635]3/* HeuristicLab
[14186]4 * Copyright (C) 2002-2016 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
[12005]24using System;
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 {
[11635]35  [StorableClass]
[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
[12005]42    private IFixedValueParameter<IntValue> LengthParameter {
43      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
44    }
45
[11987]46    [StorableConstructor]
47    protected BinaryProblem(bool deserializing) : base(deserializing) { }
[12005]48    [StorableHook(HookType.AfterDeserialization)]
49    private void AfterDeserialization() {
50      RegisterEventHandlers();
51    }
[11635]52
[12005]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
[12005]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
[12005]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) { }
[11635]97  }
98}
Note: See TracBrowser for help on using the repository browser.