Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: worked on refactoring, worked a lot on binary encoding / problems

File size: 3.3 KB
RevLine 
[11666]1#region License Information
[11956]2
[11666]3/* HeuristicLab
[17226]4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11666]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 */
[11956]21
[11666]22#endregion
23
24using System;
[17320]25using System.Threading;
[16814]26using HEAL.Attic;
[11666]27using HeuristicLab.Common;
28using HeuristicLab.Core;
[17544]29using HeuristicLab.Data;
[11987]30using HeuristicLab.Encodings.BinaryVectorEncoding;
[17382]31using HeuristicLab.Optimization;
[11666]32
[11987]33namespace HeuristicLab.Problems.Binary {
[13173]34  [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.")]
[16723]35  [StorableType("8AC6D94E-E7B4-4216-B2CA-8E142E7A1391")]
[12504]36  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 220)]
[16814]37  public class HIFFProblem : BinaryVectorProblem {
[11666]38
[16814]39    public HIFFProblem() : base() {
[17270]40      Maximization = true;
[17544]41      DimensionRefParameter.ForceValue(new IntValue(64, @readonly: false));
[11666]42    }
[17544]43
[11838]44    // In the GECCO paper, Section 4.1
[17382]45    public override ISingleObjectiveEvaluationResult Evaluate(BinaryVector individual, IRandom random, CancellationToken cancellationToken) {
[11666]46      int[] level = new int[individual.Length];
47      int levelLength = individual.Length;
48
49      // Initialize the level to the current solution
50      for (int i = 0; i < levelLength; i++) {
51        level[i] = Convert.ToInt32(individual[i]);
52      }
53      int power = 1;
54      int nextLength = levelLength / 2;
55      int total = 0;
56      int maximum = 0;
57
58      // Keep going while the next level actual has bits in it
59      while (nextLength > 0) {
60        int[] nextLevel = new int[nextLength];
61        // Construct the next level using the current level
62        for (int i = 0; i + 1 < levelLength; i += 2) {
63          if (level[i] == level[i + 1] && level[i] != -1) {
64            // Score points for a correct setting at this level
65            total += power;
66            nextLevel[i / 2] = level[i];
67          } else {
68            nextLevel[i / 2] = -1;
69          }
70          // Keep track of the maximum possible score
71          maximum += power;
72        }
73        level = nextLevel;
74        levelLength = nextLength;
75        nextLength = levelLength / 2;
76        power *= 2;
77      }
78
79      // Convert to percentage of total
[17382]80      var quality = (double)total / maximum;
81      return new SingleObjectiveEvaluationResult(quality);
[11666]82    }
[17544]83
84    [StorableConstructor]
85    protected HIFFProblem(StorableConstructorFlag _) : base(_) { }
86    protected HIFFProblem(HIFFProblem original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new HIFFProblem(this, cloner);
91    }
[11666]92  }
93}
Note: See TracBrowser for help on using the repository browser.