Changeset 2365
- Timestamp:
- 09/16/09 17:14:34 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Symbols/Variable.cs
r2222 r2365 23 23 using HeuristicLab.Operators; 24 24 using HeuristicLab.Random; 25 using HeuristicLab.Data; 25 26 26 27 namespace HeuristicLab.GP.StructureIdentification { 27 public class Variable : Terminal { 28 public class Variable : Terminal { 28 29 public const string WEIGHT = "Weight"; 29 30 public const string OFFSET = "SampleOffset"; … … 99 100 offsetRandomAdder.GetVariableInfo("Value").ActualName = OFFSET; 100 101 offsetRandomAdder.Name = "Offset Adder"; 102 offsetRandomAdder.GetVariableInfo("MinValue").Local = true; 103 offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MinValue", new DoubleData(minOffset))); 104 offsetRandomAdder.GetVariableInfo("MaxValue").Local = true; 105 offsetRandomAdder.AddVariable(new HeuristicLab.Core.Variable("MaxValue", new DoubleData(maxOffset + 1))); 101 106 102 107 combinedOp.OperatorGraph.AddOperator(seq); -
trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionTreeBase.cs
r2235 r2365 94 94 } 95 95 96 public virtualstring ToString() {96 public override string ToString() { 97 97 return Function.Name; 98 98 } -
trunk/sources/HeuristicLab.Random/3.2/NormalRandomAdder.cs
r1530 r2365 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Constraints;28 27 29 28 namespace HeuristicLab.Random { … … 69 68 /// </summary> 70 69 public NormalRandomAdder() { 71 AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind. None));70 AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.In)); 72 71 GetVariableInfo("Mu").Local = true; 73 72 AddVariable(new Variable("Mu", new DoubleData(0.0))); 74 73 75 AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind. None));74 AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.In)); 76 75 GetVariableInfo("Sigma").Local = true; 77 76 AddVariable(new Variable("Sigma", new DoubleData(1.0))); 78 77 79 AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In)); 78 AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData", typeof(IObjectData), VariableKind.In | VariableKind.Out)); 79 AddVariableInfo(new VariableInfo("MinValue", "(optional) The minimal value", typeof(DoubleData), VariableKind.In)); 80 AddVariableInfo(new VariableInfo("MaxValue", "(optional) The maximal value", typeof(DoubleData), VariableKind.In)); 80 81 AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor (effective sigma = sigma * shakingFactor)", typeof(DoubleData), VariableKind.In)); 81 82 AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In)); … … 94 95 double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data; 95 96 double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data; 97 DoubleData minValueData = GetVariableValue<DoubleData>("MinValue", scope, true, false); 98 double minValue = minValueData == null ? double.MinValue : minValueData.Data; 99 DoubleData maxValueData = GetVariableValue<DoubleData>("MaxValue", scope, true, false); 100 double maxValue = maxValueData == null ? double.MaxValue : maxValueData.Data; 101 96 102 NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor); 97 103 98 AddNormal(value, normal );104 AddNormal(value, normal, minValue, maxValue); 99 105 return null; 100 106 } 101 107 102 private void AddNormal(IObjectData value, NormalDistributedRandom normal ) {108 private void AddNormal(IObjectData value, NormalDistributedRandom normal, double minValue, double maxValue) { 103 109 // dispatch manually based on dynamic type 104 110 if (value is IntData) 105 AddNormal((IntData)value, normal); 106 else if (value is ConstrainedIntData) 107 AddNormal((ConstrainedIntData)value, normal); 108 else if (value is ConstrainedDoubleData) 109 AddNormal((ConstrainedDoubleData)value, normal); 111 AddNormal((IntData)value, normal, minValue, maxValue); 110 112 else if (value is DoubleData) 111 AddNormal((DoubleData)value, normal );113 AddNormal((DoubleData)value, normal, minValue, maxValue); 112 114 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name); 113 }114 /// <summary>115 /// Generates a new double random number and adds it to the value of116 /// the given <paramref name="data"/>.117 /// </summary>118 /// <param name="data">The double object where to add the random number.</param>119 /// <param name="normal">The continuous, normally distributed random variable.</param>120 public void AddNormal(DoubleData data, NormalDistributedRandom normal) {121 data.Data += normal.NextDouble();122 115 } 123 116 … … 128 121 /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value 129 122 /// could be found.</exception> 130 /// <param name="data">The double object where to add the random number and whose constraints 131 /// to fulfill.</param> 123 /// <param name="data">The double object where to add the random number</param> 132 124 /// <param name="normal">The continuous, normally distributed random variable.</param> 133 public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) { 125 /// <param name="minValue">The minimal value allowed for the double object.</param> 126 /// <param name="maxValue">The maximal value allowed for the double object.</param> 127 public void AddNormal(DoubleData data, NormalDistributedRandom normal, double minValue, double maxValue) { 134 128 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 135 129 double newValue = data.Data + normal.NextDouble(); 136 if (IsIntegerConstrained(data)) { 137 newValue = Math.Round(newValue); 138 } 139 if (data.TrySetData(newValue)) { 130 if (newValue >= minValue && newValue < maxValue) { 131 data.Data = newValue; 140 132 return; 141 133 } 142 134 } 143 135 throw new InvalidProgramException("Coudn't find a valid value"); 144 }145 146 /// <summary>147 /// Generates a new int random number and adds it to value of the given <paramref name="data"/>.148 /// </summary>149 /// <param name="data">The int object where to add the random number.</param>150 /// <param name="normal">The continuous, normally distributed random variable.</param>151 public void AddNormal(IntData data, NormalDistributedRandom normal) {152 data.Data = (int)Math.Round(data.Data + normal.NextDouble());153 136 } 154 137 … … 159 142 /// <exception cref="InvalidProgramException">Thrown when with the current settings no valid value 160 143 /// could be found.</exception> 161 /// <param name="data">The int object where to add the generated value and whose contraints to check.</param>144 /// <param name="data">The int object where to add the generated value.</param> 162 145 /// <param name="normal">The continuous, normally distributed random variable.</param> 163 public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) { 146 /// <param name="minValue">The minimal value allowed for the double object.</param> 147 /// <param name="maxValue">The maximal value allowed for the double object.</param> 148 public void AddNormal(IntData data, NormalDistributedRandom normal, double minValue, double maxValue) { 164 149 for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) { 165 if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble()))) 150 int newValue = (int)Math.Round(data.Data + normal.NextDouble()); 151 if (newValue >= minValue && newValue < maxValue) { 152 data.Data = newValue; 166 153 return; 154 } 167 155 } 168 156 throw new InvalidProgramException("Couldn't find a valid value."); 169 157 } 170 171 private bool IsIntegerConstrained(ConstrainedDoubleData data) {172 foreach (IConstraint constraint in data.Constraints) {173 if (constraint is IsIntegerConstraint) {174 return true;175 }176 }177 return false;178 }179 158 } 180 159 }
Note: See TracChangeset
for help on using the changeset viewer.