[11484] | 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 |
|
---|
[11598] | 22 | using System;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
[11484] | 24 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 25 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.Programmable {
|
---|
[11619] | 30 | public abstract class Individual {
|
---|
[11737] | 31 | protected IEncoding Encoding { get; private set; }
|
---|
[11598] | 32 | protected IScope Scope { get; private set; }
|
---|
[11484] | 33 |
|
---|
[11619] | 34 | public string Name { get { return Encoding.Name; } }
|
---|
| 35 |
|
---|
| 36 | protected Individual(IEncoding encoding, IScope scope) {
|
---|
[11598] | 37 | Encoding = encoding;
|
---|
| 38 | Scope = scope;
|
---|
[11484] | 39 | }
|
---|
| 40 |
|
---|
[11619] | 41 | public abstract IItem this[string name] { get; set; }
|
---|
[11813] | 42 | public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
|
---|
[11484] | 43 |
|
---|
[11619] | 44 | public Individual Copy() {
|
---|
[11885] | 45 | return CopyToScope(new Scope());
|
---|
[11484] | 46 | }
|
---|
| 47 |
|
---|
[11885] | 48 | public abstract Individual CopyToScope(IScope scope);
|
---|
| 49 |
|
---|
[11619] | 50 | protected static IItem ExtractScopeValue(string name, IScope scope) {
|
---|
| 51 | if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
|
---|
[11598] | 52 | var value = scope.Variables[name].Value;
|
---|
[11619] | 53 | if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
|
---|
[11598] | 54 | return value;
|
---|
[11484] | 55 | }
|
---|
| 56 |
|
---|
[11619] | 57 | protected static void SetScopeValue(string name, IScope scope, IItem value) {
|
---|
[11598] | 58 | if (value == null) throw new ArgumentNullException("value");
|
---|
| 59 | if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
|
---|
| 60 | else scope.Variables[name].Value = value;
|
---|
[11484] | 61 | }
|
---|
[11598] | 62 | }
|
---|
[11484] | 63 |
|
---|
[11598] | 64 | #region extension methods
|
---|
| 65 | public static class IndividualExtensionMethods {
|
---|
[11813] | 66 | public static BinaryVector BinaryVector(this Individual individual) {
|
---|
[11885] | 67 | var encoding = individual.GetEncoding<BinaryVectorEncoding>();
|
---|
[11813] | 68 | return individual.BinaryVector(encoding.Name);
|
---|
| 69 | }
|
---|
[11598] | 70 | public static BinaryVector BinaryVector(this Individual individual, string name) {
|
---|
| 71 | return (BinaryVector)individual[name];
|
---|
[11484] | 72 | }
|
---|
[11813] | 73 |
|
---|
| 74 | public static IntegerVector IntegerVector(this Individual individual) {
|
---|
[11885] | 75 | var encoding = individual.GetEncoding<IntegerVectorEncoding>();
|
---|
[11813] | 76 | return individual.IntegerVector(encoding.Name);
|
---|
| 77 | }
|
---|
[11598] | 78 | public static IntegerVector IntegerVector(this Individual individual, string name) {
|
---|
| 79 | return (IntegerVector)individual[name];
|
---|
[11484] | 80 | }
|
---|
[11813] | 81 |
|
---|
| 82 | public static Permutation Permutation(this Individual individual) {
|
---|
| 83 | var encoding = individual.GetEncoding<PermutationEncoding>();
|
---|
| 84 | return individual.Permutation(encoding.Name);
|
---|
| 85 | }
|
---|
[11598] | 86 | public static Permutation Permutation(this Individual individual, string name) {
|
---|
| 87 | return (Permutation)individual[name];
|
---|
[11484] | 88 | }
|
---|
[11813] | 89 |
|
---|
| 90 | public static RealVector RealVector(this Individual individual) {
|
---|
[11885] | 91 | var encoding = individual.GetEncoding<RealVectorEncoding>();
|
---|
[11813] | 92 | return individual.RealVector(encoding.Name);
|
---|
| 93 | }
|
---|
[11598] | 94 | public static RealVector RealVector(this Individual individual, string name) {
|
---|
| 95 | return (RealVector)individual[name];
|
---|
[11484] | 96 | }
|
---|
| 97 | }
|
---|
[11598] | 98 | #endregion
|
---|
[11484] | 99 | }
|
---|