Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/DeceptiveTrapProblem.cs @ 11672

Last change on this file since 11672 was 11672, checked in by bgoldman, 9 years ago

#2282 Commenting, added some basic unit tests for P3.

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
31  [Item("Deceptive Trap Problem", "Genome encodes completely separable blocks, where each block is fully deceptive.")]
32  [StorableClass]
33  [Creatable("Parameterless Population Pyramid")]
34  // In the GECCO paper, Section 4.1
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) {
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      for (int i = 0; i < individual.Length; i += TrapSize) {
89        total += Score(individual, i);
90      }
91      return (double)(total * TrapSize) / (TrapMaximum * individual.Length);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.