#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) {
if (configuration is ParameterizedValueConfiguration) {
var configVc = (ParameterizedValueConfiguration)configuration;
for (int i = 0; i < configVc.ParameterConfigurations.Count; i++)
Mutate(random, configVc.ParameterConfigurations.ElementAt(i), intValueManipulator, doubleValueManipulator);
} else if (configuration is ParameterConfiguration) {
var configPc = (IParameterConfiguration)configuration;
if (configPc.Optimize) {
foreach (var vc in configPc.ValueConfigurations)
if (configPc.ValueConfigurations.ItemChecked(vc))
if (vc.Optimize) Mutate(random, vc, intValueManipulator, doubleValueManipulator);
do configPc.ActualValueConfigurationIndex = random.Next(configPc.ValueConfigurations.Count);
while (!configPc.ValueConfigurations.ItemChecked(configPc.ValueConfigurations[configPc.ActualValueConfigurationIndex]));
configPc.ActualValue = configPc.ValueConfigurations[configPc.ActualValueConfigurationIndex].ActualValue;
}
} else if (configuration is RangeValueConfiguration) {
var configVc = (RangeValueConfiguration)configuration;
var value = configVc.ActualValue.Value;
var range = configVc.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);
}
}
}
}