Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/Crossovers/ParameterConfigurationCrossover.cs @ 5112

Last change on this file since 5112 was 5112, checked in by cneumuel, 14 years ago

#1215

  • resolving svn issue
File size: 4.7 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Encodings.IntegerVectorEncoding;
5using HeuristicLab.Encodings.RealVectorEncoding;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Problems.MetaOptimization {
12  /// <summary>
13  ///
14  /// </summary>
15  [Item("ParameterConfigurationCrossover", "TODO")]
16  [StorableClass]
17  public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationOperator, ICrossover {
18    public override bool CanChangeName {
19      get { return false; }
20    }
21
22    public ILookupParameter<IRandom> RandomParameter {
23      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
24    }
25    public ILookupParameter<ItemArray<IValueConfiguration>> ParentsParameter {
26      get { return (ScopeTreeLookupParameter<IValueConfiguration>)Parameters["Parents"]; }
27    }
28    public ILookupParameter<IValueConfiguration> ChildParameter {
29      get { return (ILookupParameter<IValueConfiguration>)Parameters["Child"]; }
30    }
31
32    public IValueLookupParameter<IIntValueCrossover> IntValueCrossoverParameter {
33      get { return (IValueLookupParameter<IIntValueCrossover>)Parameters[MetaOptimizationProblem.IntValueCrossoverParameterName]; }
34    }
35    public IValueLookupParameter<IDoubleValueCrossover> DoubleValueCrossoverParameter {
36      get { return (IValueLookupParameter<IDoubleValueCrossover>)Parameters[MetaOptimizationProblem.DoubleValueCrossoverParameterName]; }
37    }
38
39    [StorableConstructor]
40    protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
41    protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
42    public ParameterConfigurationCrossover()
43      : base() {
44      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
45      Parameters.Add(new ScopeTreeLookupParameter<IValueConfiguration>("Parents", "The parent vectors which should be crossed."));
46      ParentsParameter.ActualName = "ParameterConfigurationTree";
47      Parameters.Add(new LookupParameter<IValueConfiguration>("Child", "The child vector resulting from the crossover."));
48      ChildParameter.ActualName = "ParameterConfigurationTree";
49
50      Parameters.Add(new ValueLookupParameter<IIntValueCrossover>(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
51      Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
52    }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new ParameterConfigurationCrossover(this, cloner);
55    }
56
57    public override IOperation Apply() {
58      IValueConfiguration child1 = (IValueConfiguration)ParentsParameter.ActualValue[0].Clone();
59      IValueConfiguration child2 = (IValueConfiguration)ParentsParameter.ActualValue[1];
60
61      //child1.Cross(child2, RandomParameter.ActualValue);
62      //this.ChildParameter.ActualValue = child1;
63
64      child1.Cross(RandomParameter.ActualValue, child2, Cross, this);
65      this.ChildParameter.ActualValue = child1;
66
67      return base.Apply();
68    }
69
70    private static void Cross(IRandom random, IOptimizable configuartion, ParameterConfigurationCrossover pccross) {
71      if (configuartion is IValueConfiguration) {
72        var vc = configuartion as IValueConfiguration;
73        var value = vc.ActualValue.Value;
74        var range = vc.RangeConstraint;
75
76        if (value is IntValue) {
77          pccross.IntValueCrossoverParameter.ActualValue.Apply(random, (IntValue)value, (IntValueRange)range);
78        } else if (value is PercentValue) {
79          pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
80        } else if (value is DoubleValue) {
81          pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
82        }
83      } else if (configuartion is IParameterConfiguration) {
84
85      }
86    }
87
88    private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
89      IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
90        RandomParameter.ActualValue,
91        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.ActualValue.Value).Value })),
92        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.ActualValue.Value).Value })));
93      return new IntValue(integerChild[0]);
94    }
95
96  }
97}
Note: See TracBrowser for help on using the repository browser.