Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11885 was 11885, checked in by abeham, 9 years ago

#2174:

  • Some refactorings and bug fixes
  • Renamed (Binary|Integer|Real)Encoding to (Binary|Integer|Real)VectorEncoding
  • Improved error messages when compiling programmable problems
File size: 3.9 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 CopyToScope(new Scope());
46    }
47
48    public abstract Individual CopyToScope(IScope scope);
49
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));
52      var value = scope.Variables[name].Value;
53      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
54      return value;
55    }
56
57    protected static void SetScopeValue(string name, IScope scope, IItem value) {
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;
61    }
62  }
63
64  #region extension methods
65  public static class IndividualExtensionMethods {
66    public static BinaryVector BinaryVector(this Individual individual) {
67      var encoding = individual.GetEncoding<BinaryVectorEncoding>();
68      return individual.BinaryVector(encoding.Name);
69    }
70    public static BinaryVector BinaryVector(this Individual individual, string name) {
71      return (BinaryVector)individual[name];
72    }
73
74    public static IntegerVector IntegerVector(this Individual individual) {
75      var encoding = individual.GetEncoding<IntegerVectorEncoding>();
76      return individual.IntegerVector(encoding.Name);
77    }
78    public static IntegerVector IntegerVector(this Individual individual, string name) {
79      return (IntegerVector)individual[name];
80    }
81
82    public static Permutation Permutation(this Individual individual) {
83      var encoding = individual.GetEncoding<PermutationEncoding>();
84      return individual.Permutation(encoding.Name);
85    }
86    public static Permutation Permutation(this Individual individual, string name) {
87      return (Permutation)individual[name];
88    }
89
90    public static RealVector RealVector(this Individual individual) {
91      var encoding = individual.GetEncoding<RealVectorEncoding>();
92      return individual.RealVector(encoding.Name);
93    }
94    public static RealVector RealVector(this Individual individual, string name) {
95      return (RealVector)individual[name];
96    }
97  }
98  #endregion
99}
Note: See TracBrowser for help on using the repository browser.