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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
32 | using HeuristicLab.Operators;
|
---|
33 | using HeuristicLab.Optimization;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.Programmable {
|
---|
38 | [Item("ParameterVectorMoveGenerator", "Calls the GetNeighbors method of the programmable definition to obtain the moves.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class ParameterVectorMoveGenerator : InstrumentedOperator, IParameterVectorMoveOperator, IMultiMoveGenerator, IStochasticOperator {
|
---|
41 |
|
---|
42 | public ILookupParameter<IRandom> RandomParameter {
|
---|
43 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
47 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public ILookupParameter<SingleObjectiveScript> ScriptParameter {
|
---|
51 | get { return (ILookupParameter<SingleObjectiveScript>)Parameters["Script"]; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ILookupParameter<Configuration> ConfigurationParameter {
|
---|
55 | get { return (ILookupParameter<Configuration>)Parameters["Configuration"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected ParameterVectorMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
60 | protected ParameterVectorMoveGenerator(ParameterVectorMoveGenerator original, Cloner cloner)
|
---|
61 | : base(original, cloner) { }
|
---|
62 | public ParameterVectorMoveGenerator() {
|
---|
63 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
64 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample."));
|
---|
65 | Parameters.Add(new LookupParameter<SingleObjectiveScript>("Script", "The script that will execute the evaluation function and define the parameter vector."));
|
---|
66 | Parameters.Add(new LookupParameter<Configuration>("Configuration", "An item that holds the problem's configuration."));
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
70 | return new ParameterVectorMoveGenerator(this, cloner);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override IOperation InstrumentedApply() {
|
---|
74 | var random = RandomParameter.ActualValue;
|
---|
75 | var runner = ScriptParameter.ActualValue;
|
---|
76 | if (runner.Instance == null) throw new InvalidOperationException("Script instance is null, maybe the code doesn't compile.");
|
---|
77 | var sampleSize = SampleSizeParameter.ActualValue.Value;
|
---|
78 | var config = ConfigurationParameter.ActualValue;
|
---|
79 | var binDict = new Dictionary<string, BinaryVector>();
|
---|
80 | var intDict = new Dictionary<string, IntegerVector>();
|
---|
81 | var realDict = new Dictionary<string, RealVector>();
|
---|
82 | var permDict = new Dictionary<string, Permutation>();
|
---|
83 | foreach (var param in config.Parameters) {
|
---|
84 | var binConfig = param.Value as BinaryParameterConfiguration;
|
---|
85 | if (binConfig != null) {
|
---|
86 | binDict.Add(param.Key, (BinaryVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
87 | continue;
|
---|
88 | }
|
---|
89 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
90 | if (intConfig != null) {
|
---|
91 | intDict.Add(param.Key, (IntegerVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
92 | continue;
|
---|
93 | }
|
---|
94 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
95 | if (realConfig != null) {
|
---|
96 | realDict.Add(param.Key, (RealVector)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
97 | continue;
|
---|
98 | }
|
---|
99 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
100 | if (permConfig != null) {
|
---|
101 | permDict.Add(param.Key, (Permutation)ExecutionContext.Scope.Variables[param.Key].Value);
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | throw new InvalidOperationException("Parameter " + param.Key + " not found.");
|
---|
105 | }
|
---|
106 | var vector = new ParameterVector(
|
---|
107 | binaryVectors: binDict.Count > 0 ? binDict : null,
|
---|
108 | integerVectors: intDict.Count > 0 ? intDict : null,
|
---|
109 | realVectors: realDict.Count > 0 ? realDict : null,
|
---|
110 | permutations: permDict.Count > 0 ? permDict : null);
|
---|
111 | var nbhood = runner.Instance.GetNeighbors(random, vector).Take(sampleSize).ToList();
|
---|
112 |
|
---|
113 | var moveScopes = new Scope[nbhood.Count];
|
---|
114 | for (int i = 0; i < moveScopes.Length; i++) {
|
---|
115 | moveScopes[i] = new Scope(i.ToString());
|
---|
116 | foreach (var param in nbhood[i].BinaryNames)
|
---|
117 | moveScopes[i].Variables.Add(new Variable(param, nbhood[i].Binary(param)));
|
---|
118 |
|
---|
119 | foreach (var param in nbhood[i].IntegerNames)
|
---|
120 | moveScopes[i].Variables.Add(new Variable(param, nbhood[i].Integer(param)));
|
---|
121 |
|
---|
122 | foreach (var param in nbhood[i].RealNames)
|
---|
123 | moveScopes[i].Variables.Add(new Variable(param, nbhood[i].Real(param)));
|
---|
124 |
|
---|
125 | foreach (var param in nbhood[i].PermutationNames)
|
---|
126 | moveScopes[i].Variables.Add(new Variable(param, nbhood[i].Permutation(param)));
|
---|
127 | }
|
---|
128 | ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
|
---|
129 |
|
---|
130 | return base.InstrumentedApply();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|