Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/ParameterVectorMoveGenerator.cs @ 11393

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

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

File size: 6.1 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Encodings.RealVectorEncoding;
32using HeuristicLab.Operators;
33using HeuristicLab.Optimization;
34using HeuristicLab.Parameters;
35using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
36
37namespace 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<ISingleObjectiveProblemDefinitionHost> ProblemDefinitionParameter {
51      get { return (ILookupParameter<ISingleObjectiveProblemDefinitionHost>)Parameters["ProblemDefinition"]; }
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<ISingleObjectiveProblemDefinitionHost>("ProblemDefinition", "The host that holds the problem definition."));
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 host = ProblemDefinitionParameter.ActualValue;
76      if (host.Instance == null) throw new InvalidOperationException("Problem definition instance is null.");
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 = host.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}
Note: See TracBrowser for help on using the repository browser.