1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Common;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
14 | // todo: item name/descr...
|
---|
15 | [StorableClass]
|
---|
16 | public abstract 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 |
|
---|
47 | public override IOperation Apply() {
|
---|
48 | Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue);
|
---|
49 | return base.Apply();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static void Apply(IRandom random, IValueConfiguration configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
|
---|
53 | configuration.Mutate(random, Mutate, intValueManipulator, doubleValueManipulator);
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
|
---|
57 | var vc = configuration as IValueConfiguration;
|
---|
58 | var pc = configuration as IParameterConfiguration;
|
---|
59 |
|
---|
60 | if (vc != null) {
|
---|
61 | var value = vc.ActualValue.Value;
|
---|
62 | var range = vc.RangeConstraint;
|
---|
63 | if (value is IntValue) {
|
---|
64 | intValueManipulator.Apply(random, (IntValue)value, (IntValueRange)range);
|
---|
65 | } else if (value is PercentValue) {
|
---|
66 | doubleValueManipulator.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
|
---|
67 | } else if (value is DoubleValue) {
|
---|
68 | doubleValueManipulator.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
|
---|
69 | }
|
---|
70 | } else if (pc != null) {
|
---|
71 | do {
|
---|
72 | pc.ActualValueConfigurationIndex = random.Next(pc.ValueConfigurations.Count());
|
---|
73 | } while (!pc.ValueConfigurations.ItemChecked(pc.ValueConfigurations[pc.ActualValueConfigurationIndex]));
|
---|
74 | pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | } |
---|