#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 HeuristicLab.Core; using HeuristicLab.Encodings.BinaryVectorEncoding; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Encodings.RealVectorEncoding; namespace HeuristicLab.Problems.Programmable { public abstract class Individual { protected IEncoding Encoding { get; private set; } protected IScope Scope { get; private set; } public string Name { get { return Encoding.Name; } } protected Individual(IEncoding encoding, IScope scope) { Encoding = encoding; Scope = scope; } public abstract IItem this[string name] { get; set; } public Individual Copy() { return Copy(new Scope()); } internal abstract Individual Copy(IScope scope); protected static IItem ExtractScopeValue(string name, IScope scope) { if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); var value = scope.Variables[name].Value; if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name)); return value; } protected static void SetScopeValue(string name, IScope scope, IItem value) { if (value == null) throw new ArgumentNullException("value"); if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value)); else scope.Variables[name].Value = value; } } #region extension methods public static class IndividualExtensionMethods { public static BinaryVector BinaryVector(this Individual individual, string name) { return (BinaryVector)individual[name]; } public static IntegerVector IntegerVector(this Individual individual, string name) { return (IntegerVector)individual[name]; } public static Permutation Permutation(this Individual individual, string name) { return (Permutation)individual[name]; } public static RealVector RealVector(this Individual individual, string name) { return (RealVector)individual[name]; } } #endregion }