1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2016 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 |
|
---|
24 | using System;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
28 | using HeuristicLab.Persistence;
|
---|
29 |
|
---|
30 | namespace 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("f4b6bc45-781a-4161-9ffe-717b99ab3837")]
|
---|
33 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 220)]
|
---|
34 | public class HIFFProblem : BinaryProblem {
|
---|
35 | [StorableConstructor]
|
---|
36 | protected HIFFProblem(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
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()
|
---|
49 | : base() {
|
---|
50 | Encoding.Length = 64;
|
---|
51 | }
|
---|
52 | // In the GECCO paper, Section 4.1
|
---|
53 | public override double Evaluate(BinaryVector individual, IRandom random) {
|
---|
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 | }
|
---|