Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs @ 17567

Last change on this file since 17567 was 17567, checked in by abeham, 4 years ago

#2521: work in progress

File size: 4.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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 System.Threading;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Problems.Binary {
35  [Item("Deceptive Trap Problem", "Genome encodes completely separable blocks, where each block is fully deceptive.")]
36  [StorableType("399FFE01-2B76-4DBF-B363-8BB65FE95A5D")]
37  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 230)]
38  public class DeceptiveTrapProblem : BinaryVectorProblem {
39    [Storable] public IFixedValueParameter<IntValue> TrapSizeParameter { get; private set; }
40    [Storable] public IFixedValueParameter<IntValue> NumberOfTrapsParameter { get; private set; }
41
42    public new int Dimension {
43      get { return base.Dimension; }
44      set { DimensionRefParameter.Value.Value = value; }
45    }
46
47    public int TrapSize {
48      get { return TrapSizeParameter.Value.Value; }
49      set { TrapSizeParameter.Value.Value = value; }
50    }
51
52    public int NumberOfTraps {
53      get { return NumberOfTrapsParameter.Value.Value; }
54      set { NumberOfTrapsParameter.Value.Value = value; }
55    }
56
57    protected virtual int TrapMaximum {
58      get { return TrapSize; }
59    }
60
61    public DeceptiveTrapProblem() : base() {
62      Maximization = true;
63      Parameters.Add(TrapSizeParameter = new FixedValueParameter<IntValue>("Trap Size", "", new IntValue(7)));
64      Parameters.Add(NumberOfTrapsParameter = new FixedValueParameter<IntValue>("Number of Traps", "", new IntValue(7)));
65      DimensionRefParameter.Value = new IntValue(TrapSize * NumberOfTraps);
66
67      RegisterEventHandlers();
68    }
69
70    // In the GECCO paper, calculates Equation 3
71    protected virtual int Score(BinaryVector individual, int trapIndex, int trapSize) {
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 ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
86      if (individual.Length != Dimension) throw new ArgumentException("The individual has not the correct length.");
87      int total = 0;
88      var trapSize = TrapSize;
89      for (int i = 0; i < individual.Length; i += trapSize) {
90        total += Score(individual, i, trapSize);
91      }
92      var quality =  (double)(total * trapSize) / (TrapMaximum * individual.Length);
93      return new SingleObjectiveEvaluationResult(quality);
94    }
95
96    [StorableConstructor]
97    protected DeceptiveTrapProblem(StorableConstructorFlag _) : base(_) { }
98    [StorableHook(HookType.AfterDeserialization)]
99    private void AfterDeserialization() {
100      RegisterEventHandlers();
101    }
102    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
103      : base(original, cloner) {
104      TrapSizeParameter = cloner.Clone(original.TrapSizeParameter);
105      NumberOfTrapsParameter = cloner.Clone(original.NumberOfTrapsParameter);
106
107      RegisterEventHandlers();
108    }
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new DeceptiveTrapProblem(this, cloner);
111    }
112
113    private void RegisterEventHandlers() {
114      IntValueParameterChangeHandler.Create(TrapSizeParameter, TrapSizeOnChanged);
115      IntValueParameterChangeHandler.Create(NumberOfTrapsParameter, NumberOfTrapsOnChanged);
116    }
117
118    protected virtual void TrapSizeOnChanged() {
119      Dimension = TrapSize * NumberOfTraps;
120    }
121
122    private void NumberOfTrapsOnChanged() {
123      Dimension = TrapSize * NumberOfTraps;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.