Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • added NormalCrossover operators
  • renamed MedianQualityAnalyzer to ReferenceQualityAnalyzer
File size: 7.0 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;
10using System;
11
12namespace 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    /// <summary>
41    /// Whether the problem is a maximization or minimization problem.
42    /// </summary>
43    public ValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46
47    /// <summary>
48    /// The quality of the parents.
49    /// </summary>
50    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53
54    [StorableConstructor]
55    protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
56    protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
57    public ParameterConfigurationCrossover()
58      : base() {
59      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
60      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
61      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
62      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
63      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
64      Parameters.Add(new ValueLookupParameter<IIntValueCrossover>(MetaOptimizationProblem.IntValueCrossoverParameterName, ""));
65      Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>(MetaOptimizationProblem.DoubleValueCrossoverParameterName, ""));
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new ParameterConfigurationCrossover(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
73      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.");
74      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
75      bool maximization = MaximizationParameter.ActualValue.Value;
76
77      ParameterConfigurationTree child1;
78      ParameterConfigurationTree child2;
79
80      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) {
81        child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
82        child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1];
83      } else {
84        child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
85        child2 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0];
86      }
87
88      child1.Cross(RandomParameter.ActualValue, child2, Cross, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
89      this.ChildParameter.ActualValue = child1;
90
91      return base.Apply();
92    }
93
94    public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
95      configuartion.Cross(random, other, Cross, intValueCrossover, doubleValueCrossover);
96    }
97
98    private static void Cross(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
99      var vc = configuartion as IValueConfiguration;
100      var pc = configuartion as IParameterConfiguration;
101      if (vc != null) {
102        var value = vc.ActualValue.Value;
103        var range = vc.RangeConstraint;
104
105        if (value is IntValue) {
106          intValueCrossover.Apply(random, (IntValue)value, (IntValue)((IValueConfiguration)other).ActualValue.Value, (IntValueRange)range);
107        } else if (value is PercentValue) {
108          doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
109        } else if (value is DoubleValue) {
110          doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)((IValueConfiguration)other).ActualValue.Value, (DoubleValueRange)range);
111        }
112      } else if (pc != null) {
113        if (random.NextDouble() > 0.5) {
114          pc.ActualValueConfigurationIndex = ((ParameterConfiguration)other).ActualValueConfigurationIndex;
115        }
116        pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
117      }
118    }
119
120    private IntValue CrossInteger(IParameterConfiguration parameter1, IParameterConfiguration parameter2) {
121      IntegerVector integerChild = HeuristicLab.Encodings.IntegerVectorEncoding.DiscreteCrossover.Apply(
122        RandomParameter.ActualValue,
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      return new IntValue(integerChild[0]);
126    }
127
128  }
129}
Note: See TracBrowser for help on using the repository browser.