Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionGenerator.cs @ 14505

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

#2704: Improve expression generation and fix file formatting.

File size: 4.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.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Random;
27
28namespace HeuristicLab.ExpressionGenerator {
29  public static class ExpressionGenerator {
30    public static IEnumerable<Expression> GenerateRandomDistributedVariables(int numberOfVariables, string variablePrefix, IRandom randomDistribution) {
31      return Enumerable.Range(1, numberOfVariables).Select(x => Expression.RandomVariable(string.Format("{0}{1}", variablePrefix, x), randomDistribution));
32    }
33
34    public static Expression Polynomial(IRandom uniformRandom, Expression[] variables, bool useLog, bool useExp) {
35      var sumTemplate = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 1, variables.Length)) { Label = "+" };
36      sumTemplate.AddArguments(variables);
37
38      var productTemplate = new RandomArityTemplate(Product, new UniformDistributedRandom(uniformRandom, 1, variables.Length)) { Label = "*" };
39      productTemplate.AddArguments(variables);
40
41
42      //      var count = (int)Enumerable.Range(1, variables.Length).Sum(x => EnumerableExtensions.BinomialCoefficient(x, variables.Length));
43      var count = 10;
44      int i = 1;
45
46      var logTemplate = new FixedArityTemplate(Log, 1) { Label = "log" };
47      logTemplate.AddArguments(variables);
48      logTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom, true)));
49
50      var expTemplate = new FixedArityTemplate(Exp, 1) { Label = "exp" };
51      expTemplate.AddArguments(variables);
52      expTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom, true)));
53
54      var inverseTemplate = new FixedArityTemplate(Division, 1);
55      inverseTemplate.AddArguments(variables);
56      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom, true)));
57      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom, true)));
58      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom, true)));
59      inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom, true)));
60
61
62      var template = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 2, count)) { Label = "+" };
63      template.AddArguments(variables);
64      template.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
65      template.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
66      template.AddArguments(Enumerable.Range(1, count).Select(x => inverseTemplate.Instantiate(string.Format("inv{0}", i++), uniformRandom)));
67
68      return template.Instantiate("polynomial", uniformRandom);
69    }
70
71    #region static functions
72    private static double Sum(IEnumerable<double> args) {
73      return args.Sum();
74    }
75
76    private static double Product(IEnumerable<double> args) {
77      return args.Aggregate((x, y) => x * y);
78    }
79
80    private static double Division(IEnumerable<double> args) {
81      if (args.Count() == 1)
82        return 1 / args.First();
83
84      return args.Aggregate((x, y) => x / y);
85    }
86
87    private static double Subtraction(IEnumerable<double> args) {
88      if (args.Count() == 1)
89        return -args.First();
90
91      return args.Aggregate((x, y) => x - y);
92    }
93
94    private static double Exp(IEnumerable<double> args) {
95      return Math.Exp(args.Single());
96    }
97
98    private static double Log(IEnumerable<double> args) {
99      return Math.Log(args.Single());
100    }
101
102    private static double Square(IEnumerable<double> args) {
103      var v = args.Single();
104      return v * v;
105    }
106    #endregion
107  }
108}
Note: See TracBrowser for help on using the repository browser.