Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.SantaFe/3.3/FunctionLibraryInjector.cs @ 2215

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

Created a branch for #713

File size: 3.5 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.SantaFe {
32  public class FunctionLibraryInjector : OperatorBase {
33    private const string OPERATORLIBRARY = "FunctionLibrary";
34
35    private FunctionLibrary operatorLibrary;
36
37    public override string Description {
38      get { return @"Injects a function library for the ant problem."; }
39    }
40
41    public FunctionLibraryInjector()
42      : base() {
43      AddVariableInfo(new VariableInfo(OPERATORLIBRARY, "Preconfigured default operator library", typeof(FunctionLibrary), 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      IfFoodAhead ifFoodAhead = new IfFoodAhead();
54      Prog2 prog2 = new Prog2();
55      Prog3 prog3 = new Prog3();
56      Move move = new Move();
57      Left left = new Left();
58      Right right = new Right();
59
60      IFunction[] allFunctions = new IFunction[] {
61        ifFoodAhead,
62        prog2,
63        prog3,
64        move,
65        left,
66        right
67      };
68
69      SetAllowedSubOperators(ifFoodAhead, allFunctions);
70      SetAllowedSubOperators(prog2, allFunctions);
71      SetAllowedSubOperators(prog3, allFunctions);
72
73      operatorLibrary = new FunctionLibrary();
74      operatorLibrary.FunctionGroup.AddFunction(ifFoodAhead);
75      operatorLibrary.FunctionGroup.AddFunction(prog2);
76      operatorLibrary.FunctionGroup.AddFunction(prog3);
77      operatorLibrary.FunctionGroup.AddFunction(move);
78      operatorLibrary.FunctionGroup.AddFunction(left);
79      operatorLibrary.FunctionGroup.AddFunction(right);
80    }
81
82    private void SetAllowedSubOperators(IFunction f, IFunction[] gs) {
83      foreach(IConstraint c in f.Constraints) {
84        if(c is SubOperatorTypeConstraint) {
85          SubOperatorTypeConstraint typeConstraint = c as SubOperatorTypeConstraint;
86          typeConstraint.Clear();
87          foreach(IFunction g in gs) {
88            typeConstraint.AddOperator(g);
89          }
90        } else if(c is AllSubOperatorsTypeConstraint) {
91          AllSubOperatorsTypeConstraint typeConstraint = c as AllSubOperatorsTypeConstraint;
92          typeConstraint.Clear();
93          foreach(IFunction g in gs) {
94            typeConstraint.AddOperator(g);
95          }
96        }
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.