Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionTemplate.cs @ 14448

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

#2704: Rename expression label to name, and remove label from expression template as it should be specified for each individual instance.

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