Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Boolean/3.3/Variable.cs @ 1542

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

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

File size: 5.8 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      AddConstraint(new NumberOfSubOperatorsConstraint(0, 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      HeuristicLab.Core.IVariable initOp = GetVariable(INITIALIZATION);
78      if(initOp == null) {
79        AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
80      } else {
81        initOp.Value = combinedOp;
82      }
83    }
84
85    private void SetupManipulation() {
86      // manipulation operator
87      CombinedOperator combinedOp = new CombinedOperator();
88      SequentialProcessor seq = new SequentialProcessor();
89      UniformRandomizer indexRandomizer = new UniformRandomizer();
90      indexRandomizer.Min = minIndex;
91      indexRandomizer.Max = maxIndex + 1;
92      indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;
93      indexRandomizer.Name = "Index Randomizer";
94
95      combinedOp.OperatorGraph.AddOperator(seq);
96      combinedOp.OperatorGraph.AddOperator(indexRandomizer);
97      combinedOp.OperatorGraph.InitialOperator = seq;
98      seq.AddSubOperator(indexRandomizer);
99      HeuristicLab.Core.IVariable manipulationOp = GetVariable(MANIPULATION);
100      if(manipulationOp == null) {
101        AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
102      } else {
103        manipulationOp.Value = combinedOp;
104      }
105    }
106
107    public void SetConstraints(int[] allowedIndexes) {
108      ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false);
109      Array.Sort(allowedIndexes);
110      minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1];
111      List<IConstraint> constraints = new List<IConstraint>();
112      int start = allowedIndexes[0];
113      int prev = start;
114      for(int i = 1; i < allowedIndexes.Length; i++) {
115        if(allowedIndexes[i] != prev + 1) {
116          IntBoundedConstraint lastRange = new IntBoundedConstraint();
117          lastRange.LowerBound = start;
118          lastRange.LowerBoundEnabled = true;
119          lastRange.LowerBoundIncluded = true;
120          lastRange.UpperBound = prev;
121          lastRange.UpperBoundEnabled = true;
122          lastRange.UpperBoundIncluded = true;
123          constraints.Add(lastRange);
124          start = allowedIndexes[i];
125          prev = start;
126        }
127        prev = allowedIndexes[i];
128      }
129      IntBoundedConstraint range = new IntBoundedConstraint();
130      range.LowerBound = start;
131      range.LowerBoundEnabled = true;
132      range.LowerBoundIncluded = true;
133      range.UpperBound = prev;
134      range.UpperBoundEnabled = true;
135      range.UpperBoundIncluded = true;
136      constraints.Add(range);
137      if(constraints.Count > 1) {
138        OrConstraint or = new OrConstraint();
139        foreach(IConstraint c in constraints) or.Clauses.Add(c);
140        index.AddConstraint(or);
141      } else {
142        index.AddConstraint(constraints[0]);
143      }
144
145      SetupInitialization();
146      SetupManipulation();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.