Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionEvaluator.cs @ 14409

Last change on this file since 14409 was 14409, checked in by bburlacu, 7 years ago

#2704: Initial commit of HeuristicLab.ExpressionGenerator plugin. Note that the code depends on #2703 which should be merged first.

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.ExpressionGenerator {
27  public class ExpressionEvaluator {
28    private readonly Dictionary<Expression, double> evaluationCache;
29
30    public ExpressionEvaluator() {
31      evaluationCache = new Dictionary<Expression, double>();
32    }
33
34    public double Evaluate(Expression expression) {
35      double evaluation;
36      if (evaluationCache.TryGetValue(expression, out evaluation))
37        return evaluation;
38
39      switch (expression.Type) {
40        case ExpressionType.Constant:
41          evaluation = expression.Value;
42          break;
43        case ExpressionType.RandomVariable:
44          evaluation = expression.Distribution.NextDouble();
45          break;
46        case ExpressionType.Function:
47          var args = expression.Arguments.Select(Evaluate);
48          evaluation = expression.Transform(args);
49          break;
50        default:
51          throw new ArgumentException("Unknown expression type.");
52      }
53
54      evaluationCache[expression] = evaluation;
55      return evaluation;
56    }
57
58    public Dictionary<Expression, List<double>> GenerateData(Expression expression, int rows) {
59      var data = new Dictionary<Expression, List<double>>();
60      for (int i = 0; i < rows; ++i) {
61        evaluationCache.Clear();
62        Evaluate(expression);
63        foreach (var pair in evaluationCache) {
64          if (!data.ContainsKey(pair.Key))
65            data[pair.Key] = new List<double>();
66
67          data[pair.Key].Add(pair.Value);
68        }
69      }
70      return data;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.