Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/HIFFProblem.cs @ 11672

Last change on this file since 11672 was 11672, checked in by bgoldman, 9 years ago

#2282 Commenting, added some basic unit tests for P3.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
29  [Item("Hierararchical If and only If problem", "Genome evaluated in nested subsets to see if each subset contains either all 0s or all 1s.")]
30  [StorableClass]
31  [Creatable("Parameterless Population Pyramid")]
32  // In the GECCO paper, Section 4.1
33  public class HIFFProblem : BinaryVectorProblem {
34    [StorableConstructor]
35    protected HIFFProblem(bool deserializing) : base(deserializing) { }
36    protected HIFFProblem(HIFFProblem original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new HIFFProblem(this, cloner);
41    }
42
43    public override bool Maximization {
44      get { return true; }
45    }
46
47    public HIFFProblem() : base() {
48      Length = 64;
49    }
50
51    public override double Evaluate(bool[] individual) {
52      int[] level = new int[individual.Length];
53      int levelLength = individual.Length;
54
55      // Initialize the level to the current solution
56      for (int i = 0; i < levelLength; i++) {
57        level[i] = Convert.ToInt32(individual[i]);
58      }
59      int power = 1;
60      int nextLength = levelLength / 2;
61      int total = 0;
62      int maximum = 0;
63
64      // Keep going while the next level actual has bits in it
65      while (nextLength > 0) {
66        int[] nextLevel = new int[nextLength];
67        // Construct the next level using the current level
68        for (int i = 0; i + 1 < levelLength; i += 2) {
69          if (level[i] == level[i + 1] && level[i] != -1) {
70            // Score points for a correct setting at this level
71            total += power;
72            nextLevel[i / 2] = level[i];
73          } else {
74            nextLevel[i / 2] = -1;
75          }
76          // Keep track of the maximum possible score
77          maximum += power;
78        }
79        level = nextLevel;
80        levelLength = nextLength;
81        nextLength = levelLength / 2;
82        power *= 2;
83      }
84
85      // Convert to percentage of total
86      return (double)total / maximum;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.