Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.Boolean/3.3/Variable.cs @ 2211

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

Created a branch for #713

File size: 5.3 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 HeuristicLab.Data;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.Constraints;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Operators;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.GP.Boolean {
34  public sealed class Variable : FunctionBase {
35    public const string INDEX = "Variable";
36
37    private int minIndex;
38    private int maxIndex;
39
40    public override string Description {
41      get { return ""; }
42    }
43
44    public Variable()
45      : base() {
46      AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));
47      GetVariableInfo(INDEX).Local = true;
48      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));
49      GetVariableInfo(INITIALIZATION).Local = false;
50      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));
51      GetVariableInfo(MANIPULATION).Local = false;
52
53      MinArity = 0; MaxArity = 0;
54
55      ConstrainedIntData variable = new ConstrainedIntData();
56      AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));
57      minIndex = 0; maxIndex = 100;
58
59      SetupInitialization();
60      SetupManipulation();
61
62    }
63
64    private void SetupInitialization() {
65      CombinedOperator combinedOp = new CombinedOperator();
66      SequentialProcessor seq = new SequentialProcessor();
67      UniformRandomizer indexRandomizer = new UniformRandomizer();
68      indexRandomizer.Min = minIndex;
69      indexRandomizer.Max = maxIndex + 1; // uniform randomizer generates numbers in the range [min, max[
70      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
71      indexRandomizer.Name = "Index Randomizer";
72
73      combinedOp.OperatorGraph.AddOperator(seq);
74      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
75      combinedOp.OperatorGraph.InitialOperator = seq;
76      seq.AddSubOperator(indexRandomizer);
77      Initializer = combinedOp;
78    }
79
80    private void SetupManipulation() {
81      // manipulation operator
82      CombinedOperator combinedOp = new CombinedOperator();
83      SequentialProcessor seq = new SequentialProcessor();
84      UniformRandomizer indexRandomizer = new UniformRandomizer();
85      indexRandomizer.Min = minIndex;
86      indexRandomizer.Max = maxIndex + 1;
87      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
88      indexRandomizer.Name = "Index Randomizer";
89
90      combinedOp.OperatorGraph.AddOperator(seq);
91      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
92      combinedOp.OperatorGraph.InitialOperator = seq;
93      seq.AddSubOperator(indexRandomizer);
94      Manipulator = combinedOp;
95    }
96
97    public void SetConstraints(int[] allowedIndexes) {
98      //ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
99      Array.Sort(allowedIndexes);
100      minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1];
101      List<IConstraint> constraints = new List<IConstraint>();
102      int start = allowedIndexes[0];
103      int prev = start;
104      for(int i = 1; i < allowedIndexes.Length; i++) {
105        if(allowedIndexes[i] != prev + 1) {
106          IntBoundedConstraint lastRange = new IntBoundedConstraint();
107          lastRange.LowerBound = start;
108          lastRange.LowerBoundEnabled = true;
109          lastRange.LowerBoundIncluded = true;
110          lastRange.UpperBound = prev;
111          lastRange.UpperBoundEnabled = true;
112          lastRange.UpperBoundIncluded = true;
113          constraints.Add(lastRange);
114          start = allowedIndexes[i];
115          prev = start;
116        }
117        prev = allowedIndexes[i];
118      }
119      IntBoundedConstraint range = new IntBoundedConstraint();
120      range.LowerBound = start;
121      range.LowerBoundEnabled = true;
122      range.LowerBoundIncluded = true;
123      range.UpperBound = prev;
124      range.UpperBoundEnabled = true;
125      range.UpperBoundIncluded = true;
126      constraints.Add(range);
127      if(constraints.Count > 1) {
128        OrConstraint or = new OrConstraint();
129        foreach(IConstraint c in constraints) or.Clauses.Add(c);
130        index.AddConstraint(or);
131      } else {
132        index.AddConstraint(constraints[0]);
133      }
134
135      SetupInitialization();
136      SetupManipulation();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.