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 abstract class ExpressionTemplate {
|
---|
30 | private List<Tuple<Expression, double>> arguments;
|
---|
31 | private readonly Func<IEnumerable<double>, double> transform;
|
---|
32 | public string Label { get; set; }
|
---|
33 |
|
---|
34 | protected Expression Instantiate(string name, IRandom random, int n, bool sampleWithRepetition = false) {
|
---|
35 | var weights = arguments.Select(x => x.Item2);
|
---|
36 | var args = sampleWithRepetition
|
---|
37 | ? arguments.SampleProportional(random, n, weights).Select(x => x.Item1)
|
---|
38 | : arguments.SampleProportionalWithoutRepetition(random, n, weights).Select(x => x.Item1);
|
---|
39 | var func = Expression.Function(name, transform, args);
|
---|
40 | func.Label = Label;
|
---|
41 | return func;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public abstract Expression Instantiate(string name, IRandom random, bool sampleWithRepetition = false);
|
---|
45 |
|
---|
46 | protected ExpressionTemplate(Func<IEnumerable<double>, double> transform) {
|
---|
47 | this.transform = transform;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void AddArgument(Expression expression, double weight = 1d) {
|
---|
51 | arguments = arguments ?? new List<Tuple<Expression, double>>();
|
---|
52 | arguments.Add(Tuple.Create(expression, weight));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void AddArguments(IEnumerable<Expression> expressions) {
|
---|
56 | arguments = arguments ?? new List<Tuple<Expression, double>>();
|
---|
57 | arguments.AddRange(expressions.Select(x => Tuple.Create(x, 1d)));
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void AddArguments(IEnumerable<Expression> expressions, IEnumerable<double> weights) {
|
---|
61 | arguments = arguments ?? new List<Tuple<Expression, double>>();
|
---|
62 | arguments.AddRange(expressions.Zip(weights, Tuple.Create));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public class RandomArityTemplate : ExpressionTemplate {
|
---|
67 | private readonly IRandom arityDistribution;
|
---|
68 |
|
---|
69 | public RandomArityTemplate(Func<IEnumerable<double>, double> transform, IRandom arityDistribution) : base(transform) {
|
---|
70 | this.arityDistribution = arityDistribution;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override Expression Instantiate(string name, IRandom random, bool sampleWithRepetition = false) {
|
---|
74 | var arity = (int)Math.Round(arityDistribution.NextDouble());
|
---|
75 | return Instantiate(name, random, arity, sampleWithRepetition);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public class FixedArityTemplate : ExpressionTemplate {
|
---|
80 | private readonly int arity;
|
---|
81 | public FixedArityTemplate(Func<IEnumerable<double>, double> transform, int arity) : base(transform) {
|
---|
82 | this.arity = arity;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override Expression Instantiate(string name, IRandom random, bool sampleWithRepetition = false) {
|
---|
86 | return Instantiate(name, random, arity, sampleWithRepetition);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|