Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Manipulators/ParameterConfigurationManipulator.cs

Last change on this file was 16996, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.MetaOptimization for new persistence

File size: 4.0 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HEAL.Attic;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  // todo: item name/descr...
12  [StorableType("ACFF1652-4E03-4A30-9A93-331F32FC13EF")]
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(StorableConstructorFlag _) : base(_) { }
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}
Note: See TracBrowser for help on using the repository browser.