1 | using System;
|
---|
2 | using HeuristicLab.Common;
|
---|
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]
|
---|
16 | public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, IParameterConfigurationCrossover {
|
---|
17 | public override bool CanChangeName {
|
---|
18 | get { return false; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public ILookupParameter<IRandom> RandomParameter {
|
---|
22 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
23 | }
|
---|
24 | public ILookupParameter<ItemArray<ParameterConfigurationTree>> ParentsParameter {
|
---|
25 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
|
---|
26 | }
|
---|
27 | public ILookupParameter<ParameterConfigurationTree> ChildParameter {
|
---|
28 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["Child"]; }
|
---|
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 |
|
---|
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 |
|
---|
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."));
|
---|
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."));
|
---|
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."));
|
---|
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() {
|
---|
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;
|
---|
74 |
|
---|
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 |
|
---|
86 | child1.Cross(RandomParameter.ActualValue, child2, Cross, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
|
---|
87 | this.ChildParameter.ActualValue = child1;
|
---|
88 |
|
---|
89 | return base.Apply();
|
---|
90 | }
|
---|
91 |
|
---|
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 |
|
---|
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;
|
---|
99 | if (vc != null) {
|
---|
100 | var value = vc.ActualValue.Value;
|
---|
101 | var range = vc.RangeConstraint;
|
---|
102 |
|
---|
103 | if (value is IntValue) {
|
---|
104 | intValueCrossover.Apply(random, (IntValue)value, (IntValue)((IValueConfiguration)other).ActualValue.Value, (IntValueRange)range);
|
---|
105 | } else if (value is PercentValue) {
|
---|
106 | doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
|
---|
107 | } else if (value is DoubleValue) {
|
---|
108 | doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, (DoubleValueRange)range);
|
---|
109 | }
|
---|
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;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
|
---|
119 | IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
|
---|
120 | RandomParameter.ActualValue,
|
---|
121 | new ItemArray<IntegerVector>(
|
---|
122 | new IntegerVector[] {
|
---|
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 | }));
|
---|
126 | return new IntValue(integerChild[0]);
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|
130 | }
|
---|