1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Text.RegularExpressions;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 |
|
---|
34 | [StorableClass]
|
---|
35 | public class Calculator : IDeepCloneable {
|
---|
36 |
|
---|
37 | #region Fields & Properties
|
---|
38 |
|
---|
39 | private List<string> tokens;
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | public string Formula {
|
---|
43 | get { return string.Join(" ", tokens); }
|
---|
44 | set { tokens = Tokenize(value).ToList(); }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private static readonly Regex TokenRegex = new Regex(@"[a-zA-Z0-9._]+|""([^""]|\"")+""|[-+*/^]|log");
|
---|
48 |
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Construction & Cloning
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected Calculator(bool deserializing) { }
|
---|
55 | public Calculator() { }
|
---|
56 | public Calculator(Calculator original, Cloner cloner) {
|
---|
57 | if (original.tokens != null)
|
---|
58 | this.tokens = original.tokens.ToList();
|
---|
59 | }
|
---|
60 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new Calculator(this, cloner);
|
---|
62 | }
|
---|
63 | public object Clone() {
|
---|
64 | return Clone(new Cloner());
|
---|
65 | }
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | public IEnumerable<string> Tokenize(string s) {
|
---|
69 | return TokenRegex.Matches(s).Cast<Match>().Select(m => m.Value);
|
---|
70 | }
|
---|
71 |
|
---|
72 | private double GetVariableValue(IDictionary<string, IItem> variables, string name) {
|
---|
73 | if (variables.ContainsKey(name)) {
|
---|
74 | var item = variables[name];
|
---|
75 | var intValue = item as IntValue;
|
---|
76 | if (intValue != null) {
|
---|
77 | return intValue.Value;
|
---|
78 | } else {
|
---|
79 | var doubleValue = item as DoubleValue;
|
---|
80 | if (doubleValue != null)
|
---|
81 | return doubleValue.Value;
|
---|
82 | else
|
---|
83 | throw new InvalidOperationException("Non numerical argument");
|
---|
84 | }
|
---|
85 | } else {
|
---|
86 | throw new InvalidOperationException(string.Format("variable \"{0}\" not found", name));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IItem GetValue(IDictionary<string, IItem> variables) {
|
---|
91 | var stack = new Stack<double>();
|
---|
92 | Action<Func<double, double, double>> binf = op => {
|
---|
93 | var b = stack.Pop();
|
---|
94 | stack.Push(op(stack.Pop(), b));
|
---|
95 | };
|
---|
96 | try {
|
---|
97 | foreach (var token in tokens) {
|
---|
98 | double d;
|
---|
99 | if (double.TryParse(token,
|
---|
100 | NumberStyles.AllowDecimalPoint |
|
---|
101 | NumberStyles.AllowExponent |
|
---|
102 | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out d)) {
|
---|
103 | stack.Push(d);
|
---|
104 | } else if (token.StartsWith("\"")) {
|
---|
105 | stack.Push(GetVariableValue(variables, token.Substring(1, token.Length - 2).Replace(@"\""", @"""")));
|
---|
106 | } else {
|
---|
107 | switch (token) {
|
---|
108 | case "log": stack.Push(Math.Log(stack.Pop())); break;
|
---|
109 | case "+": binf((x, y) => x + y); break;
|
---|
110 | case "-": binf((x, y) => x - y); break;
|
---|
111 | case "*": binf((x, y) => x * y); break;
|
---|
112 | case "/": binf((x, y) => x / y); break;
|
---|
113 | case "^": binf(Math.Pow); break;
|
---|
114 | default: stack.Push(GetVariableValue(variables, token)); break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | } catch (InvalidOperationException x) {
|
---|
119 | return new StringValue(string.Format("Calculation Failed: {0}", x.Message));
|
---|
120 | }
|
---|
121 | if (stack.Count != 1)
|
---|
122 | return new StringValue("Invalid final evaluation stack size != 1");
|
---|
123 | return new DoubleValue(stack.Pop());
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|