Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Rectored problems and encodings.

File size: 3.2 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 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 : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
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 override void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
67      base.Analyze(individuals, qualities, results, random);
68      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
69      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
70
71      if (!results.ContainsKey("Best Solution")) {
72        results.Add(new Result("Best Solution", typeof(BinaryVector)));
73      }
74      results["Best Solution"].Value = (IItem)best.Clone();
75    }
76
77    protected override void OnEncodingChanged() {
78      base.OnEncodingChanged();
79      Encoding.LengthParameter = LengthParameter;
80    }
81
82
83    private void RegisterEventHandlers() {
84      LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
85    }
86
87    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
88  }
89}
Note: See TracBrowser for help on using the repository browser.