#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ParameterConfigurationEncoding { /// /// TODO /// [Item("ParameterConfigurationManipulator", "TODO")] [StorableClass] public abstract class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator { public override bool CanChangeName { get { return false; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public IValueLookupParameter ParameterConfigurationTreeParameter { get { return (IValueLookupParameter)Parameters["ParameterConfigurationTree"]; } } public IValueLookupParameter IntValueManipulatorParameter { get { return (IValueLookupParameter)Parameters["IntValueManipulator"]; } } public IValueLookupParameter DoubleValueManipulatorParameter { get { return (IValueLookupParameter)Parameters["DoubleValueManipulator"]; } } #region Constructors and Cloning public ParameterConfigurationManipulator() : base() { Parameters.Add(new ValueLookupParameter("Random", "The pseudo random number generator which should be used for stochastic manipulation operators.")); Parameters.Add(new ValueLookupParameter("ParameterConfigurationTree", "The parameter configuration which should be manipulated.")); Parameters.Add(new ValueLookupParameter("IntValueManipulator", "")); Parameters.Add(new ValueLookupParameter("DoubleValueManipulator", "")); } [StorableConstructor] protected ParameterConfigurationManipulator(bool deserializing) : base(deserializing) { } protected ParameterConfigurationManipulator(ParameterConfigurationManipulator original, Cloner cloner) : base(original, cloner) { } #endregion public override IOperation Apply() { Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue); return base.Apply(); } public static void Apply(IRandom random, IValueConfiguration configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { Mutate(random, configuration, intValueManipulator, doubleValueManipulator); } public static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { var vc = configuration as RangeValueConfiguration; var pc = configuration as IParameterConfiguration; if (vc != null) { var value = vc.ActualValue.Value; var range = vc.RangeConstraint; if (value is IntValue) { intValueManipulator.Apply(random, (IntValue)value, (IntValueRange)range); } else if (value is PercentValue) { doubleValueManipulator.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange()); } else if (value is DoubleValue) { doubleValueManipulator.Apply(random, (DoubleValue)value, (DoubleValueRange)range); } } else if (pc != null) { do { pc.ActualValueConfigurationIndex = random.Next(pc.ValueConfigurations.Count()); } while (!pc.ValueConfigurations.ItemChecked(pc.ValueConfigurations[pc.ActualValueConfigurationIndex])); pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue; } } } }