Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Boolean/3.3/FunctionLibraryInjector.cs @ 1835

Last change on this file since 1835 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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