Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: work in progress

File size: 3.4 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.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Problems.Binary {
33  [Item("Hierararchical If and only If problem (HIFF)", "Genome evaluated in nested subsets to see if each subset contains either all 0s or all 1s.")]
34  [StorableType("8AC6D94E-E7B4-4216-B2CA-8E142E7A1391")]
35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 220)]
36  public class HIFFProblem : BinaryVectorProblem {
37
38    public new int Dimension {
39      get { return base.Dimension; }
40      set { DimensionRefParameter.Value.Value = value; }
41    }
42
43    public HIFFProblem() : base() {
44      Maximization = true;
45      Dimension = 64;
46    }
47
48    // In the GECCO paper, Section 4.1
49    public override ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
50      int[] level = new int[individual.Length];
51      int levelLength = individual.Length;
52
53      // Initialize the level to the current solution
54      for (int i = 0; i < levelLength; i++) {
55        level[i] = Convert.ToInt32(individual[i]);
56      }
57      int power = 1;
58      int nextLength = levelLength / 2;
59      int total = 0;
60      int maximum = 0;
61
62      // Keep going while the next level actual has bits in it
63      while (nextLength > 0) {
64        int[] nextLevel = new int[nextLength];
65        // Construct the next level using the current level
66        for (int i = 0; i + 1 < levelLength; i += 2) {
67          if (level[i] == level[i + 1] && level[i] != -1) {
68            // Score points for a correct setting at this level
69            total += power;
70            nextLevel[i / 2] = level[i];
71          } else {
72            nextLevel[i / 2] = -1;
73          }
74          // Keep track of the maximum possible score
75          maximum += power;
76        }
77        level = nextLevel;
78        levelLength = nextLength;
79        nextLength = levelLength / 2;
80        power *= 2;
81      }
82
83      // Convert to percentage of total
84      var quality = (double)total / maximum;
85      return new SingleObjectiveEvaluationResult(quality);
86    }
87
88    [StorableConstructor]
89    protected HIFFProblem(StorableConstructorFlag _) : base(_) { }
90    protected HIFFProblem(HIFFProblem original, Cloner cloner)
91      : base(original, cloner) {
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new HIFFProblem(this, cloner);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.