Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinaryVector/3.3/DeceptiveTrapProblem.cs @ 11956

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

#2282: Created plugin for binary vector problems.

File size: 3.5 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.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.BinaryVector {
32  // This code is based off the publication
33  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
34  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
35  [Item("Deceptive Trap Problem", "Genome encodes completely separable blocks, where each block is fully deceptive.")]
36  [StorableClass]
37  [Creatable("Parameterless Population Pyramid")]
38  public class DeceptiveTrapProblem : BinaryVectorProblem {
39    [StorableConstructor]
40    protected DeceptiveTrapProblem(bool deserializing) : base(deserializing) { }
41    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
42      : base(original, cloner) {
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new DeceptiveTrapProblem(this, cloner);
46    }
47
48    public override bool Maximization {
49      get { return true; }
50    }
51
52    private const string TrapSizeParameterName = "Trap Size";
53
54    public IFixedValueParameter<IntValue> TrapSizeParameter {
55      get { return (IFixedValueParameter<IntValue>)Parameters[TrapSizeParameterName]; }
56    }
57
58    public int TrapSize {
59      get { return TrapSizeParameter.Value.Value; }
60      set { TrapSizeParameter.Value.Value = value; }
61    }
62
63    protected virtual int TrapMaximum {
64      get { return TrapSize; }
65    }
66
67    public DeceptiveTrapProblem()
68      : base() {
69      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
70      Length = 49;
71    }
72
73    // In the GECCO paper, calculates Equation 3
74    protected virtual int Score(bool[] individual, int trapIndex, int trapSize) {
75      int result = 0;
76      // count number of bits in trap set to 1
77      for (int index = trapIndex; index < trapIndex + trapSize; index++) {
78        if (individual[index]) result++;
79      }
80
81      // Make it deceptive
82      if (result < trapSize) {
83        result = trapSize - result - 1;
84      }
85      return result;
86    }
87
88    public override double Evaluate(bool[] individual) {
89      if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length.");
90      int total = 0;
91      var trapSize = TrapSize;
92      for (int i = 0; i < individual.Length; i += trapSize) {
93        total += Score(individual, i, trapSize);
94      }
95      return (double)(total * trapSize) / (TrapMaximum * individual.Length);
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.