[8574] | 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 |
|
---|
| 22 | using System;
|
---|
[8665] | 23 | using System.Linq;
|
---|
[8574] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// An operator which crosses two sets of parameters.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("ParameterConfigurationCrossover", "An operator which crosses two sets of parameters.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public class ParameterConfigurationCrossover : SingleSuccessorOperator, IParameterConfigurationCrossover {
|
---|
| 38 | public override bool CanChangeName {
|
---|
| 39 | get { return false; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 43 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 44 | }
|
---|
| 45 | public ILookupParameter<ItemArray<ParameterConfigurationTree>> ParentsParameter {
|
---|
| 46 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["Parents"]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<ParameterConfigurationTree> ChildParameter {
|
---|
| 49 | get { return (ILookupParameter<ParameterConfigurationTree>)Parameters["Child"]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public IValueLookupParameter<IIntValueCrossover> IntValueCrossoverParameter {
|
---|
| 53 | get { return (IValueLookupParameter<IIntValueCrossover>)Parameters["IntValueCrossover"]; }
|
---|
| 54 | }
|
---|
| 55 | public IValueLookupParameter<IDoubleValueCrossover> DoubleValueCrossoverParameter {
|
---|
| 56 | get { return (IValueLookupParameter<IDoubleValueCrossover>)Parameters["DoubleValueCrossover"]; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /// <summary>
|
---|
| 60 | /// Whether the problem is a maximization or minimization problem.
|
---|
| 61 | /// </summary>
|
---|
| 62 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 63 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /// <summary>
|
---|
| 67 | /// The quality of the parents.
|
---|
| 68 | /// </summary>
|
---|
| 69 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 70 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | #region Constructors and Cloning
|
---|
| 74 | public ParameterConfigurationCrossover()
|
---|
| 75 | : base() {
|
---|
| 76 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic crossover operators."));
|
---|
| 77 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("Parents", "The parent vectors which should be crossed."));
|
---|
| 78 | Parameters.Add(new LookupParameter<ParameterConfigurationTree>("Child", "The child vector resulting from the crossover."));
|
---|
| 79 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
| 80 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
| 81 | Parameters.Add(new ValueLookupParameter<IIntValueCrossover>("IntValueCrossover", ""));
|
---|
| 82 | Parameters.Add(new ValueLookupParameter<IDoubleValueCrossover>("DoubleValueCrossover", ""));
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [StorableConstructor]
|
---|
| 86 | protected ParameterConfigurationCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 87 | protected ParameterConfigurationCrossover(ParameterConfigurationCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 89 | return new ParameterConfigurationCrossover(this, cloner);
|
---|
| 90 | }
|
---|
| 91 | #endregion
|
---|
| 92 |
|
---|
| 93 | public override IOperation Apply() {
|
---|
| 94 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("ParameterConfigurationCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
| 95 | 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.");
|
---|
| 96 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 97 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 98 |
|
---|
| 99 | ParameterConfigurationTree child1;
|
---|
| 100 | ParameterConfigurationTree child2;
|
---|
| 101 |
|
---|
| 102 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value) {
|
---|
| 103 | child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[0].Clone();
|
---|
| 104 | child2 = ParentsParameter.ActualValue[1];
|
---|
| 105 | } else {
|
---|
| 106 | child1 = (ParameterConfigurationTree)ParentsParameter.ActualValue[1].Clone();
|
---|
| 107 | child2 = ParentsParameter.ActualValue[0];
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | Cross(RandomParameter.ActualValue, child1, child2, IntValueCrossoverParameter.ActualValue, DoubleValueCrossoverParameter.ActualValue);
|
---|
[8590] | 111 | ChildParameter.ActualValue = child1;
|
---|
[8574] | 112 |
|
---|
| 113 | return base.Apply();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public static void Apply(IRandom random, IOptimizable configuartion, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
| 117 | Cross(random, configuartion, other, intValueCrossover, doubleValueCrossover);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public static void Cross(IRandom random, IOptimizable configuration, IOptimizable other, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
[8665] | 121 | if (configuration is ParameterizedValueConfiguration) {
|
---|
| 122 | var configVc = (ParameterizedValueConfiguration)configuration;
|
---|
| 123 | var otherVc = (ParameterizedValueConfiguration)other;
|
---|
| 124 | for (int i = 0; i < configVc.ParameterConfigurations.Count; i++)
|
---|
| 125 | Cross(random, configVc.ParameterConfigurations.ElementAt(i), otherVc.ParameterConfigurations.ElementAt(i), intValueCrossover, doubleValueCrossover);
|
---|
| 126 | } else if (configuration is ParameterConfiguration) {
|
---|
| 127 | var configPc = (IParameterConfiguration)configuration;
|
---|
| 128 | if (configPc.Optimize) {
|
---|
| 129 | var otherPc = (IParameterConfiguration)other;
|
---|
| 130 | for (int i = 0; i < configPc.ValueConfigurations.Count; i++)
|
---|
| 131 | if (configPc.ValueConfigurations.ItemChecked(configPc.ValueConfigurations[i]))
|
---|
| 132 | if (configPc.ValueConfigurations[i].Optimize) Cross(random, configPc.ValueConfigurations[i], otherPc.ValueConfigurations[i], intValueCrossover, doubleValueCrossover);
|
---|
| 133 | if (random.NextDouble() > 0.5) configPc.ActualValueConfigurationIndex = otherPc.ActualValueConfigurationIndex;
|
---|
| 134 | configPc.ActualValue = configPc.ValueConfigurations[configPc.ActualValueConfigurationIndex].ActualValue;
|
---|
| 135 | }
|
---|
| 136 | } else if (configuration is RangeValueConfiguration) {
|
---|
| 137 | var configVc = (RangeValueConfiguration)configuration;
|
---|
| 138 | var value = configVc.ActualValue.Value;
|
---|
| 139 | var range = configVc.RangeConstraint;
|
---|
| 140 | if (value is IntValue)
|
---|
[8574] | 141 | intValueCrossover.Apply(random, (IntValue)value, (IntValue)other.ActualValue.Value, (IntValueRange)range);
|
---|
[8665] | 142 | else if (value is PercentValue)
|
---|
[8574] | 143 | doubleValueCrossover.Apply(random, (PercentValue)value, (DoubleValue)other.ActualValue.Value, ((PercentValueRange)range).AsDoubleValueRange());
|
---|
[8665] | 144 | else if (value is DoubleValue)
|
---|
[8574] | 145 | doubleValueCrossover.Apply(random, (DoubleValue)value, (DoubleValue)other.ActualValue.Value, (DoubleValueRange)range);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|