Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Manipulators/ParameterConfigurationManipulator.cs @ 5277

Last change on this file since 5277 was 5277, checked in by cneumuel, 14 years ago

#1215

  • implemented crossover and manipulator operators for int and double values
File size: 4.2 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, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue);
52      return base.Apply();
53    }
54
55    public static void Apply(IRandom random, IValueConfiguration configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
56      configuration.Mutate(random, Mutate, intValueManipulator, doubleValueManipulator);
57    }
58
59    private static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
60      var vc = configuration as IValueConfiguration;
61      var pc = configuration as IParameterConfiguration;
62
63      if (vc != null) {
64        var value = vc.ActualValue.Value;
65        var range = vc.RangeConstraint;
66        if (value is IntValue) {
67          intValueManipulator.Apply(random, (IntValue)value, (IntValueRange)range);
68        } else if (value is PercentValue) {
69          doubleValueManipulator.Apply(random, (PercentValue)value, ((PercentValueRange)range).AsDoubleValueRange());
70        } else if (value is DoubleValue) {
71          doubleValueManipulator.Apply(random, (DoubleValue)value, (DoubleValueRange)range);
72        }
73      } else if (pc != null) {
74        do {
75          pc.ActualValueConfigurationIndex = random.Next(pc.ValueConfigurations.Count());
76        } while (!pc.ValueConfigurations.ItemChecked(pc.ValueConfigurations[pc.ActualValueConfigurationIndex]));
77        pc.ActualValue = pc.ValueConfigurations[pc.ActualValueConfigurationIndex].ActualValue;
78      }
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.