Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282 Evaluation counting, results and graphing, HIFF problem.

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 System.Linq;
25using System.Text;
26using System.Threading.Tasks;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid.Problems {
32  [Item("Hierararchical If and only If problem", "Genome evaluated in nested subsets to see if each subset contains either all 0s or all 1s.")]
33  [StorableClass]
34  [Creatable("Parameterless Population Pyramid")]
35  public class HIFFProblem : BinaryVectorProblem {
36    [StorableConstructor]
37    protected HIFFProblem(bool deserializing) : base(deserializing) { }
38    protected HIFFProblem(HIFFProblem original, Cloner cloner)
39      : base(original, cloner) {
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new HIFFProblem(this, cloner);
43    }
44
45    public override bool Maximization {
46      get { return true; }
47    }
48
49    public HIFFProblem() : base() {
50      Length = 32;
51    }
52
53    public override double Evaluate(bool[] individual) {
54      int[] level = new int[individual.Length];
55      int levelLength = individual.Length;
56
57      // Initialize the level to the current solution
58      for (int i = 0; i < levelLength; i++) {
59        level[i] = Convert.ToInt32(individual[i]);
60      }
61      int power = 1;
62      int nextLength = levelLength / 2;
63      int total = 0;
64      int maximum = 0;
65
66      // Keep going while the next level actual has bits in it
67      while (nextLength > 0) {
68        int[] nextLevel = new int[nextLength];
69        // Construct the next level using the current level
70        for (int i = 0; i + 1 < levelLength; i += 2) {
71          if (level[i] == level[i + 1] && level[i] != -1) {
72            // Score points for a correct setting at this level
73            total += power;
74            nextLevel[i / 2] = level[i];
75          } else {
76            nextLevel[i / 2] = -1;
77          }
78          // Keep track of the maximum possible score
79          maximum += power;
80        }
81        level = nextLevel;
82        levelLength = nextLength;
83        nextLength = levelLength / 2;
84        power *= 2;
85      }
86
87      // Convert to percentage of total
88      return (double)total / maximum;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.