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 |
|
---|
24 | using System;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace 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 | }
|
---|