Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/Crossovers/ParameterConfigurationCrossover.cs @ 8590

Last change on this file since 8590 was 8590, checked in by jkarder, 12 years ago

#1853:

  • added MultiDoubleValueCrossover
  • fixed bug in AverageDoubleValueCrossover
  • minor code improvements
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
31  /// <summary>
32  /// An operator which crosses two sets of parameters.
33  /// </summary>
34  [Item("ParameterConfigurationCrossover", "An operator which crosses two sets of parameters.")]
35  [StorableClass]
36  public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationCrossover {
37    public override bool CanChangeName {
38      get { return false; }
39    }
40
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public ILookupParameter<ItemArray<ParameterConfigurationTree>> ParentsParameter {
45      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
46    }
47    public ILookupParameter<ParameterConfigurationTree> ChildParameter {
48      get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["Child"]; }
49    }
50
51    public IValueLookupParameter<IIntValueCrossover> IntValueCrossoverParameter {
52      get { return (IValueLookupParameter<IIntValueCrossover>)Parameters["IntValueCrossover"]; }
53    }
54    public IValueLookupParameter<IDoubleValueCrossover> DoubleValueCrossoverParameter {
55      get { return (IValueLookupParameter<IDoubleValueCrossover>)Parameters["DoubleValueCrossover"]; }
56    }
57
58    /// <summary>
59    /// Whether the problem is a maximization or minimization problem.
60    /// </summary>
61    public ValueLookupParameter<BoolValue> MaximizationParameter {
62      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
63    }
64
65    /// <summary>
66    /// The quality of the parents.
67    /// </summary>
68    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
69      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
70    }
71
72    #region Constructors and Cloning
73    public ParameterConfigurationCrossover()
74      : base() {
75      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
76      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
77      Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
78      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
80      Parameters.Add(new ValueLookupParameter<IIntValueCrossover>("IntValueCrossover", ""));
81      Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>("DoubleValueCrossover", ""));
82    }
83
84    [StorableConstructor]
85    protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
86    protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new ParameterConfigurationCrossover(this, cloner);
89    }
90    #endregion
91
92    public override IOperation Apply() {
93      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("ParameterConfigurationCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
94      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.");
95      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
96      bool maximization = MaximizationParameter.ActualValue.Value;
97
98      ParameterConfigurationTree child1;
99      ParameterConfigurationTree child2;
100
101      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) {
102        child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
103        child2 = ParentsParameter.ActualValue[1];
104      } else {
105        child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
106        child2 = ParentsParameter.ActualValue[0];
107      }
108
109      Cross(RandomParameter.ActualValue, child1, child2, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
110      ChildParameter.ActualValue = child1;
111
112      return base.Apply();
113    }
114
115    public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
116      Cross(random, configuartion, other, intValueCrossover, doubleValueCrossover);
117    }
118
119    public static void Cross(IRandom random, IOptimizable configuration, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
120      var vc = configuration as RangeValueConfiguration;
121      var pc = configuration as IParameterConfiguration;
122      if (vc != null) {
123        var value = vc.ActualValue.Value;
124        var range = vc.RangeConstraint;
125
126        if (value is IntValue) {
127          intValueCrossover.Apply(random, (IntValue)value, (IntValue)other.ActualValue.Value, (IntValueRange)range);
128        } else if (value is PercentValue) {
129          doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)other.ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
130        } else if (value is DoubleValue) {
131          doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)other.ActualValue.Value, (DoubleValueRange)range);
132        }
133      } else if (pc != null) {
134        if (random.NextDouble() > 0.5) {
135          pc.ActualValueConfigurationIndex = ((ParameterConfiguration)other).ActualValueConfigurationIndex;
136        }
137        pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.