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;
|
---|
10 | using System;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | /// <summary>
|
---|
14 | ///
|
---|
15 | /// </summary>
|
---|
16 | [Item("ParameterConfigurationCrossover", "TODO")]
|
---|
17 | [StorableClass]
|
---|
18 | public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, IParameterConfigurationCrossover {
|
---|
19 | public override bool CanChangeName {
|
---|
20 | get { return false; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public ILookupParameter<IRandom> RandomParameter {
|
---|
24 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
25 | }
|
---|
26 | public ILookupParameter<ItemArray<ParameterConfigurationTree>> ParentsParameter {
|
---|
27 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
|
---|
28 | }
|
---|
29 | public ILookupParameter<ParameterConfigurationTree> ChildParameter {
|
---|
30 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["Child"]; }
|
---|
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 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
|
---|
42 | protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
43 | public ParameterConfigurationCrossover()
|
---|
44 | : base() {
|
---|
45 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
46 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
|
---|
47 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
|
---|
48 |
|
---|
49 | Parameters.Add(new ValueLookupParameter<IIntValueCrossover>(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
|
---|
50 | Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
|
---|
51 | }
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new ParameterConfigurationCrossover(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IOperation Apply() {
|
---|
57 | ParameterConfigurationTree child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
|
---|
58 | ParameterConfigurationTree child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1];
|
---|
59 |
|
---|
60 | child1.Cross(RandomParameter.ActualValue, child2, Cross, this);
|
---|
61 | this.ChildParameter.ActualValue = child1;
|
---|
62 |
|
---|
63 | return base.Apply();
|
---|
64 | }
|
---|
65 |
|
---|
66 | private static void Cross(IRandom random, IOptimizable configuartion, ParameterConfigurationCrossover pccross) {
|
---|
67 | if (configuartion is IValueConfiguration) {
|
---|
68 | var vc = configuartion as IValueConfiguration;
|
---|
69 | var value = vc.ActualValue.Value;
|
---|
70 | var range = vc.RangeConstraint;
|
---|
71 |
|
---|
72 | if (value is IntValue) {
|
---|
73 | pccross.IntValueCrossoverParameter.ActualValue.Apply(random, (IntValue)value, (IntValueRange)range);
|
---|
74 | } else if (value is PercentValue) {
|
---|
75 | pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
|
---|
76 | } else if (value is DoubleValue) {
|
---|
77 | pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
|
---|
78 | }
|
---|
79 | } else if (configuartion is IParameterConfiguration) {
|
---|
80 |
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
|
---|
85 | IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
|
---|
86 | RandomParameter.ActualValue,
|
---|
87 | new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.ActualValue.Value).Value })),
|
---|
88 | new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.ActualValue.Value).Value })));
|
---|
89 | return new IntValue(integerChild[0]);
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 | }
|
---|