Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs @ 15027

Last change on this file since 15027 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.4 KB
RevLine 
[11666]1#region License Information
[11956]2
[11666]3/* HeuristicLab
[14185]4 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11956]5 *
[11666]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 */
[11956]21
[11666]22#endregion
23
24using System;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[11987]28using HeuristicLab.Encodings.BinaryVectorEncoding;
[11666]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[11987]32namespace HeuristicLab.Problems.Binary {
[11666]33  [Item("Deceptive Trap Problem", "Genome encodes completely separable blocks, where each block is fully deceptive.")]
34  [StorableClass]
[12504]35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 230)]
[11987]36  public class DeceptiveTrapProblem : BinaryProblem {
[11666]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    }
[11669]60
61    protected virtual int TrapMaximum {
[11674]62      get { return TrapSize; }
[11669]63    }
64
[11666]65    public DeceptiveTrapProblem()
66      : base() {
[11669]67      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
[11996]68      Encoding.Length = 49;
[11666]69    }
70
[11672]71    // In the GECCO paper, calculates Equation 3
[11987]72    protected virtual int Score(BinaryVector individual, int trapIndex, int trapSize) {
[11669]73      int result = 0;
[11672]74      // count number of bits in trap set to 1
[11674]75      for (int index = trapIndex; index < trapIndex + trapSize; index++) {
[11669]76        if (individual[index]) result++;
77      }
78
79      // Make it deceptive
[11674]80      if (result < trapSize) {
81        result = trapSize - result - 1;
[11669]82      }
83      return result;
84    }
85
[11987]86    public override double Evaluate(BinaryVector individual, IRandom random) {
[11666]87      if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length.");
88      int total = 0;
[11674]89      var trapSize = TrapSize;
90      for (int i = 0; i < individual.Length; i += trapSize) {
91        total += Score(individual, i, trapSize);
[11666]92      }
[11674]93      return (double)(total * trapSize) / (TrapMaximum * individual.Length);
[11666]94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.