Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.SantaFe/FunctionLibraryInjector.cs @ 641

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

implemented #299 (Implement Santa-Fe ant benchmark problem for GP)

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