using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HEAL.Attic;
namespace HeuristicLab.Problems.MetaOptimization {
///
///
///
[Item("ParameterConfigurationCrossover", "TODO")]
[StorableType("586A425D-E8FD-4C96-AF82-ECFC03FFE288")]
public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, 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[MetaOptimizationProblem.IntValueCrossoverParameterName]; }
}
public IValueLookupParameter DoubleValueCrossoverParameter {
get { return (IValueLookupParameter)Parameters[MetaOptimizationProblem.DoubleValueCrossoverParameterName]; }
}
///
/// 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"]; }
}
[StorableConstructor]
protected ParameterConfigurationCrossover(StorableConstructorFlag _) : base(_) { }
protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
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(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
Parameters.Add(new ValueLookupParameter(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ParameterConfigurationCrossover(this, cloner);
}
public override IOperation Apply() {
if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: 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 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1];
} else {
child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0];
}
child1.Cross(RandomParameter.ActualValue, child2, Cross, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
this.ChildParameter.ActualValue = child1;
return base.Apply();
}
public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
configuartion.Cross(random, other, Cross, intValueCrossover, doubleValueCrossover);
}
public static void Cross(IRandom random, IOptimizable configuration, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
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) {
intValueCrossover.Apply(random, (IntValue)value, (IntValue)((IValueConfiguration)other).ActualValue.Value, (IntValueRange)range);
} else if (value is PercentValue) {
doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
} else if (value is DoubleValue) {
doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, (DoubleValueRange)range);
}
} else if (pc != null) {
if (random.NextDouble() > 0.5) {
pc.ActualValueConfigurationIndex = ((ParameterConfiguration)other).ActualValueConfigurationIndex;
}
pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
}
}
private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
RandomParameter.ActualValue,
new ItemArray(
new IntegerVector[] {
new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.ActualValue.Value).Value })),
new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.ActualValue.Value).Value }))
}));
return new IntValue(integerChild[0]);
}
}
}