Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/Manipulators/ParameterConfigurationManipulator.cs @ 14853

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

#1853: fixed execution of crossovers and manipulators

File size: 5.5 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
32  /// <summary>
33  /// TODO
34  /// </summary>
35  [Item("ParameterConfigurationManipulator", "TODO")]
36  [StorableClass]
37  public abstract class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator {
38    public override bool CanChangeName {
39      get { return false; }
40    }
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public IValueLookupParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
45      get { return (IValueLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
46    }
47
48    public IValueLookupParameter<IIntValueManipulator> IntValueManipulatorParameter {
49      get { return (IValueLookupParameter<IIntValueManipulator>)Parameters["IntValueManipulator"]; }
50    }
51    public IValueLookupParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
52      get { return (IValueLookupParameter<IDoubleValueManipulator>)Parameters["DoubleValueManipulator"]; }
53    }
54
55    #region Constructors and Cloning
56    public ParameterConfigurationManipulator()
57      : base() {
58      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
59      Parameters.Add(new ValueLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "The parameter configuration which should be manipulated."));
60      Parameters.Add(new ValueLookupParameter<IIntValueManipulator>("IntValueManipulator", ""));
61      Parameters.Add(new ValueLookupParameter<IDoubleValueManipulator>("DoubleValueManipulator", ""));
62    }
63
64    [StorableConstructor]
65    protected ParameterConfigurationManipulator(bool deserializing) : base(deserializing) { }
66    protected ParameterConfigurationManipulator(ParameterConfigurationManipulator original, Cloner cloner) : base(original, cloner) { }
67    #endregion
68
69    public override IOperation Apply() {
70      Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue);
71      return base.Apply();
72    }
73
74    public static void Apply(IRandom random, IValueConfiguration configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
75      Mutate(random, configuration, intValueManipulator, doubleValueManipulator);
76    }
77
78    public static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
79      if (configuration is ParameterizedValueConfiguration) {
80        var configVc = (ParameterizedValueConfiguration)configuration;
81        for (int i = 0; i < configVc.ParameterConfigurations.Count; i++)
82          Mutate(random, configVc.ParameterConfigurations.ElementAt(i), intValueManipulator, doubleValueManipulator);
83      } else if (configuration is ParameterConfiguration) {
84        var configPc = (IParameterConfiguration)configuration;
85        if (configPc.Optimize) {
86          foreach (var vc in configPc.ValueConfigurations)
87            if (configPc.ValueConfigurations.ItemChecked(vc))
88              if (vc.Optimize) Mutate(random, vc, intValueManipulator, doubleValueManipulator);
89          do configPc.ActualValueConfigurationIndex = random.Next(configPc.ValueConfigurations.Count);
90          while (!configPc.ValueConfigurations.ItemChecked(configPc.ValueConfigurations[configPc.ActualValueConfigurationIndex]));
91          configPc.ActualValue = configPc.ValueConfigurations[configPc.ActualValueConfigurationIndex].ActualValue;
92        }
93      } else if (configuration is RangeValueConfiguration) {
94        var configVc = (RangeValueConfiguration)configuration;
95        var value = configVc.ActualValue.Value;
96        var range = configVc.RangeConstraint;
97        if (value is IntValue)
98          intValueManipulator.Apply(random, (IntValue)value, (IntValueRange)range);
99        else if (value is PercentValue)
100          doubleValueManipulator.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
101        else if (value is DoubleValue)
102          doubleValueManipulator.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.