Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinaryVector/3.3/HIFFProblem.cs @ 11956

Last change on this file since 11956 was 11956, checked in by mkommend, 10 years ago

#2282: Created plugin for binary vector problems.

File size: 3.3 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.BinaryVector {
30  // This code is based off the publication
31  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
32  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
33  [Item("Hierararchical If and only If problem", "Genome evaluated in nested subsets to see if each subset contains either all 0s or all 1s.")]
34  [StorableClass]
35  [Creatable("Parameterless Population Pyramid")]
36  public class HIFFProblem : BinaryVectorProblem {
37    [StorableConstructor]
38    protected HIFFProblem(bool deserializing) : base(deserializing) { }
39    protected HIFFProblem(HIFFProblem original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new HIFFProblem(this, cloner);
44    }
45
46    public override bool Maximization {
47      get { return true; }
48    }
49
50    public HIFFProblem()
51      : base() {
52      Length = 64;
53    }
54    // In the GECCO paper, Section 4.1
55    public override double Evaluate(bool[] individual) {
56      int[] level = new int[individual.Length];
57      int levelLength = individual.Length;
58
59      // Initialize the level to the current solution
60      for (int i = 0; i < levelLength; i++) {
61        level[i] = Convert.ToInt32(individual[i]);
62      }
63      int power = 1;
64      int nextLength = levelLength / 2;
65      int total = 0;
66      int maximum = 0;
67
68      // Keep going while the next level actual has bits in it
69      while (nextLength > 0) {
70        int[] nextLevel = new int[nextLength];
71        // Construct the next level using the current level
72        for (int i = 0; i + 1 < levelLength; i += 2) {
73          if (level[i] == level[i + 1] && level[i] != -1) {
74            // Score points for a correct setting at this level
75            total += power;
76            nextLevel[i / 2] = level[i];
77          } else {
78            nextLevel[i / 2] = -1;
79          }
80          // Keep track of the maximum possible score
81          maximum += power;
82        }
83        level = nextLevel;
84        levelLength = nextLength;
85        nextLength = levelLength / 2;
86        power *= 2;
87      }
88
89      // Convert to percentage of total
90      return (double)total / maximum;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.