Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Boolean/FunctionLibraryInjector.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: 3.7 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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.Constraints;
30
31namespace HeuristicLab.GP.Boolean {
32  public class FunctionLibraryInjector : OperatorBase {
33    private const string OPERATORLIBRARY = "FunctionLibrary";
34
35    private GPOperatorLibrary operatorLibrary;
36
37    public override string Description {
38      get { return @"Injects a function library for boolean logic."; }
39    }
40
41    public FunctionLibraryInjector()
42      : base() {
43      AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(GPOperatorLibrary), VariableKind.New));
44    }
45
46    public override IOperation Apply(IScope scope) {
47      InitDefaultOperatorLibrary();
48      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(OPERATORLIBRARY), operatorLibrary));
49      return null;
50    }
51
52    private void InitDefaultOperatorLibrary() {
53      And and = new And();
54      Or or = new Or();
55      Not not = new Not();
56      Nand nand = new Nand();
57      Nor nor = new Nor();
58      Xor xor = new Xor();
59      HeuristicLab.GP.Boolean.Variable variable = new HeuristicLab.GP.Boolean.Variable();
60
61      IFunction[] allFunctions = new IFunction[] {
62        and,
63        or,
64        not,
65        nand,
66        nor,
67        xor,
68        variable
69      };
70
71      SetAllowedSubOperators(and, allFunctions);
72      SetAllowedSubOperators(or, allFunctions);
73      SetAllowedSubOperators(not, allFunctions);
74      SetAllowedSubOperators(nand, allFunctions);
75      SetAllowedSubOperators(nor, allFunctions);
76      SetAllowedSubOperators(not, allFunctions);
77
78      operatorLibrary = new GPOperatorLibrary();
79      operatorLibrary.GPOperatorGroup.AddOperator(and);
80      operatorLibrary.GPOperatorGroup.AddOperator(or);
81      operatorLibrary.GPOperatorGroup.AddOperator(not);
82      operatorLibrary.GPOperatorGroup.AddOperator(nand);
83      operatorLibrary.GPOperatorGroup.AddOperator(nor);
84      operatorLibrary.GPOperatorGroup.AddOperator(xor);
85      operatorLibrary.GPOperatorGroup.AddOperator(variable);
86    }
87
88    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
89      foreach(IConstraint c in f.Constraints) {
90        if(c is SubOperatorTypeConstraint) {
91          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
92          typeConstraint.Clear();
93          foreach(IFunction g in gs) {
94            typeConstraint.AddOperator(g);
95          }
96        } else if(c is AllSubOperatorsTypeConstraint) {
97          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
98          typeConstraint.Clear();
99          foreach(IFunction g in gs) {
100            typeConstraint.AddOperator(g);
101          }
102        }
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.