Changeset 467 for trunk/sources/HeuristicLab.Functions
- Timestamp:
- 08/08/08 17:09:55 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Functions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/Differential.cs
r425 r467 32 32 33 33 namespace HeuristicLab.Functions { 34 public sealed class Differential : FunctionBase { 35 36 public const string WEIGHT = "Weight"; 37 public const string OFFSET = "SampleOffset"; 38 public const string INDEX = "Variable"; 34 public sealed class Differential : Variable { 39 35 40 36 public override string Description { … … 46 42 public Differential() 47 43 : base() { 48 AddVariableInfo(new VariableInfo(INDEX, "Index of the variable in the dataset representing this feature", typeof(ConstrainedIntData), VariableKind.None));49 GetVariableInfo(INDEX).Local = true;50 AddVariableInfo(new VariableInfo(WEIGHT, "Weight is multiplied to the feature value", typeof(ConstrainedDoubleData), VariableKind.None));51 GetVariableInfo(WEIGHT).Local = true;52 AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None));53 GetVariableInfo(OFFSET).Local = true;54 55 ConstrainedDoubleData weight = new ConstrainedDoubleData();56 // initialize a totally arbitrary range for the weight = [-20.0, 20.0]57 weight.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));58 AddVariable(new HeuristicLab.Core.Variable(WEIGHT, weight));59 60 ConstrainedIntData variable = new ConstrainedIntData();61 AddVariable(new HeuristicLab.Core.Variable(INDEX, variable));62 63 ConstrainedIntData sampleOffset = new ConstrainedIntData();64 // initialize a sample offset for static models65 IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(0, 0);66 offsetConstraint.LowerBoundIncluded = true;67 offsetConstraint.UpperBoundIncluded = true;68 sampleOffset.AddConstraint(offsetConstraint);69 AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset));70 71 SetupInitialization();72 SetupManipulation();73 74 // variable can't have suboperators75 AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));76 }77 78 private void SetupInitialization() {79 AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for differentials", typeof(CombinedOperator), VariableKind.None));80 GetVariableInfo(INITIALIZATION).Local = false;81 CombinedOperator combinedOp = new CombinedOperator();82 SequentialProcessor seq = new SequentialProcessor();83 UniformRandomizer indexRandomizer = new UniformRandomizer();84 indexRandomizer.Min = 0;85 indexRandomizer.Max = 10;86 indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;87 indexRandomizer.Name = "Index Randomizer";88 NormalRandomizer weightRandomizer = new NormalRandomizer();89 weightRandomizer.Mu = 1.0;90 weightRandomizer.Sigma = 1.0;91 weightRandomizer.GetVariableInfo("Value").ActualName = WEIGHT;92 weightRandomizer.Name = "Weight Randomizer";93 UniformRandomizer offsetRandomizer = new UniformRandomizer();94 offsetRandomizer.Min = 0.0;95 offsetRandomizer.Max = 1.0;96 offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET;97 offsetRandomizer.Name = "Offset Randomizer";98 99 combinedOp.OperatorGraph.AddOperator(seq);100 combinedOp.OperatorGraph.AddOperator(indexRandomizer);101 combinedOp.OperatorGraph.AddOperator(weightRandomizer);102 combinedOp.OperatorGraph.AddOperator(offsetRandomizer);103 combinedOp.OperatorGraph.InitialOperator = seq;104 seq.AddSubOperator(indexRandomizer);105 seq.AddSubOperator(weightRandomizer);106 seq.AddSubOperator(offsetRandomizer);107 AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));108 }109 110 private void SetupManipulation() {111 // manipulation operator112 AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for differentials", typeof(CombinedOperator), VariableKind.None));113 GetVariableInfo(MANIPULATION).Local = false;114 CombinedOperator combinedOp = new CombinedOperator();115 SequentialProcessor seq = new SequentialProcessor();116 UniformRandomizer indexRandomizer = new UniformRandomizer();117 indexRandomizer.Min = 0;118 indexRandomizer.Max = 10;119 indexRandomizer.GetVariableInfo("Value").ActualName = INDEX;120 indexRandomizer.Name = "Index Randomizer";121 NormalRandomAdder weightRandomAdder = new NormalRandomAdder();122 weightRandomAdder.Mu = 0.0;123 weightRandomAdder.Sigma = 0.1;124 weightRandomAdder.GetVariableInfo("Value").ActualName = WEIGHT;125 weightRandomAdder.Name = "Weight Adder";126 NormalRandomAdder offsetRandomAdder = new NormalRandomAdder();127 offsetRandomAdder.Mu = 0.0;128 offsetRandomAdder.Sigma = 1.0;129 offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET;130 offsetRandomAdder.Name = "Offset Adder";131 132 combinedOp.OperatorGraph.AddOperator(seq);133 combinedOp.OperatorGraph.AddOperator(indexRandomizer);134 combinedOp.OperatorGraph.AddOperator(weightRandomAdder);135 combinedOp.OperatorGraph.AddOperator(offsetRandomAdder);136 combinedOp.OperatorGraph.InitialOperator = seq;137 seq.AddSubOperator(indexRandomizer);138 seq.AddSubOperator(weightRandomAdder);139 seq.AddSubOperator(offsetRandomAdder);140 AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));141 44 } 142 45 -
trunk/sources/HeuristicLab.Functions/Variable.cs
r425 r467 32 32 33 33 namespace HeuristicLab.Functions { 34 public sealedclass Variable : FunctionBase {34 public class Variable : FunctionBase { 35 35 36 36 public const string WEIGHT = "Weight"; 37 37 public const string OFFSET = "SampleOffset"; 38 38 public const string INDEX = "Variable"; 39 40 private int minIndex; 41 private int maxIndex; 42 private int minOffset; 43 private int maxOffset; 39 44 40 45 public override string Description { … … 54 59 AddVariableInfo(new VariableInfo(OFFSET, "SampleOffset is added to the sample index", typeof(ConstrainedIntData), VariableKind.None)); 55 60 GetVariableInfo(OFFSET).Local = true; 61 AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None)); 62 GetVariableInfo(INITIALIZATION).Local = false; 63 AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None)); 64 GetVariableInfo(MANIPULATION).Local = false; 56 65 57 66 ConstrainedDoubleData weight = new ConstrainedDoubleData(); … … 62 71 ConstrainedIntData variable = new ConstrainedIntData(); 63 72 AddVariable(new HeuristicLab.Core.Variable(INDEX, variable)); 73 minIndex = 0; maxIndex = 100; 64 74 65 75 ConstrainedIntData sampleOffset = new ConstrainedIntData(); 66 // initialize a sample offset for static models67 IntBoundedConstraint offsetConstraint = new IntBoundedConstraint(0, 0);68 offsetConstraint.LowerBoundIncluded = true;69 offsetConstraint.UpperBoundIncluded = true;70 sampleOffset.AddConstraint(offsetConstraint);71 76 AddVariable(new HeuristicLab.Core.Variable(OFFSET, sampleOffset)); 72 77 … … 79 84 80 85 private void SetupInitialization() { 81 AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator for variables", typeof(CombinedOperator), VariableKind.None));82 GetVariableInfo(INITIALIZATION).Local = false;83 86 CombinedOperator combinedOp = new CombinedOperator(); 84 87 SequentialProcessor seq = new SequentialProcessor(); 85 88 UniformRandomizer indexRandomizer = new UniformRandomizer(); 86 indexRandomizer.Min = 0;87 indexRandomizer.Max = 10;89 indexRandomizer.Min = minIndex; 90 indexRandomizer.Max = maxIndex; 88 91 indexRandomizer.GetVariableInfo("Value").ActualName = INDEX; 89 92 indexRandomizer.Name = "Index Randomizer"; … … 94 97 weightRandomizer.Name = "Weight Randomizer"; 95 98 UniformRandomizer offsetRandomizer = new UniformRandomizer(); 96 offsetRandomizer.Min = 0.0;97 offsetRandomizer.Max = 1.0;99 offsetRandomizer.Min = minOffset; 100 offsetRandomizer.Max = maxOffset; 98 101 offsetRandomizer.GetVariableInfo("Value").ActualName = OFFSET; 99 102 offsetRandomizer.Name = "Offset Randomizer"; … … 107 110 seq.AddSubOperator(weightRandomizer); 108 111 seq.AddSubOperator(offsetRandomizer); 109 AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp)); 112 HeuristicLab.Core.IVariable initOp = GetVariable(INITIALIZATION); 113 if(initOp == null) { 114 AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp)); 115 } else { 116 initOp.Value = combinedOp; 117 } 110 118 } 111 119 112 120 private void SetupManipulation() { 113 121 // manipulation operator 114 AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator for variables", typeof(CombinedOperator), VariableKind.None));115 GetVariableInfo(MANIPULATION).Local = false;116 122 CombinedOperator combinedOp = new CombinedOperator(); 117 123 SequentialProcessor seq = new SequentialProcessor(); 118 124 UniformRandomizer indexRandomizer = new UniformRandomizer(); 119 indexRandomizer.Min = 0;120 indexRandomizer.Max = 10;125 indexRandomizer.Min = minIndex; 126 indexRandomizer.Max = maxIndex; 121 127 indexRandomizer.GetVariableInfo("Value").ActualName = INDEX; 122 128 indexRandomizer.Name = "Index Randomizer"; … … 140 146 seq.AddSubOperator(weightRandomAdder); 141 147 seq.AddSubOperator(offsetRandomAdder); 142 AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp)); 148 HeuristicLab.Core.IVariable manipulationOp = GetVariable(MANIPULATION); 149 if(manipulationOp == null) { 150 AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp)); 151 } else { 152 manipulationOp.Value = combinedOp; 153 } 154 } 155 156 public void SetConstraints(int[] allowedIndexes, int minSampleOffset, int maxSampleOffset) { 157 ConstrainedIntData offset = GetVariableValue<ConstrainedIntData>(OFFSET, null, false); 158 IntBoundedConstraint rangeConstraint = new IntBoundedConstraint(); 159 this.minOffset = minSampleOffset; 160 this.maxOffset = maxSampleOffset; 161 rangeConstraint.LowerBound = minSampleOffset; 162 rangeConstraint.LowerBoundEnabled = true; 163 rangeConstraint.LowerBoundIncluded = true; 164 rangeConstraint.UpperBound = maxSampleOffset; 165 rangeConstraint.UpperBoundEnabled = true; 166 rangeConstraint.UpperBoundIncluded = true; 167 offset.AddConstraint(rangeConstraint); 168 169 ConstrainedIntData index = GetVariableValue<ConstrainedIntData>(INDEX, null, false); 170 Array.Sort(allowedIndexes); 171 minIndex = allowedIndexes[0]; maxIndex = allowedIndexes[allowedIndexes.Length - 1]; 172 List<IConstraint> constraints = new List<IConstraint>(); 173 int start = allowedIndexes[0]; 174 int prev = start; 175 for(int i = 1; i < allowedIndexes.Length; i++) { 176 if(allowedIndexes[i] != prev + 1) { 177 IntBoundedConstraint lastRange = new IntBoundedConstraint(); 178 lastRange.LowerBound = start; 179 lastRange.LowerBoundEnabled = true; 180 lastRange.LowerBoundIncluded = true; 181 lastRange.UpperBound = prev; 182 lastRange.UpperBoundEnabled = true; 183 lastRange.UpperBoundIncluded = true; 184 constraints.Add(lastRange); 185 start = allowedIndexes[i]; 186 prev = start; 187 } 188 prev = allowedIndexes[i]; 189 } 190 if(constraints.Count > 1) { 191 OrConstraint or = new OrConstraint(); 192 foreach(IConstraint c in constraints) or.Clauses.Add(c); 193 index.AddConstraint(or); 194 } else { 195 index.AddConstraint(constraints[0]); 196 } 197 198 SetupInitialization(); 199 SetupManipulation(); 143 200 } 144 201
Note: See TracChangeset
for help on using the changeset viewer.