Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Boolean/BooleanTreeInterpreter.cs @ 720

Last change on this file since 720 was 720, checked in by gkronber, 15 years ago

added quick (untested) implementation for #340 (Plugin for genetic programming for boolean logic).

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 HeuristicLab.DataAnalysis;
27using HeuristicLab.Core;
28using System.Xml;
29using System.Diagnostics;
30using HeuristicLab.Data;
31
32namespace HeuristicLab.GP.Boolean {
33  internal class BooleanTreeInterpreter {
34    private const double EPSILON = 0.00001;
35    private Dataset dataset;
36    private IFunctionTree tree;
37    private int targetVariable;
38    private int currentRow;
39
40    public void Reset(Dataset dataset, IFunctionTree tree, int targetVariable) {
41      this.dataset = dataset;
42      this.tree = tree;
43      this.targetVariable = targetVariable;
44    }
45
46    internal int GetNumberMatchingInstances(int start, int end) {
47      int matchingInstances = 0;
48      for(int i = start; i < end; i++) {
49        currentRow = i;
50        int result = Step(tree) ? 1 : 0;
51        if(result - dataset.GetValue(i, targetVariable) < EPSILON) matchingInstances++;
52      }
53      return matchingInstances;
54    }
55
56    internal bool Step(IFunctionTree tree) {
57      int symbol = SymbolTable.MapFunction(tree.Function);
58      switch(symbol) {
59        case SymbolTable.AND: return Step(tree.SubTrees[0]) & Step(tree.SubTrees[0]);
60        case SymbolTable.OR: return Step(tree.SubTrees[0]) | Step(tree.SubTrees[0]);
61        case SymbolTable.NOT: return !Step(tree.SubTrees[0]);
62        case SymbolTable.XOR: return Step(tree.SubTrees[0]) ^ Step(tree.SubTrees[1]);
63        case SymbolTable.NAND: return !(Step(tree.SubTrees[0]) & Step(tree.SubTrees[0]));
64        case SymbolTable.NOR: return !(Step(tree.SubTrees[0]) | Step(tree.SubTrees[0]));
65        case SymbolTable.VARIABLE:
66          int index = ((ConstrainedIntData)tree.LocalVariables.ToArray()[0].Value).Data;
67          if(dataset.GetValue(currentRow, targetVariable) == 0.0) return false;
68          else return true;
69        case SymbolTable.UNKNOWN:
70        default:
71          throw new InvalidOperationException(tree.Function.ToString());
72
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.