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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Random;
|
---|
27 |
|
---|
28 | namespace 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 | const int count = 10; // how many instances of each template should be produce?
|
---|
42 | int i = 1;
|
---|
43 |
|
---|
44 | var logTemplate = new FixedArityTemplate(Log, 1) { Label = "log" };
|
---|
45 | logTemplate.AddArguments(variables);
|
---|
46 | logTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom)));
|
---|
47 |
|
---|
48 | var expTemplate = new FixedArityTemplate(Exp, 1) { Label = "exp" };
|
---|
49 | expTemplate.AddArguments(variables);
|
---|
50 | expTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom)));
|
---|
51 |
|
---|
52 | var inverseTemplate = new FixedArityTemplate(Division, 1) { Label = "/" };
|
---|
53 | inverseTemplate.AddArguments(variables);
|
---|
54 | inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => sumTemplate.Instantiate(string.Format("sum{0}", i++), uniformRandom)));
|
---|
55 | inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => productTemplate.Instantiate(string.Format("prod{0}", i++), uniformRandom)));
|
---|
56 | inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
|
---|
57 | inverseTemplate.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
|
---|
58 |
|
---|
59 | var template = new RandomArityTemplate(Sum, new UniformDistributedRandom(uniformRandom, 2, count)) { Label = "+" };
|
---|
60 | template.AddArguments(variables);
|
---|
61 | template.AddArguments(Enumerable.Range(1, count).Select(x => logTemplate.Instantiate(string.Format("log{0}", i++), uniformRandom)));
|
---|
62 | template.AddArguments(Enumerable.Range(1, count).Select(x => expTemplate.Instantiate(string.Format("exp{0}", i++), uniformRandom)));
|
---|
63 | template.AddArguments(Enumerable.Range(1, count).Select(x => inverseTemplate.Instantiate(string.Format("inv{0}", i++), uniformRandom)));
|
---|
64 |
|
---|
65 | return template.Instantiate("polynomial", uniformRandom);
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region static functions
|
---|
69 | private static double Sum(IEnumerable<double> args) {
|
---|
70 | return args.Sum();
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static double Product(IEnumerable<double> args) {
|
---|
74 | return args.Aggregate((x, y) => x * y);
|
---|
75 | }
|
---|
76 |
|
---|
77 | private static double Division(IEnumerable<double> args) {
|
---|
78 | if (args.Count() == 1)
|
---|
79 | return 1 / args.First();
|
---|
80 |
|
---|
81 | return args.Aggregate((x, y) => x / y);
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static double Subtraction(IEnumerable<double> args) {
|
---|
85 | if (args.Count() == 1)
|
---|
86 | return -args.First();
|
---|
87 |
|
---|
88 | return args.Aggregate((x, y) => x - y);
|
---|
89 | }
|
---|
90 |
|
---|
91 | private static double Exp(IEnumerable<double> args) {
|
---|
92 | return Math.Exp(args.Single());
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static double Log(IEnumerable<double> args) {
|
---|
96 | return Math.Log(args.Single());
|
---|
97 | }
|
---|
98 |
|
---|
99 | private static double Square(IEnumerable<double> args) {
|
---|
100 | var v = args.Single();
|
---|
101 | return v * v;
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 | }
|
---|
105 | }
|
---|