Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Crossovers/ParameterConfigurationCrossover.cs @ 5184

Last change on this file since 5184 was 5184, checked in by cneumuel, 13 years ago

#1215

  • enhanced combinations generator (now with batchruns!)
  • fixed ActualNames for metaopt-alg
  • added penalty for invalid solution-candidates (algs which throw exceptions)
  • migrated to .NET 4.0
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, IParameterConfigurationCrossover {
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<ParameterConfigurationTree>> ParentsParameter {
26      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
27    }
28    public ILookupParameter<ParameterConfigurationTree> ChildParameter {
29      get { return (ILookupParameter<ParameterConfigurationTree>)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<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
46      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
47
48      Parameters.Add(new ValueLookupParameter<IIntValueCrossover>(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
49      Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
50    }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new ParameterConfigurationCrossover(this, cloner);
53    }
54
55    public override IOperation Apply() {
56      ParameterConfigurationTree child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
57      ParameterConfigurationTree child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1];
58
59      //child1.Cross(child2, RandomParameter.ActualValue);
60      //this.ChildParameter.ActualValue = child1;
61
62      child1.Cross(RandomParameter.ActualValue, child2, Cross, this);
63      this.ChildParameter.ActualValue = child1;
64
65      return base.Apply();
66    }
67
68    private static void Cross(IRandom random, IOptimizable configuartion, ParameterConfigurationCrossover pccross) {
69      if (configuartion is IValueConfiguration) {
70        var vc = configuartion as IValueConfiguration;
71        var value = vc.ActualValue.Value;
72        var range = vc.RangeConstraint;
73
74        if (value is IntValue) {
75          pccross.IntValueCrossoverParameter.ActualValue.Apply(random, (IntValue)value, (IntValueRange)range);
76        } else if (value is PercentValue) {
77          pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
78        } else if (value is DoubleValue) {
79          pccross.DoubleValueCrossoverParameter.ActualValue.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
80        }
81      } else if (configuartion is IParameterConfiguration) {
82
83      }
84    }
85
86    private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
87      IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
88        RandomParameter.ActualValue,
89        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter1.ActualValue.Value).Value })),
90        new IntegerVector(new IntArray(new int[] { ((IntValue)parameter2.ActualValue.Value).Value })));
91      return new IntValue(integerChild[0]);
92    }
93
94  }
95}
Note: See TracBrowser for help on using the repository browser.