Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Crossovers/ParameterConfigurationCrossover.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

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