Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs @ 18050

Last change on this file since 18050 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.3 KB
RevLine 
[11666]1#region License Information
[11956]2
[11666]3/* HeuristicLab
[17181]4 * Copyright (C) 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;
[17097]30using HEAL.Attic;
[11666]31
[11987]32namespace HeuristicLab.Problems.Binary {
[11666]33  [Item("Deceptive Trap Problem", "Genome encodes completely separable blocks, where each block is fully deceptive.")]
[17097]34  [StorableType("399FFE01-2B76-4DBF-B363-8BB65FE95A5D")]
[12708]35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 230)]
[11987]36  public class DeceptiveTrapProblem : BinaryProblem {
[11666]37    [StorableConstructor]
[17097]38    protected DeceptiveTrapProblem(StorableConstructorFlag _) : base(_) { }
[11666]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)));
[12005]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.