Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11619 was 11619, checked in by mkommend, 10 years ago

#2174: Refactored individuals.

File size: 3.0 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    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}
Note: See TracBrowser for help on using the repository browser.