#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;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
///
/// An operator which crosses two sets of parameters.
///
[Item("ParameterConfigurationCrossover", "An operator which crosses two sets of parameters.")]
[StorableClass]
public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationCrossover {
public override bool CanChangeName {
get { return false; }
}
public ILookupParameter RandomParameter {
get { return (LookupParameter)Parameters["Random"]; }
}
public ILookupParameter> ParentsParameter {
get { return (ScopeTreeLookupParameter)Parameters["Parents"]; }
}
public ILookupParameter ChildParameter {
get { return (ILookupParameter)Parameters["Child"]; }
}
public IValueLookupParameter IntValueCrossoverParameter {
get { return (IValueLookupParameter)Parameters["IntValueCrossover"]; }
}
public IValueLookupParameter DoubleValueCrossoverParameter {
get { return (IValueLookupParameter)Parameters["DoubleValueCrossover"]; }
}
///
/// Whether the problem is a maximization or minimization problem.
///
public ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
///
/// The quality of the parents.
///
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
#region Constructors and Cloning
public ParameterConfigurationCrossover()
: base() {
Parameters.Add(new LookupParameter("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
Parameters.Add(new ScopeTreeLookupParameter("Parents", "The parent vectors which should be crossed."));
Parameters.Add(new LookupParameter("Child", "The child vector resulting from the crossover."));
Parameters.Add(new ValueLookupParameter("Maximization", "Whether the problem is a maximization problem or not."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The quality values of the parents."));
Parameters.Add(new ValueLookupParameter("IntValueCrossover", ""));
Parameters.Add(new ValueLookupParameter("DoubleValueCrossover", ""));
}
[StorableConstructor]
protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new ParameterConfigurationCrossover(this, cloner);
}
#endregion
public override IOperation Apply() {
if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("ParameterConfigurationCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != 2) throw new InvalidOperationException("ParameterConfigurationCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
ItemArray qualities = QualityParameter.ActualValue;
bool maximization = MaximizationParameter.ActualValue.Value;
ParameterConfigurationTree child1;
ParameterConfigurationTree child2;
if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) {
child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
child2 = ParentsParameter.ActualValue[1];
} else {
child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
child2 = ParentsParameter.ActualValue[0];
}
Cross(RandomParameter.ActualValue, child1, child2, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
ChildParameter.ActualValue = child1;
return base.Apply();
}
public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
Cross(random, configuartion, other, intValueCrossover, doubleValueCrossover);
}
public static void Cross(IRandom random, IOptimizable configuration, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
if (configuration is ParameterizedValueConfiguration) {
var configVc = (ParameterizedValueConfiguration)configuration;
var otherVc = (ParameterizedValueConfiguration)other;
for (int i = 0; i < configVc.ParameterConfigurations.Count; i++)
Cross(random, configVc.ParameterConfigurations.ElementAt(i), otherVc.ParameterConfigurations.ElementAt(i), intValueCrossover, doubleValueCrossover);
} else if (configuration is ParameterConfiguration) {
var configPc = (IParameterConfiguration)configuration;
if (configPc.Optimize) {
var otherPc = (IParameterConfiguration)other;
for (int i = 0; i < configPc.ValueConfigurations.Count; i++)
if (configPc.ValueConfigurations.ItemChecked(configPc.ValueConfigurations[i]))
if (configPc.ValueConfigurations[i].Optimize) Cross(random, configPc.ValueConfigurations[i], otherPc.ValueConfigurations[i], intValueCrossover, doubleValueCrossover);
if (random.NextDouble() > 0.5) configPc.ActualValueConfigurationIndex = otherPc.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)
intValueCrossover.Apply(random, (IntValue)value, (IntValue)other.ActualValue.Value, (IntValueRange)range);
else if (value is PercentValue)
doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)other.ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
else if (value is DoubleValue)
doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)other.ActualValue.Value, (DoubleValueRange)range);
}
}
}
}