#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Constraints; namespace HeuristicLab.Random { /// /// UniformItemChooser takes a list of items selects one of the elements randomly and sets the selected element as value of output variable. /// public class UniformItemChooser : OperatorBase { /// public override string Description { get { return "Initializes the value of variable 'Value' to a random value chosen uniformly from the a list of values"; } } /// /// Initializes a new instance of with four variable infos /// (Value, Random, Max and Min), being a random number generator /// between 0.0 and 1.0. /// public UniformItemChooser() { AddVariableInfo(new VariableInfo("Value", "The value to manipulate", typeof(IItem), VariableKind.Out)); AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In)); AddVariableInfo(new VariableInfo("Values", "The list of possible values", typeof(ItemList), VariableKind.In)); } /// /// Selects a random element from 'values' and sets the selected element as value of variable 'Value' /// /// The scope where to apply the random number generator. /// null. public override IOperation Apply(IScope scope) { IVariable value = scope.GetVariable(scope.TranslateName("Value")); MersenneTwister mt = GetVariableValue("Random", scope, true); ItemList values = GetVariableValue("Values", scope, true); value.Value = values[mt.Next(values.Count)]; return null; } } }