[11396] | 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 HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 26 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 31 | internal static class Helper {
|
---|
[11484] | 32 | internal static Individual Extract(IScope scope, Encoding encoding) {
|
---|
| 33 | Dictionary<string, BinaryVector> binDict = null;
|
---|
| 34 | Dictionary<string, IntegerVector> intDict = null;
|
---|
| 35 | Dictionary<string, RealVector> realDict = null;
|
---|
| 36 | Dictionary<string, Permutation> permDict = null;
|
---|
| 37 | var multiEncoding = encoding as MultiEncoding;
|
---|
| 38 | if (multiEncoding != null) {
|
---|
| 39 | foreach (var enc in multiEncoding.Encodings) {
|
---|
| 40 | var binConfig = enc as BinaryEncoding;
|
---|
| 41 | if (binConfig != null) {
|
---|
| 42 | if (binDict == null) binDict = new Dictionary<string, BinaryVector>();
|
---|
| 43 | binDict.Add(enc.Name, (BinaryVector)scope.Variables[enc.Name].Value);
|
---|
| 44 | continue;
|
---|
| 45 | }
|
---|
| 46 | var intConfig = enc as IntegerEncoding;
|
---|
| 47 | if (intConfig != null) {
|
---|
| 48 | if (intDict == null) intDict = new Dictionary<string, IntegerVector>();
|
---|
| 49 | intDict.Add(enc.Name, (IntegerVector)scope.Variables[enc.Name].Value);
|
---|
| 50 | continue;
|
---|
| 51 | }
|
---|
| 52 | var realConfig = enc as RealEncoding;
|
---|
| 53 | if (realConfig != null) {
|
---|
| 54 | if (realDict == null) realDict = new Dictionary<string, RealVector>();
|
---|
| 55 | realDict.Add(enc.Name, (RealVector)scope.Variables[enc.Name].Value);
|
---|
| 56 | continue;
|
---|
| 57 | }
|
---|
| 58 | var permConfig = enc as PermutationEncoding;
|
---|
| 59 | if (permConfig != null) {
|
---|
| 60 | if (permDict == null) permDict = new Dictionary<string, Permutation>();
|
---|
| 61 | permDict.Add(enc.Name, (Permutation)scope.Variables[enc.Name].Value);
|
---|
| 62 | continue;
|
---|
| 63 | }
|
---|
| 64 | throw new InvalidOperationException("Encoding for variable " + enc.Name + " not recognized.");
|
---|
[11396] | 65 | }
|
---|
[11484] | 66 | } else {
|
---|
| 67 | var binaryEncoding = encoding as BinaryEncoding;
|
---|
| 68 | var handled = binaryEncoding != null;
|
---|
| 69 | if (binaryEncoding != null) binDict = new Dictionary<string, BinaryVector>(capacity: 1) {
|
---|
| 70 | {binaryEncoding.Name, (BinaryVector)scope.Variables[binaryEncoding.Name].Value}
|
---|
| 71 | };
|
---|
| 72 | var integerEncoding = encoding as IntegerEncoding;
|
---|
| 73 | handled = handled || integerEncoding != null;
|
---|
| 74 | if (integerEncoding != null) intDict = new Dictionary<string, IntegerVector>(capacity: 1) {
|
---|
| 75 | {integerEncoding.Name, (IntegerVector)scope.Variables[integerEncoding.Name].Value}
|
---|
| 76 | };
|
---|
| 77 | var realEncoding = encoding as RealEncoding;
|
---|
| 78 | handled = handled || realEncoding != null;
|
---|
| 79 | if (realEncoding != null) realDict = new Dictionary<string, RealVector>(capacity: 1) {
|
---|
| 80 | {realEncoding.Name, (RealVector)scope.Variables[realEncoding.Name].Value}
|
---|
| 81 | };
|
---|
| 82 | var permEncoding = encoding as PermutationEncoding;
|
---|
| 83 | handled = handled || permEncoding != null;
|
---|
| 84 | if (permEncoding != null) permDict = new Dictionary<string, Permutation>(capacity: 1) {
|
---|
| 85 | {permEncoding.ItemName, (Permutation)scope.Variables[permEncoding.ItemName].Value}
|
---|
| 86 | };
|
---|
| 87 | if (!handled) throw new InvalidOperationException("Encoding for variable " + encoding.Name + " not recognized.");
|
---|
[11396] | 88 | }
|
---|
[11484] | 89 | return new Individual(binDict, intDict, realDict, permDict);
|
---|
[11396] | 90 | }
|
---|
[11405] | 91 |
|
---|
[11484] | 92 | internal static void Write(IScope scope, Individual vector) {
|
---|
[11405] | 93 | foreach (var param in vector.BinaryNames) {
|
---|
| 94 | if (scope.Variables.ContainsKey(param))
|
---|
| 95 | scope.Variables[param].Value = vector.BinaryVector(param);
|
---|
| 96 | else scope.Variables.Add(new Variable(param, vector.BinaryVector(param)));
|
---|
| 97 | }
|
---|
| 98 | foreach (var param in vector.IntegerNames) {
|
---|
| 99 | if (scope.Variables.ContainsKey(param))
|
---|
| 100 | scope.Variables[param].Value = vector.IntegerVector(param);
|
---|
| 101 | else scope.Variables.Add(new Variable(param, vector.IntegerVector(param)));
|
---|
| 102 | }
|
---|
| 103 | foreach (var param in vector.RealNames) {
|
---|
| 104 | if (scope.Variables.ContainsKey(param))
|
---|
| 105 | scope.Variables[param].Value = vector.RealVector(param);
|
---|
| 106 | else scope.Variables.Add(new Variable(param, vector.RealVector(param)));
|
---|
| 107 | }
|
---|
| 108 | foreach (var param in vector.PermutationNames) {
|
---|
| 109 | if (scope.Variables.ContainsKey(param))
|
---|
| 110 | scope.Variables[param].Value = vector.Permutation(param);
|
---|
| 111 | else scope.Variables.Add(new Variable(param, vector.Permutation(param)));
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[11396] | 114 | }
|
---|
| 115 | }
|
---|