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

Last change on this file since 8574 was 8574, checked in by jkarder, 9 years ago

#1853:

  • extracted experiment generation from encoding
  • added creators
  • added crossovers
  • added manipulators
  • added support for parameters of type IFixedValueParameter
  • minor code improvements
File size: 4.8 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      var vc = configuration as RangeValueConfiguration;
80      var pc = configuration as IParameterConfiguration;
81
82      if (vc != null) {
83        var value = vc.ActualValue.Value;
84        var range = vc.RangeConstraint;
85        if (value is IntValue) {
86          intValueManipulator.Apply(random, (IntValue)value, (IntValueRange)range);
87        } else if (value is PercentValue) {
88          doubleValueManipulator.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
89        } else if (value is DoubleValue) {
90          doubleValueManipulator.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
91        }
92      } else if (pc != null) {
93        do {
94          pc.ActualValueConfigurationIndex = random.Next(pc.ValueConfigurations.Count());
95        } while (!pc.ValueConfigurations.ItemChecked(pc.ValueConfigurations[pc.ActualValueConfigurationIndex]));
96        pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.