#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.IntVector { /// /// Uniformly distributed change of a single position of an integer vector. /// /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg. /// /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg. /// [Item("UniformOnePositionManipulator", " Uniformly distributed change of a single position of an integer vector. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")] [StorableClass] public class UniformOnePositionManipulator : IntVectorManipulator { /// /// The lower bound of the values in the int vector. /// public ValueLookupParameter MinimumParameter { get { return (ValueLookupParameter)Parameters["Minimum"]; } } /// /// The upper bound of the values in the int vector. /// public ValueLookupParameter MaximumParameter { get { return (ValueLookupParameter)Parameters["Maximum"]; } } /// /// Initializes a new instance of with two parameters /// (Minimum and Maximum). /// public UniformOnePositionManipulator() { Parameters.Add(new ValueLookupParameter("Minimum", "Minimum of the sampling range for the vector element (included)")); Parameters.Add(new ValueLookupParameter("Maximum", "Maximum of the sampling range for the vector element (excluded)")); } /// /// Changes randomly a single position in the given integer . /// /// A random number generator. /// The integer vector to manipulate. /// The minimum value of the sampling range for /// the vector element to change (inclusive). /// The maximum value of the sampling range for /// the vector element to change (exclusive). public static void Apply(IRandom random, IntArrayData vector, IntData min, IntData max) { int index = random.Next(vector.Length); vector[index] = random.Next(min.Value, max.Value); } /// /// Changes randomly a single position in the given integer . /// /// Calls . /// A random number generator. /// The integer vector to manipulate. protected override void Manipulate(IRandom random, IntArrayData vector) { if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found."); if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found."); Apply(random, vector, MinimumParameter.ActualValue, MaximumParameter.ActualValue); } } }