Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.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: 3.9 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.Globalization;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Core;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.ExpressionGenerator {
31  public class Expression : IExpression {
32    public string Label { get; set; }
33
34    public ExpressionType Type { get; }
35
36    private readonly List<Expression> arguments;
37    public IEnumerable<Expression> Arguments {
38      get { return arguments ?? Enumerable.Empty<Expression>(); }
39    }
40
41    public IRandom Distribution { get; }
42
43    public Func<IEnumerable<double>, double> Transform { get; }
44
45    public double Value { get; }
46
47    public Expression(string label, double value) {
48      Type = ExpressionType.Constant;
49      Value = value;
50      Label = label;
51    }
52
53    public Expression(string label, IRandom distribution) {
54      Type = ExpressionType.RandomVariable;
55      Distribution = distribution;
56      Label = label;
57    }
58
59    public Expression(string label, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
60      Type = ExpressionType.Function;
61      Transform = transform;
62      this.arguments = this.arguments ?? new List<Expression>();
63      this.arguments.AddRange(arguments);
64      Label = label;
65    }
66
67    public static Expression Constant(string label, double value) {
68      return new Expression(label, value);
69    }
70
71    public static Expression RandomVariable(string label, IRandom distribution) {
72      return new Expression(label, distribution);
73    }
74
75    public static Expression Function(string label, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {
76      return new Expression(label, transform, arguments);
77    }
78
79    public override string ToString() {
80      var sb = new StringBuilder();
81
82      switch (Type) {
83        case ExpressionType.Constant:
84          sb.Append(Value.ToString("0.000", CultureInfo.CurrentCulture));
85          break;
86        case ExpressionType.RandomVariable:
87          sb.Append(string.Format("{0} ~ {1}", Label, GetStringDescription(Distribution)));
88          break;
89        case ExpressionType.Function:
90          sb.Append(string.Format("{0} (", Label));
91          if (arguments != null) {
92            for (int i = 0; i < arguments.Count - 1; ++i)
93              sb.Append(string.Format("{0}, ", arguments[i]));
94            sb.Append(string.Format("{0})", arguments.Last()));
95          }
96          break;
97      }
98
99      return sb.ToString();
100    }
101
102    private static string GetStringDescription(IRandom random) {
103      var normal = random as NormalDistributedRandom;
104      if (normal != null)
105        return string.Format("N({0}, {1})", normal.Mu, normal.Sigma);
106
107      var uniform = random as UniformDistributedRandom;
108      if (uniform != null)
109        return string.Format("U({0}, {1})", uniform.Min, uniform.Max);
110
111      var gamma = random as GammaDistributedRandom;
112      if (gamma != null)
113        return string.Format("G({0}, {1})", gamma.Shape, gamma.Rate);
114
115      return random.ToString();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.