Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/Manipulators/ParameterConfigurationManipulator.cs @ 5112

Last change on this file since 5112 was 5112, checked in by cneumuel, 13 years ago

#1215

  • resolving svn issue
File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Core;
8using HeuristicLab.Parameters;
9using HeuristicLab.Common;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.Data;
12
13namespace HeuristicLab.Problems.MetaOptimization {
14  // todo: item name/descr...
15  [StorableClass]
16  public class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator {
17    public override bool CanChangeName {
18      get { return false; }
19    }
20    public ILookupParameter<IRandom> RandomParameter {
21      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
22    }
23    public IValueLookupParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
24      get { return (IValueLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
25    }
26
27    public IValueLookupParameter<IIntValueManipulator> IntValueManipulatorParameter {
28      get { return (IValueLookupParameter<IIntValueManipulator>)Parameters[MetaOptimizationProblem.IntValueManipulatorParameterName]; }
29    }
30    public IValueLookupParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
31      get { return (IValueLookupParameter<IDoubleValueManipulator>)Parameters[MetaOptimizationProblem.DoubleValueManipulatorParameterName]; }
32    }
33
34    public ParameterConfigurationManipulator() {
35      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
36      Parameters.Add(new ValueLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "The parameter configuration which should be manipulated."));
37
38      Parameters.Add(new ValueLookupParameter<IIntValueManipulator>(MetaOptimizationProblem.IntValueManipulatorParameterName, ""));
39      Parameters.Add(new ValueLookupParameter<IDoubleValueManipulator>(MetaOptimizationProblem.DoubleValueManipulatorParameterName, ""));
40    }
41    [StorableConstructor]
42    protected ParameterConfigurationManipulator(bool deserializing) : base(deserializing) { }
43    protected ParameterConfigurationManipulator(ParameterConfigurationManipulator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new ParameterConfigurationManipulator(this, cloner);
48    }
49
50    public sealed override IOperation Apply() {
51      Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, this);
52      return base.Apply();
53    }
54
55    public static void Apply(IRandom random, IValueConfiguration configuration, ParameterConfigurationManipulator pcmanip) {
56      configuration.Mutate(random, Mutate, pcmanip);
57    }
58
59    private static void Mutate(IRandom random, IOptimizable configuration, ParameterConfigurationManipulator pcmanip) {
60      if (configuration is IValueConfiguration) {
61        var vc = configuration as IValueConfiguration;
62        var value = vc.ActualValue.Value;
63        var range = vc.RangeConstraint;
64        if (value is IntValue) {
65          pcmanip.IntValueManipulatorParameter.ActualValue.Apply(random, (IntValue)value, (IntValueRange)range);
66        } else if (value is PercentValue) {
67          pcmanip.DoubleValueManipulatorParameter.ActualValue.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
68        } else if (value is DoubleValue) {
69          pcmanip.DoubleValueManipulatorParameter.ActualValue.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
70        }
71      } else if (configuration is IParameterConfiguration) {
72        var pc = configuration as IParameterConfiguration;
73        pc.ActualValueConfigurationIndex = random.Next(pc.ValueConfigurations.CheckedItems.Count());
74        pc.ActualValue = pc.ValueConfigurations.CheckedItems.ElementAt(pc.ActualValueConfigurationIndex).ActualValue;
75      }
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.