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