Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Helper.cs @ 11405

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

#2174: Made single-objective programmable problem compatible with scatter search

File size: 3.9 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 HeuristicLab.Core;
25using HeuristicLab.Encodings.BinaryVectorEncoding;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Encodings.RealVectorEncoding;
29
30namespace HeuristicLab.Problems.Programmable {
31  internal static class Helper {
32    internal static ParameterVector Extract(IScope scope, Configuration config) {
33      var binDict = new Dictionary<string, BinaryVector>();
34      var intDict = new Dictionary<string, IntegerVector>();
35      var realDict = new Dictionary<string, RealVector>();
36      var permDict = new Dictionary<string, Permutation>();
37      foreach (var param in config.Parameters) {
38        var binConfig = param.Value as BinaryParameterConfiguration;
39        if (binConfig != null) {
40          binDict.Add(param.Key, (BinaryVector)scope.Variables[param.Key].Value);
41          continue;
42        }
43        var intConfig = param.Value as IntegerParameterConfiguration;
44        if (intConfig != null) {
45          intDict.Add(param.Key, (IntegerVector)scope.Variables[param.Key].Value);
46          continue;
47        }
48        var realConfig = param.Value as RealParameterConfiguration;
49        if (realConfig != null) {
50          realDict.Add(param.Key, (RealVector)scope.Variables[param.Key].Value);
51          continue;
52        }
53        var permConfig = param.Value as PermutationParameterConfiguration;
54        if (permConfig != null) {
55          permDict.Add(param.Key, (Permutation)scope.Variables[param.Key].Value);
56          continue;
57        }
58        throw new InvalidOperationException("Parameter " + param.Key + " not found.");
59      }
60      return new ParameterVector(
61        binaryVectors: binDict.Count > 0 ? binDict : null,
62        integerVectors: intDict.Count > 0 ? intDict : null,
63        realVectors: realDict.Count > 0 ? realDict : null,
64        permutations: permDict.Count > 0 ? permDict : null);
65    }
66
67    internal static void Write(IScope scope, ParameterVector vector) {
68      foreach (var param in vector.BinaryNames) {
69        if (scope.Variables.ContainsKey(param))
70          scope.Variables[param].Value = vector.BinaryVector(param);
71        else scope.Variables.Add(new Variable(param, vector.BinaryVector(param)));
72      }
73      foreach (var param in vector.IntegerNames) {
74        if (scope.Variables.ContainsKey(param))
75          scope.Variables[param].Value = vector.IntegerVector(param);
76        else scope.Variables.Add(new Variable(param, vector.IntegerVector(param)));
77      }
78      foreach (var param in vector.RealNames) {
79        if (scope.Variables.ContainsKey(param))
80          scope.Variables[param].Value = vector.RealVector(param);
81        else scope.Variables.Add(new Variable(param, vector.RealVector(param)));
82      }
83      foreach (var param in vector.PermutationNames) {
84        if (scope.Variables.ContainsKey(param))
85          scope.Variables[param].Value = vector.Permutation(param);
86        else scope.Variables.Add(new Variable(param, vector.Permutation(param)));
87      }
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.