Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs @ 11598

Last change on this file since 11598 was 11598, checked in by mkommend, 8 years ago

#2174: Added first version of refactored individuals.

File size: 4.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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Programmable {
34  [StorableClass]
35  public class Individual : Item {
36    [Storable]
37    public IEncoding Encoding { get; private set; }
38    [Storable]
39    protected IScope Scope { get; private set; }
40
41    public Individual(IEncoding encoding, IScope scope) {
42      Encoding = encoding;
43      Scope = scope;
44    }
45
46    [StorableConstructor]
47    protected Individual(bool deserializing) : base(deserializing) { }
48
49    public override IDeepCloneable Clone(Cloner cloner) { return new Individual(this, cloner); }
50    protected Individual(Individual original, Cloner cloner)
51      : base(original, cloner) {
52      Encoding = cloner.Clone(original.Encoding);
53      Scope = cloner.Clone(original.Scope);
54    }
55
56    public virtual IItem this[string name] {
57      get { return ExtractScopeValue(name, Encoding.Name, Scope); }
58      set { SetScopeValue(name, Encoding.Name, Scope, value); }
59    }
60
61    internal virtual Individual Copy(IScope scope) {
62      var individual = Encoding.CreateIndividual(scope);
63      individual[Encoding.Name] = this[Encoding.Name];
64      return individual;
65    }
66
67    protected static IItem ExtractScopeValue(string name, string encodingName, IScope scope) {
68      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
69      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format("Encoding part {0} cannot be found in the provided scope.", name));
70      var value = scope.Variables[name].Value;
71      if (value == null) throw new ArgumentException(string.Format("Encoding part {0} is null.", name));
72      return value;
73    }
74
75    protected static void SetScopeValue(string name, string encodingName, IScope scope, IItem value) {
76      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
77      if (value == null) throw new ArgumentNullException("value");
78
79      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
80      else scope.Variables[name].Value = value;
81      var variable = scope.Variables[name];
82    }
83  }
84
85  #region extension methods
86  public static class IndividualExtensionMethods {
87    public static BinaryVector BinaryVector(this Individual individual, string name) {
88      return (BinaryVector)individual[name];
89    }
90    public static IntegerVector IntegerVector(this Individual individual, string name) {
91      return (IntegerVector)individual[name];
92    }
93    public static Permutation Permutation(this Individual individual, string name) {
94      return (Permutation)individual[name];
95    }
96    public static RealVector RealVector(this Individual individual, string name) {
97      return (RealVector)individual[name];
98    }
99  }
100  #endregion
101}
Note: See TracBrowser for help on using the repository browser.