1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 |
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.GeneticProgramming.Boolean {
|
---|
36 | [Item("Even Parity Problem", "The Boolean even parity genetic programming problem. See Koza, 1992, page 529 section 20.2 Symbolic Regression of Even-Parity Functions")]
|
---|
37 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 900)]
|
---|
38 | [StorableType("76D6001D-135F-45FB-BC79-061EDAEE33A9")]
|
---|
39 | public sealed class EvenParityProblem : SymbolicExpressionTreeProblem {
|
---|
40 |
|
---|
41 | #region parameter names
|
---|
42 | private const string NumberOfBitsParameterName = "NumberOfBits";
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Parameter Properties
|
---|
46 | public IFixedValueParameter<IntValue> NumberOfBitsParameter {
|
---|
47 | get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfBitsParameterName]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Properties
|
---|
52 | public int NumberOfBits {
|
---|
53 | get { return NumberOfBitsParameter.Value.Value; }
|
---|
54 | set { NumberOfBitsParameter.Value.Value = value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region item cloning and persistence
|
---|
59 | // persistence
|
---|
60 | [StorableConstructor]
|
---|
61 | private EvenParityProblem(StorableConstructorFlag _) : base(_) { }
|
---|
62 | [StorableHook(HookType.AfterDeserialization)]
|
---|
63 | private void AfterDeserialization() {
|
---|
64 | RegisterEventHandlers();
|
---|
65 | }
|
---|
66 |
|
---|
67 | // cloning
|
---|
68 | private EvenParityProblem(EvenParityProblem original, Cloner cloner)
|
---|
69 | : base(original, cloner) {
|
---|
70 | RegisterEventHandlers();
|
---|
71 | }
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
73 | return new EvenParityProblem(this, cloner);
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | public EvenParityProblem()
|
---|
78 | : base(new SymbolicExpressionTreeEncoding()) {
|
---|
79 | Maximization = true;
|
---|
80 | Parameters.Add(new FixedValueParameter<IntValue>(NumberOfBitsParameterName, "The number of bits for the input parameter for the even parity function", new IntValue(4)));
|
---|
81 |
|
---|
82 | Encoding.TreeLength = 100;
|
---|
83 | Encoding.TreeDepth = 17;
|
---|
84 |
|
---|
85 | UpdateGrammar();
|
---|
86 | RegisterEventHandlers();
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void UpdateGrammar() {
|
---|
90 | var g = new SimpleSymbolicExpressionGrammar();
|
---|
91 | g.AddSymbols(new[] { "AND", "OR", "NAND", "NOR" }, 2, 2); // see Koza, 1992, page 529 section 20.2 Symbolic Regression of Even-Parity Functions
|
---|
92 |
|
---|
93 | // add one terminal symbol for each bit
|
---|
94 | for (int i = 0; i < NumberOfBits; i++)
|
---|
95 | g.AddTerminalSymbol(string.Format("{0}", i));
|
---|
96 |
|
---|
97 | Encoding.Grammar = g;
|
---|
98 | Encoding.GrammarParameter.ReadOnly = GrammarRefParameter.ReadOnly = true;
|
---|
99 |
|
---|
100 | BestKnownQuality = Math.Pow(2, NumberOfBits); // this is a benchmark problem (the best achievable quality is known for a given number of bits)
|
---|
101 | BestKnownQualityParameter.ReadOnly = true;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | public override ISingleObjectiveEvaluationResult Evaluate(ISymbolicExpressionTree tree, IRandom random, CancellationToken cancellationToken) {
|
---|
106 | if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero.");
|
---|
107 | if (NumberOfBits > 10) throw new NotSupportedException("Even parity does not support problems with number of bits > 10.");
|
---|
108 | var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits));
|
---|
109 | var targets = bs.Select(b => CalcTarget(b, NumberOfBits));
|
---|
110 | var pred = Interpret(tree, bs);
|
---|
111 | var quality = targets.Zip(pred, (t, p) => t == p ? 1 : 0).Sum(); // count number of correct predictions
|
---|
112 |
|
---|
113 | return new SingleObjectiveEvaluationResult(quality);
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static bool CalcTarget(int b, int numBits) {
|
---|
117 | bool res = GetBits(b, 0);
|
---|
118 | for (byte i = 1; i < numBits; i++)
|
---|
119 | res = res ^ GetBits(b, i); // XOR
|
---|
120 | return res;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private static IEnumerable<bool> Interpret(ISymbolicExpressionTree tree, IEnumerable<int> bs) {
|
---|
124 | // skip programRoot and startSymbol
|
---|
125 | return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), bs);
|
---|
126 | }
|
---|
127 |
|
---|
128 | private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs) {
|
---|
129 | Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval =
|
---|
130 | (left, right, f) => InterpretRec(left, bs).Zip(InterpretRec(right, bs), f);
|
---|
131 |
|
---|
132 | switch (node.Symbol.Name) {
|
---|
133 | case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
|
---|
134 | case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
|
---|
135 | case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));
|
---|
136 | case "NOR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));
|
---|
137 | default: {
|
---|
138 | byte bitPos;
|
---|
139 | if (byte.TryParse(node.Symbol.Name, out bitPos)) {
|
---|
140 | return bs.Select(b => GetBits(b, bitPos));
|
---|
141 | } else throw new NotSupportedException(string.Format("Found unexpected symbol {0}", node.Symbol.Name));
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private static bool GetBits(int b, byte bitPos) {
|
---|
147 | return (b & (1 << bitPos)) != 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | #region events
|
---|
151 | private void RegisterEventHandlers() {
|
---|
152 | NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 | }
|
---|
156 | }
|
---|