Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2898_GeneralizedAdditiveModels/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs @ 16219

Last change on this file since 16219 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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