- Timestamp:
- 07/10/15 14:40:52 (9 years ago)
- Location:
- stable
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 12091,12096,12114
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Optimization
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Optimization merged: 12091,12096,12114
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Optimization.Operators/3.3/ExpressionCalculator.cs
r12009 r12719 74 74 variables (run parameters or results): 75 75 unquoted or in double quotes if they contain special characters or whitespace 76 mathematical functions :76 mathematical functions (resulting in double values): 77 77 +, -, *, /, ^ (power), log 78 78 predicates: 79 79 ==, <, >, isnull, not 80 conversions: 81 toint, todouble 82 array indexing: 83 [] 80 84 stack manipulation: 81 85 drop swap dup -
stable/HeuristicLab.Optimization/3.3/RunCollectionModification/Calculator.cs
r12009 r12719 90 90 } 91 91 } 92 } 93 catch (Exception x) { 92 } catch (Exception x) { 94 93 throw new Exception(string.Format( 95 94 "Calculation of '{1}'{0}failed at token #{2}: {3} {0}current stack is: {0}{4}", Environment.NewLine, … … 104 103 var result = stack.Pop(); 105 104 if (result is string) return new StringValue((string)result); 105 if (result is int) return new IntValue((int)result); 106 106 if (result is double) return new DoubleValue((double)result); 107 107 if (result is bool) return new BoolValue((bool)result); … … 139 139 break; 140 140 141 case "log": Apply(stack, x => Math.Log((double)x)); break; 142 case "+": Apply(stack, (x, y) => (double)x + (double)y); break; 143 case "-": Apply(stack, (x, y) => (double)x - (double)y); break; 144 case "*": Apply(stack, (x, y) => (double)x * (double)y); break; 145 case "/": Apply(stack, (x, y) => (double)x / (double)y); break; 146 case "^": Apply(stack, (x, y) => Math.Pow((double)x, (double)y)); break; 147 case "<": Apply(stack, (x, y) => (double)x < (double)y); break; 148 case ">": Apply(stack, (x, y) => (double)x > (double)y); break; 141 case "log": Apply(stack, x => Math.Log(Convert.ToDouble(x))); break; 142 case "+": Apply(stack, (x, y) => Convert.ToDouble(x) + Convert.ToDouble(y)); break; 143 case "-": Apply(stack, (x, y) => Convert.ToDouble(x) - Convert.ToDouble(y)); break; 144 case "*": Apply(stack, (x, y) => Convert.ToDouble(x) * Convert.ToDouble(y)); break; 145 case "/": Apply(stack, (x, y) => Convert.ToDouble(x) / Convert.ToDouble(y)); break; 146 case "^": Apply(stack, (x, y) => Math.Pow(Convert.ToDouble(x), Convert.ToDouble(y))); break; 147 case "<": Apply(stack, (x, y) => Convert.ToDouble(x) < Convert.ToDouble(y)); break; 148 case ">": Apply(stack, (x, y) => Convert.ToDouble(x) > Convert.ToDouble(y)); break; 149 150 case "toint": Apply(stack, x => Convert.ToInt32(x)); break; 151 case "todouble": Apply(stack, x => Convert.ToDouble(x)); break; 152 153 case "[]": Apply(stack, (a, i) => GetArrayValueAtIndex(a, Convert.ToInt32(i))); break; 149 154 150 155 case "==": Apply(stack, (x, y) => Equal(x, y)); break; 151 case "not": Apply(stack, x => ! (bool)x); break;156 case "not": Apply(stack, x => !Convert.ToBoolean(x)); break; 152 157 case "isnull": Apply(stack, x => x == null); break; 153 case "if": Apply(stack, (then, else_, cond) => (bool)cond? then : else_); break;154 155 case "ismatch": Apply(stack, (s, p) => new Regex( (string)p).IsMatch((string)s)); break;156 case "rename": Apply(stack, (s, p, r) => new Regex( (string)p).Replace((string)s, (string)r)); break;158 case "if": Apply(stack, (then, else_, cond) => Convert.ToBoolean(cond) ? then : else_); break; 159 160 case "ismatch": Apply(stack, (s, p) => new Regex(Convert.ToString(p)).IsMatch(Convert.ToString(s))); break; 161 case "rename": Apply(stack, (s, p, r) => new Regex(Convert.ToString(p)).Replace(Convert.ToString(s), Convert.ToString(r))); break; 157 162 158 163 default: stack.Push(GetVariableValue(variables, token)); break; … … 178 183 var v = value as BoolValue; 179 184 if (v != null) return v.Value; 185 return null; 186 } 187 188 private static object GetArrayValue(IItem value) { 189 if (value is IntArray || value is DoubleArray || value is BoolArray || value is StringArray) 190 return value; 180 191 return null; 181 192 } … … 188 199 GetDoubleValue(item) ?? 189 200 GetBoolValue(item) ?? 201 GetArrayValue(item) ?? 190 202 item.ToString(); 191 203 } 192 204 return null; 193 205 } 206 207 private static object GetArrayValueAtIndex(object array, int index) { 208 if (array is IntArray) 209 return ((IntArray)array)[index]; 210 if (array is DoubleArray) 211 return ((DoubleArray)array)[index]; 212 if (array is BoolArray) 213 return ((BoolArray)array)[index]; 214 if (array is StringArray) 215 return ((StringArray)array)[index]; 216 throw new NotSupportedException(string.Format("Type {0} is not a supported array type", array.GetType().Name)); 217 } 194 218 #endregion 195 219 196 220 #region variadic equality 197 private static bool Equal(object a, object b) { return EqualNumber(a, b) || EqualBool(a, b) || EqualString(a, b) || a == b; } 198 private static bool EqualNumber(object a, object b) { return a is double && b is double && (double)a == (double)b; } 221 private static bool Equal(object a, object b) { return EqualIntegerNumber(a, b) || EqualFloatingNumber(a, b) || EqualBool(a, b) || EqualString(a, b) || a == b; } 222 private static bool EqualIntegerNumber(object a, object b) { return a is int && b is int && (int)a == (int)b; } 223 private static bool EqualFloatingNumber(object a, object b) { return a is double && b is double && (double)a == (double)b; } 199 224 private static bool EqualBool(object a, object b) { return a is bool && b is bool && (bool)a == (bool)b; } 200 225 private static bool EqualString(object a, object b) { return a is string && b is string && ((string)a).Equals((string)b); } … … 208 233 try { 209 234 stack.Push(func(a)); 210 } 211 catch (Exception) { 235 } catch (Exception) { 212 236 stack.Push(a); 213 237 throw; … … 222 246 try { 223 247 stack.Push(func(a, b)); 224 } 225 catch (Exception) { 248 } catch (Exception) { 226 249 stack.Push(b); 227 250 stack.Push(a); … … 238 261 try { 239 262 stack.Push(func(a, b, c)); 240 } 241 catch (Exception) { 263 } catch (Exception) { 242 264 stack.Push(a); 243 265 stack.Push(b); -
stable/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionFormulaModifer.cs
r12009 r12719 68 68 variables (run parameters or results): 69 69 unquoted or in double quotes if they contain special characters or whitespace 70 mathematical functions :71 +, -, /, ^ (power), log70 mathematical functions (resulting in double values): 71 +, -, *, /, ^ (power), log 72 72 predicates: 73 73 ==, <, >, isnull, not 74 conversions: 75 toint, todouble 76 array indexing: 77 [] 74 78 stack manipulation: 75 79 drop swap dup
Note: See TracChangeset
for help on using the changeset viewer.