#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using HeuristicLab.Core; using HeuristicLab.Encodings.BinaryVectorEncoding; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Encodings.RealVectorEncoding; namespace HeuristicLab.Problems.Programmable { internal static class Helper { internal static Individual Extract(IScope scope, Encoding encoding) { Dictionary binDict = null; Dictionary intDict = null; Dictionary realDict = null; Dictionary permDict = null; var multiEncoding = encoding as MultiEncoding; if (multiEncoding != null) { foreach (var enc in multiEncoding.Encodings) { var binConfig = enc as BinaryEncoding; if (binConfig != null) { if (binDict == null) binDict = new Dictionary(); binDict.Add(enc.Name, (BinaryVector)scope.Variables[enc.Name].Value); continue; } var intConfig = enc as IntegerEncoding; if (intConfig != null) { if (intDict == null) intDict = new Dictionary(); intDict.Add(enc.Name, (IntegerVector)scope.Variables[enc.Name].Value); continue; } var realConfig = enc as RealEncoding; if (realConfig != null) { if (realDict == null) realDict = new Dictionary(); realDict.Add(enc.Name, (RealVector)scope.Variables[enc.Name].Value); continue; } var permConfig = enc as PermutationEncoding; if (permConfig != null) { if (permDict == null) permDict = new Dictionary(); permDict.Add(enc.Name, (Permutation)scope.Variables[enc.Name].Value); continue; } throw new InvalidOperationException("Encoding for variable " + enc.Name + " not recognized."); } } else { var binaryEncoding = encoding as BinaryEncoding; var handled = binaryEncoding != null; if (binaryEncoding != null) binDict = new Dictionary(capacity: 1) { {binaryEncoding.Name, (BinaryVector)scope.Variables[binaryEncoding.Name].Value} }; var integerEncoding = encoding as IntegerEncoding; handled = handled || integerEncoding != null; if (integerEncoding != null) intDict = new Dictionary(capacity: 1) { {integerEncoding.Name, (IntegerVector)scope.Variables[integerEncoding.Name].Value} }; var realEncoding = encoding as RealEncoding; handled = handled || realEncoding != null; if (realEncoding != null) realDict = new Dictionary(capacity: 1) { {realEncoding.Name, (RealVector)scope.Variables[realEncoding.Name].Value} }; var permEncoding = encoding as PermutationEncoding; handled = handled || permEncoding != null; if (permEncoding != null) permDict = new Dictionary(capacity: 1) { {permEncoding.ItemName, (Permutation)scope.Variables[permEncoding.ItemName].Value} }; if (!handled) throw new InvalidOperationException("Encoding for variable " + encoding.Name + " not recognized."); } return new Individual(binDict, intDict, realDict, permDict); } internal static void Write(IScope scope, Individual vector) { foreach (var param in vector.BinaryNames) { if (scope.Variables.ContainsKey(param)) scope.Variables[param].Value = vector.BinaryVector(param); else scope.Variables.Add(new Variable(param, vector.BinaryVector(param))); } foreach (var param in vector.IntegerNames) { if (scope.Variables.ContainsKey(param)) scope.Variables[param].Value = vector.IntegerVector(param); else scope.Variables.Add(new Variable(param, vector.IntegerVector(param))); } foreach (var param in vector.RealNames) { if (scope.Variables.ContainsKey(param)) scope.Variables[param].Value = vector.RealVector(param); else scope.Variables.Add(new Variable(param, vector.RealVector(param))); } foreach (var param in vector.PermutationNames) { if (scope.Variables.ContainsKey(param)) scope.Variables[param].Value = vector.Permutation(param); else scope.Variables.Add(new Variable(param, vector.Permutation(param))); } } } }