Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282 Code cleanup, added Deceptive Step Trap problem.

File size: 3.2 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  public class DeceptiveTrapProblem : BinaryVectorProblem {
35    [StorableConstructor]
36    protected DeceptiveTrapProblem(bool deserializing) : base(deserializing) { }
37    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new DeceptiveTrapProblem(this, cloner);
42    }
43
44    public override bool Maximization {
45      get { return true; }
46    }
47
48    private const string TrapSizeParameterName = "Trap Size";
49
50    public IFixedValueParameter<IntValue> TrapSizeParameter {
51      get { return (IFixedValueParameter<IntValue>)Parameters[TrapSizeParameterName]; }
52    }
53
54    public int TrapSize {
55      get { return TrapSizeParameter.Value.Value; }
56      set { TrapSizeParameter.Value.Value = value; }
57    }
58
59    protected virtual int TrapMaximum {
60      get { return TrapSize;  }
61    }
62
63    public DeceptiveTrapProblem()
64      : base() {
65      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
66      Length = 49;
67    }
68
69    protected virtual int Score(bool[] individual, int trapIndex) {
70      int result = 0;
71      for (int index = trapIndex; index < trapIndex + TrapSize; index++) {
72        if (individual[index]) result++;
73      }
74
75      // Make it deceptive
76      if (result < TrapSize) {
77        result = TrapSize - result - 1;
78      }
79      return result;
80    }
81
82    public override double Evaluate(bool[] individual) {
83      if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length.");
84      int total = 0;
85      for (int i = 0; i < individual.Length; i += TrapSize) {
86        total += Score(individual, i);
87      }
88      return (double)(total * TrapSize) / (TrapMaximum * individual.Length);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.