Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorMoveMaker.cs @ 11424

Last change on this file since 11424 was 11393, checked in by abeham, 10 years ago

#2174: enabled possibility to set different problem definitions than just scripted ones

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Programmable {
33  [Item("ParameterVectorMoveMaker", "Applies a move.")]
34  [StorableClass]
35  public class ParameterVectorMoveMaker : InstrumentedOperator, IParameterVectorMoveOperator, IMoveMaker {
36    public ILookupParameter<Configuration> ConfigurationParameter {
37      get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
38    }
39
40    public ILookupParameter<DoubleValue> QualityParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
42    }
43
44    public ILookupParameter<DoubleValue> MoveQualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
46    }
47
48    [StorableConstructor]
49    protected ParameterVectorMoveMaker(bool deserializing) : base(deserializing) { }
50    protected ParameterVectorMoveMaker(ParameterVectorMoveMaker original, Cloner cloner) : base(original, cloner) { }
51    public ParameterVectorMoveMaker() {
52      Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
53      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
54      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new ParameterVectorMoveMaker(this, cloner);
59    }
60
61    public override IOperation InstrumentedApply() {
62      var config = ConfigurationParameter.ActualValue;
63      var name = config.Parameters.First().Key;
64
65      var scope = ExecutionContext.Scope.Parent;
66      while (!scope.Variables.ContainsKey(name)) {
67        if (scope.Parent == null) throw new InvalidOperationException("Scope with solution could not be found!");
68        scope = scope.Parent;
69      }
70
71      foreach (var param in config.Parameters) {
72        if (!scope.Variables.ContainsKey(param.Key) || !ExecutionContext.Scope.Variables.ContainsKey(param.Key))
73          throw new InvalidOperationException("Parameter " + param.Key + " not found.");
74        scope.Variables[param.Key].Value = ExecutionContext.Scope.Variables[param.Key].Value;
75      }
76
77      if (MoveQualityParameter.ActualValue == null) throw new InvalidOperationException("Move has not been evaluated!");
78      if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
79      else QualityParameter.ActualValue.Value = MoveQualityParameter.ActualValue.Value;
80      return base.InstrumentedApply();
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.