Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs @ 11813

Last change on this file since 11813 was 11813, checked in by mkommend, 9 years ago

#2174: Updated views and renamed programmable problem to basic problem and added individual extension methods.

File size: 3.8 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.BinaryVectorEncoding;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Encodings.RealVectorEncoding;
28
29namespace HeuristicLab.Problems.Programmable {
30  public abstract class Individual {
31    protected 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    public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
43
44    public Individual Copy() {
45      return Copy(new Scope());
46    }
47    internal abstract Individual Copy(IScope scope);
48
49    protected static IItem ExtractScopeValue(string name, IScope scope) {
50      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
51      var value = scope.Variables[name].Value;
52      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
53      return value;
54    }
55
56    protected static void SetScopeValue(string name, IScope scope, IItem value) {
57      if (value == null) throw new ArgumentNullException("value");
58      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
59      else scope.Variables[name].Value = value;
60    }
61  }
62
63  #region extension methods
64  public static class IndividualExtensionMethods {
65    public static BinaryVector BinaryVector(this Individual individual) {
66      var encoding = individual.GetEncoding<BinaryEncoding>();
67      return individual.BinaryVector(encoding.Name);
68    }
69    public static BinaryVector BinaryVector(this Individual individual, string name) {
70      return (BinaryVector)individual[name];
71    }
72
73    public static IntegerVector IntegerVector(this Individual individual) {
74      var encoding = individual.GetEncoding<IntegerEncoding>();
75      return individual.IntegerVector(encoding.Name);
76    }
77    public static IntegerVector IntegerVector(this Individual individual, string name) {
78      return (IntegerVector)individual[name];
79    }
80
81    public static Permutation Permutation(this Individual individual) {
82      var encoding = individual.GetEncoding<PermutationEncoding>();
83      return individual.Permutation(encoding.Name);
84    }
85    public static Permutation Permutation(this Individual individual, string name) {
86      return (Permutation)individual[name];
87    }
88
89    public static RealVector RealVector(this Individual individual) {
90      var encoding = individual.GetEncoding<RealEncoding>();
91      return individual.RealVector(encoding.Name);
92    }
93    public static RealVector RealVector(this Individual individual, string name) {
94      return (RealVector)individual[name];
95    }
96  }
97  #endregion
98}
Note: See TracBrowser for help on using the repository browser.