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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace 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<IRandom>("Random", "The random number generator to use."));
|
---|
53 | Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
|
---|
54 | Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
|
---|
55 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
|
---|
56 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move."));
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new ParameterVectorMoveMaker(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override IOperation InstrumentedApply() {
|
---|
64 | var config = ConfigurationParameter.ActualValue;
|
---|
65 | var name = config.Parameters.First().Key;
|
---|
66 |
|
---|
67 | var scope = ExecutionContext.Scope.Parent;
|
---|
68 | while (!scope.Variables.ContainsKey(name)) {
|
---|
69 | if (scope.Parent == null) throw new InvalidOperationException("Scope with solution could not be found!");
|
---|
70 | scope = scope.Parent;
|
---|
71 | }
|
---|
72 |
|
---|
73 | foreach (var param in config.Parameters) {
|
---|
74 | if (!scope.Variables.ContainsKey(param.Key) || !ExecutionContext.Scope.Variables.ContainsKey(param.Key))
|
---|
75 | throw new InvalidOperationException("Parameter " + param.Key + " not found.");
|
---|
76 | scope.Variables[param.Key].Value = ExecutionContext.Scope.Variables[param.Key].Value;
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (MoveQualityParameter.ActualValue == null) throw new InvalidOperationException("Move has not been evaluated!");
|
---|
80 | if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
|
---|
81 | else QualityParameter.ActualValue.Value = MoveQualityParameter.ActualValue.Value;
|
---|
82 | return base.InstrumentedApply();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|