Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282: Changed creatable attribute and project files in P3.

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