Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16824 was 16814, checked in by mkommend, 6 years ago

#2521: Adapted binary problems and moved to abstract base class to encoding.

File size: 3.1 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29
30namespace HeuristicLab.Problems.Binary {
31  [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.")]
32  [StorableType("8AC6D94E-E7B4-4216-B2CA-8E142E7A1391")]
33  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 220)]
34  public class HIFFProblem : BinaryVectorProblem {
35    [StorableConstructor]
36    protected HIFFProblem(StorableConstructorFlag _) : base(_) { }
37    protected HIFFProblem(HIFFProblem original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new HIFFProblem(this, cloner);
42    }
43
44    public override bool Maximization {
45      get { return true; }
46    }
47
48    public HIFFProblem() : base() {
49      Encoding.Length = 64;
50    }
51    // In the GECCO paper, Section 4.1
52    public override double Evaluate(BinaryVector individual, IRandom random) {
53      int[] level = new int[individual.Length];
54      int levelLength = individual.Length;
55
56      // Initialize the level to the current solution
57      for (int i = 0; i < levelLength; i++) {
58        level[i] = Convert.ToInt32(individual[i]);
59      }
60      int power = 1;
61      int nextLength = levelLength / 2;
62      int total = 0;
63      int maximum = 0;
64
65      // Keep going while the next level actual has bits in it
66      while (nextLength > 0) {
67        int[] nextLevel = new int[nextLength];
68        // Construct the next level using the current level
69        for (int i = 0; i + 1 < levelLength; i += 2) {
70          if (level[i] == level[i + 1] && level[i] != -1) {
71            // Score points for a correct setting at this level
72            total += power;
73            nextLevel[i / 2] = level[i];
74          } else {
75            nextLevel[i / 2] = -1;
76          }
77          // Keep track of the maximum possible score
78          maximum += power;
79        }
80        level = nextLevel;
81        levelLength = nextLength;
82        nextLength = levelLength / 2;
83        power *= 2;
84      }
85
86      // Convert to percentage of total
87      return (double)total / maximum;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.