[5112] | 1 | using HeuristicLab.Common;
|
---|
| 2 | using HeuristicLab.Core;
|
---|
| 3 | using HeuristicLab.Data;
|
---|
| 4 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 5 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 6 | using HeuristicLab.Operators;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using HeuristicLab.Parameters;
|
---|
| 9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5207] | 10 | using System;
|
---|
[5112] | 11 |
|
---|
| 12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 13 | /// <summary>
|
---|
| 14 | ///
|
---|
| 15 | /// </summary>
|
---|
| 16 | [Item("ParameterConfigurationCrossover", "TODO")]
|
---|
| 17 | [StorableClass]
|
---|
[5184] | 18 | public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, IParameterConfigurationCrossover {
|
---|
[5112] | 19 | public override bool CanChangeName {
|
---|
| 20 | get { return false; }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 24 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 25 | }
|
---|
[5184] | 26 | public ILookupParameter<ItemArray<ParameterConfigurationTree>> ParentsParameter {
|
---|
| 27 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
|
---|
[5112] | 28 | }
|
---|
[5184] | 29 | public ILookupParameter<ParameterConfigurationTree> ChildParameter {
|
---|
| 30 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["Child"]; }
|
---|
[5112] | 31 | }
|
---|
| 32 |
|
---|
| 33 | public IValueLookupParameter<IIntValueCrossover> IntValueCrossoverParameter {
|
---|
| 34 | get { return (IValueLookupParameter<IIntValueCrossover>)Parameters[MetaOptimizationProblem.IntValueCrossoverParameterName]; }
|
---|
| 35 | }
|
---|
| 36 | public IValueLookupParameter<IDoubleValueCrossover> DoubleValueCrossoverParameter {
|
---|
| 37 | get { return (IValueLookupParameter<IDoubleValueCrossover>)Parameters[MetaOptimizationProblem.DoubleValueCrossoverParameterName]; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[5293] | 40 | /// <summary>
|
---|
| 41 | /// Whether the problem is a maximization or minimization problem.
|
---|
| 42 | /// </summary>
|
---|
| 43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// The quality of the parents.
|
---|
| 49 | /// </summary>
|
---|
| 50 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 51 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[5112] | 54 | [StorableConstructor]
|
---|
| 55 | protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 56 | protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 57 | public ParameterConfigurationCrossover()
|
---|
| 58 | : base() {
|
---|
| 59 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
[5184] | 60 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
|
---|
| 61 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
|
---|
[5293] | 62 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
| 63 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
[5112] | 64 | Parameters.Add(new ValueLookupParameter<IIntValueCrossover>(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
|
---|
| 65 | Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
|
---|
| 66 | }
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 68 | return new ParameterConfigurationCrossover(this, cloner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IOperation Apply() {
|
---|
[5293] | 72 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
| 73 | 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.");
|
---|
| 74 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 75 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
[5112] | 76 |
|
---|
[5293] | 77 | ParameterConfigurationTree child1;
|
---|
| 78 | ParameterConfigurationTree child2;
|
---|
| 79 |
|
---|
| 80 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) {
|
---|
| 81 | child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
|
---|
| 82 | child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1];
|
---|
| 83 | } else {
|
---|
| 84 | child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
|
---|
| 85 | child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0];
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[5277] | 88 | child1.Cross(RandomParameter.ActualValue, child2, Cross, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
|
---|
[5112] | 89 | this.ChildParameter.ActualValue = child1;
|
---|
| 90 |
|
---|
| 91 | return base.Apply();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[5277] | 94 | public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
| 95 | configuartion.Cross(random, other, Cross, intValueCrossover, doubleValueCrossover);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private static void Cross(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
| 99 | var vc = configuartion as IValueConfiguration;
|
---|
| 100 | var pc = configuartion as IParameterConfiguration;
|
---|
| 101 | if (vc != null) {
|
---|
[5112] | 102 | var value = vc.ActualValue.Value;
|
---|
| 103 | var range = vc.RangeConstraint;
|
---|
| 104 |
|
---|
| 105 | if (value is IntValue) {
|
---|
[5277] | 106 | intValueCrossover.Apply(random, (IntValue)value, (IntValue)((IValueConfiguration)other).ActualValue.Value, (IntValueRange)range);
|
---|
[5112] | 107 | } else if (value is PercentValue) {
|
---|
[5277] | 108 | doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
|
---|
[5112] | 109 | } else if (value is DoubleValue) {
|
---|
[5277] | 110 | doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, (DoubleValueRange)range);
|
---|
[5112] | 111 | }
|
---|
[5277] | 112 | } else if (pc != null) {
|
---|
| 113 | if (random.NextDouble() > 0.5) {
|
---|
| 114 | pc.ActualValueConfigurationIndex = ((ParameterConfiguration)other).ActualValueConfigurationIndex;
|
---|
| 115 | }
|
---|
| 116 | pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
|
---|
[5112] | 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
|
---|
| 121 | IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
|
---|
| 122 | RandomParameter.ActualValue,
|
---|
| 123 | new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.ActualValue.Value).Value })),
|
---|
| 124 | new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.ActualValue.Value).Value })));
|
---|
| 125 | return new IntValue(integerChild[0]);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | }
|
---|
| 129 | }
|
---|