Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: adapted readonly of reference parameters

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      Encoding.LengthParameter.ReadOnly = DimensionRefParameter.ReadOnly = true;
61      Dimension = TrapSize * NumberOfTraps;
62
63      RegisterEventHandlers();
64    }
65
66    // In the GECCO paper, calculates Equation 3
67    protected virtual int Score(BinaryVector individual, int trapIndex, int trapSize) {
68      int result = 0;
69      // count number of bits in trap set to 1
70      for (int index = trapIndex; index < trapIndex + trapSize; index++) {
71        if (individual[index]) result++;
72      }
73
74      // Make it deceptive
75      if (result < trapSize) {
76        result = trapSize - result - 1;
77      }
78      return result;
79    }
80
81    public override ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
82      if (individual.Length != Dimension) throw new ArgumentException("The individual has not the correct length.");
83      int total = 0;
84      var trapSize = TrapSize;
85      for (int i = 0; i < individual.Length; i += trapSize) {
86        total += Score(individual, i, trapSize);
87      }
88      var quality =  (double)(total * trapSize) / (TrapMaximum * individual.Length);
89      return new SingleObjectiveEvaluationResult(quality);
90    }
91
92    [StorableConstructor]
93    protected DeceptiveTrapProblem(StorableConstructorFlag _) : base(_) { }
94    [StorableHook(HookType.AfterDeserialization)]
95    private void AfterDeserialization() {
96      RegisterEventHandlers();
97    }
98    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
99      : base(original, cloner) {
100      TrapSizeParameter = cloner.Clone(original.TrapSizeParameter);
101      NumberOfTrapsParameter = cloner.Clone(original.NumberOfTrapsParameter);
102
103      RegisterEventHandlers();
104    }
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new DeceptiveTrapProblem(this, cloner);
107    }
108
109    private void RegisterEventHandlers() {
110      IntValueParameterChangeHandler.Create(TrapSizeParameter, TrapSizeOnChanged);
111      IntValueParameterChangeHandler.Create(NumberOfTrapsParameter, NumberOfTrapsOnChanged);
112    }
113
114    protected virtual void TrapSizeOnChanged() {
115      Dimension = TrapSize * NumberOfTraps;
116    }
117
118    private void NumberOfTrapsOnChanged() {
119      Dimension = TrapSize * NumberOfTraps;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.