Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17382 was 17382, checked in by mkommend, 4 years ago

#2521: Refactored single-objective problems to use EvaluationResult instead of double as return type from Evaluate.

File size: 3.5 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    [StorableConstructor]
40    protected DeceptiveTrapProblem(StorableConstructorFlag _) : base(_) { }
41    protected DeceptiveTrapProblem(DeceptiveTrapProblem original, Cloner cloner)
42      : base(original, cloner) {
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new DeceptiveTrapProblem(this, cloner);
46    }
47
48    private const string TrapSizeParameterName = "Trap Size";
49
50    public IFixedValueParameter<IntValue> TrapSizeParameter {
51      get { return (IFixedValueParameter<IntValue>)Parameters[TrapSizeParameterName]; }
52    }
53
54    public int TrapSize {
55      get { return TrapSizeParameter.Value.Value; }
56      set { TrapSizeParameter.Value.Value = value; }
57    }
58
59    protected virtual int TrapMaximum {
60      get { return TrapSize; }
61    }
62
63    public DeceptiveTrapProblem() : base() {
64      Maximization = true;
65      Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7)));
66      Encoding.Length = 49;
67    }
68
69    // In the GECCO paper, calculates Equation 3
70    protected virtual int Score(BinaryVector individual, int trapIndex, int trapSize) {
71      int result = 0;
72      // count number of bits in trap set to 1
73      for (int index = trapIndex; index < trapIndex + trapSize; index++) {
74        if (individual[index]) result++;
75      }
76
77      // Make it deceptive
78      if (result < trapSize) {
79        result = trapSize - result - 1;
80      }
81      return result;
82    }
83
84    public override ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
85      if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length.");
86      int total = 0;
87      var trapSize = TrapSize;
88      for (int i = 0; i < individual.Length; i += trapSize) {
89        total += Score(individual, i, trapSize);
90      }
91      var quality =  (double)(total * trapSize) / (TrapMaximum * individual.Length);
92      return new SingleObjectiveEvaluationResult(quality);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.