Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Reverse behavior, parameters are not readonly by default, can be readonly if computed automatically

File size: 4.6 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 int TrapSize {
43      get { return TrapSizeParameter.Value.Value; }
44      set { TrapSizeParameter.Value.Value = value; }
45    }
46
47    public int NumberOfTraps {
48      get { return NumberOfTrapsParameter.Value.Value; }
49      set { NumberOfTrapsParameter.Value.Value = value; }
50    }
51
52    protected virtual int TrapMaximum {
53      get { return TrapSize; }
54    }
55
56    public DeceptiveTrapProblem() : base() {
57      Maximization = true;
58      Parameters.Add(TrapSizeParameter = new FixedValueParameter<IntValue>("Trap Size", "", new IntValue(7)));
59      Parameters.Add(NumberOfTrapsParameter = new FixedValueParameter<IntValue>("Number of Traps", "", new IntValue(7)));
60      DimensionRefParameter.ForceValue(new IntValue(TrapSize * NumberOfTraps, @readonly: true));
61
62      RegisterEventHandlers();
63    }
64
65    // In the GECCO paper, calculates Equation 3
66    protected virtual int Score(BinaryVector individual, int trapIndex, int trapSize) {
67      int result = 0;
68      // count number of bits in trap set to 1
69      for (int index = trapIndex; index < trapIndex + trapSize; index++) {
70        if (individual[index]) result++;
71      }
72
73      // Make it deceptive
74      if (result < trapSize) {
75        result = trapSize - result - 1;
76      }
77      return result;
78    }
79
80    public override ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
81      if (individual.Length != Dimension) throw new ArgumentException("The individual has not the correct length.");
82      int total = 0;
83      var trapSize = TrapSize;
84      for (int i = 0; i < individual.Length; i += trapSize) {
85        total += Score(individual, i, trapSize);
86      }
87      var quality =  (double)(total * trapSize) / (TrapMaximum * individual.Length);
88      return new SingleObjectiveEvaluationResult(quality);
89    }
90
91    [StorableConstructor]
92    protected DeceptiveTrapProblem(StorableConstructorFlag _) : base(_) { }
93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      RegisterEventHandlers();
96    }
97    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
98      : base(original, cloner) {
99      TrapSizeParameter = cloner.Clone(original.TrapSizeParameter);
100      NumberOfTrapsParameter = cloner.Clone(original.NumberOfTrapsParameter);
101
102      RegisterEventHandlers();
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new DeceptiveTrapProblem(this, cloner);
106    }
107
108    private void RegisterEventHandlers() {
109      TrapSizeParameter.Value.ValueChanged += TrapSizeOnChanged;
110      NumberOfTrapsParameter.Value.ValueChanged += NumberOfTrapsOnChanged;
111    }
112
113    protected virtual void TrapSizeOnChanged(object sender, EventArgs e) {
114      Dimension = TrapSize * NumberOfTraps;
115    }
116
117    private void NumberOfTrapsOnChanged(object sender, EventArgs e) {
118      Dimension = TrapSize * NumberOfTraps;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.