Changeset 12719


Ignore:
Timestamp:
07/10/15 14:40:52 (6 years ago)
Author:
mkommend
Message:

#2344: Merged r12091, r12096 and r12114 into stable.

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Optimization

  • stable/HeuristicLab.Optimization.Operators/3.3/ExpressionCalculator.cs

    r12009 r12719  
    7474variables (run parameters or results):
    7575  unquoted or in double quotes if they contain special characters or whitespace
    76 mathematical functions:
     76mathematical functions (resulting in double values):
    7777  +, -, *, /, ^ (power), log
    7878predicates:
    7979  ==, <, >, isnull, not
     80conversions:
     81  toint, todouble
     82array indexing:
     83  []
    8084stack manipulation:
    8185  drop swap dup
  • stable/HeuristicLab.Optimization/3.3/RunCollectionModification/Calculator.cs

    r12009 r12719  
    9090          }
    9191        }
    92       }
    93       catch (Exception x) {
     92      } catch (Exception x) {
    9493        throw new Exception(string.Format(
    9594          "Calculation of '{1}'{0}failed at token #{2}: {3} {0}current stack is: {0}{4}", Environment.NewLine,
     
    104103      var result = stack.Pop();
    105104      if (result is string) return new StringValue((string)result);
     105      if (result is int) return new IntValue((int)result);
    106106      if (result is double) return new DoubleValue((double)result);
    107107      if (result is bool) return new BoolValue((bool)result);
     
    139139          break;
    140140
    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;
    149154
    150155        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;
    152157        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;
    157162
    158163        default: stack.Push(GetVariableValue(variables, token)); break;
     
    178183      var v = value as BoolValue;
    179184      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;
    180191      return null;
    181192    }
     
    188199          GetDoubleValue(item) ??
    189200          GetBoolValue(item) ??
     201          GetArrayValue(item) ??
    190202          item.ToString();
    191203      }
    192204      return null;
    193205    }
     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    }
    194218    #endregion
    195219
    196220    #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; }
    199224    private static bool EqualBool(object a, object b) { return a is bool && b is bool && (bool)a == (bool)b; }
    200225    private static bool EqualString(object a, object b) { return a is string && b is string && ((string)a).Equals((string)b); }
     
    208233      try {
    209234        stack.Push(func(a));
    210       }
    211       catch (Exception) {
     235      } catch (Exception) {
    212236        stack.Push(a);
    213237        throw;
     
    222246      try {
    223247        stack.Push(func(a, b));
    224       }
    225       catch (Exception) {
     248      } catch (Exception) {
    226249        stack.Push(b);
    227250        stack.Push(a);
     
    238261      try {
    239262        stack.Push(func(a, b, c));
    240       }
    241       catch (Exception) {
     263      } catch (Exception) {
    242264        stack.Push(a);
    243265        stack.Push(b);
  • stable/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionFormulaModifer.cs

    r12009 r12719  
    6868variables (run parameters or results):
    6969  unquoted or in double quotes if they contain special characters or whitespace
    70 mathematical functions:
    71   +, -, /, ^ (power), log
     70mathematical functions (resulting in double values):
     71  +, -, *, /, ^ (power), log
    7272predicates:
    7373  ==, <, >, isnull, not
     74conversions:
     75  toint, todouble
     76array indexing:
     77  []
    7478stack manipulation:
    7579  drop swap dup
Note: See TracChangeset for help on using the changeset viewer.