Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Random/3.2/UniformItemChooser.cs @ 13780

Last change on this file since 13780 was 2174, checked in by gkronber, 15 years ago

Implemented #302 (Show variable names instead of var<index> in GP function trees).

  • Added a new operator that chooses a random value from a list of possible values.
  • Changed mutation operator for variables and differentials
  • Changed internal linear representation of function trees to support different types for local data.
File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Constraints;
28
29namespace HeuristicLab.Random {
30  /// <summary>
31  /// UniformItemChooser takes a list of items selects one of the elements randomly and sets the selected element as value of output variable.
32  /// </summary>
33  public class UniformItemChooser : OperatorBase {
34    /// <inheritdoc select="summary"/>
35    public override string Description {
36      get { return "Initializes the value of variable 'Value' to a random value chosen uniformly from the a list of values"; }
37    }
38
39    /// <summary>
40    /// Initializes a new instance of <see cref="UniformItemChooser"/> with four variable infos
41    /// (<c>Value</c>, <c>Random</c>, <c>Max</c> and <c>Min</c>), being a random number generator
42    /// between 0.0 and 1.0.
43    /// </summary>
44    public UniformItemChooser() {
45      AddVariableInfo(new VariableInfo("Value", "The value to manipulate", typeof(IItem), VariableKind.Out));
46      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
47      AddVariableInfo(new VariableInfo("Values", "The list of possible values", typeof(ItemList), VariableKind.In));
48    }
49
50    /// <summary>
51    /// Selects a random element from 'values' and sets the selected element as value of variable 'Value'
52    /// </summary>
53    /// <param name="scope">The scope where to apply the random number generator.</param>
54    /// <returns><c>null</c>.</returns>
55    public override IOperation Apply(IScope scope) {
56      IVariable value = scope.GetVariable(scope.TranslateName("Value"));
57      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
58      ItemList values = GetVariableValue<ItemList>("Values", scope, true);
59      value.Value = values[mt.Next(values.Count)];
60      return null;
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.