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 HeuristicLab.Core;
|
---|
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 {
|
---|
30 | public abstract class Individual {
|
---|
31 | public IEncoding Encoding { get; private set; }
|
---|
32 | protected IScope Scope { get; private set; }
|
---|
33 |
|
---|
34 | public string Name { get { return Encoding.Name; } }
|
---|
35 |
|
---|
36 | protected Individual(IEncoding encoding, IScope scope) {
|
---|
37 | Encoding = encoding;
|
---|
38 | Scope = scope;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public abstract IItem this[string name] { get; set; }
|
---|
42 |
|
---|
43 | public Individual Copy() {
|
---|
44 | return Copy(new Scope());
|
---|
45 | }
|
---|
46 | internal abstract Individual Copy(IScope scope);
|
---|
47 |
|
---|
48 | protected static IItem ExtractScopeValue(string name, IScope scope) {
|
---|
49 | if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
|
---|
50 | var value = scope.Variables[name].Value;
|
---|
51 | if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
|
---|
52 | return value;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected static void SetScopeValue(string name, IScope scope, IItem value) {
|
---|
56 | if (value == null) throw new ArgumentNullException("value");
|
---|
57 | if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
|
---|
58 | else scope.Variables[name].Value = value;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region extension methods
|
---|
63 | public static class IndividualExtensionMethods {
|
---|
64 | public static BinaryVector BinaryVector(this Individual individual, string name) {
|
---|
65 | return (BinaryVector)individual[name];
|
---|
66 | }
|
---|
67 | public static IntegerVector IntegerVector(this Individual individual, string name) {
|
---|
68 | return (IntegerVector)individual[name];
|
---|
69 | }
|
---|
70 | public static Permutation Permutation(this Individual individual, string name) {
|
---|
71 | return (Permutation)individual[name];
|
---|
72 | }
|
---|
73 | public static RealVector RealVector(this Individual individual, string name) {
|
---|
74 | return (RealVector)individual[name];
|
---|
75 | }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 | }
|
---|