Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.Boolean/3.3/FunctionLibraryInjector.cs @ 2202

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

Created a branch for #713

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