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