Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2704_HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionEvaluator.cs @ 16136

Last change on this file since 16136 was 14873, checked in by gkronber, 7 years ago

#2704: added possibility to automatically adjust constants based on the distributions of evaluated expressions to allow limiting distributions for arguments of functions (e.g. log should have positive args only, exp should have rather small args only)

File size: 2.6 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      return GenerateData(new [] {expression}, rows);
60    }
61    public Dictionary<Expression, List<double>> GenerateData(IEnumerable<Expression> expressions, int rows) {
62      var data = new Dictionary<Expression, List<double>>();
63      for (int i = 0; i < rows; ++i) {
64        evaluationCache.Clear();
65        foreach(var expr in expressions) Evaluate(expr);
66        foreach (var pair in evaluationCache) {
67          if (!data.ContainsKey(pair.Key))
68            data[pair.Key] = new List<double>();
69
70          data[pair.Key].Add(pair.Value);
71        }
72      }
73      return data;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.