Index: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs
===================================================================
--- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs	(revision 11484)
+++ branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs	(revision 11598)
@@ -20,104 +20,82 @@
 #endregion
 
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using HeuristicLab.Common;
+using HeuristicLab.Core;
 using HeuristicLab.Encodings.BinaryVectorEncoding;
 using HeuristicLab.Encodings.IntegerVectorEncoding;
 using HeuristicLab.Encodings.PermutationEncoding;
 using HeuristicLab.Encodings.RealVectorEncoding;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
 
 namespace HeuristicLab.Problems.Programmable {
-  public sealed class Individual : IDeepCloneable {
-    private readonly Dictionary<string, BinaryVector> binaryVectors;
-    private readonly Dictionary<string, IntegerVector> integerVectors;
-    private readonly Dictionary<string, RealVector> realVectors;
-    private readonly Dictionary<string, Permutation> permutationVectors;
+  [StorableClass]
+  public class Individual : Item {
+    [Storable]
+    public IEncoding Encoding { get; private set; }
+    [Storable]
+    protected IScope Scope { get; private set; }
 
-    public Individual(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null,
-      IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null,
-      IEnumerable<KeyValuePair<string, RealVector>> realVectors = null,
-      IEnumerable<KeyValuePair<string, Permutation>> permutations = null) {
-      // prevent unnecessary allocation of dictionaries for unused vectors
-      if (binaryVectors != null) this.binaryVectors = binaryVectors.ToDictionary(x => x.Key, x => x.Value);
-      if (integerVectors != null) this.integerVectors = integerVectors.ToDictionary(x => x.Key, x => x.Value);
-      if (realVectors != null) this.realVectors = realVectors.ToDictionary(x => x.Key, x => x.Value);
-      if (permutations != null) this.permutationVectors = permutations.ToDictionary(x => x.Key, x => x.Value);
-    }
-    private Individual(Individual original, Cloner cloner) {
-      cloner.RegisterClonedObject(original, this);
-      if (original.binaryVectors != null) {
-        binaryVectors = new Dictionary<string, BinaryVector>(original.binaryVectors.Comparer);
-        foreach (var param in original.binaryVectors)
-          binaryVectors[param.Key] = cloner.Clone(param.Value);
-      }
-      if (original.integerVectors != null) {
-        integerVectors = new Dictionary<string, IntegerVector>(original.integerVectors.Comparer);
-        foreach (var param in original.integerVectors)
-          integerVectors[param.Key] = cloner.Clone(param.Value);
-      }
-      if (original.realVectors != null) {
-        realVectors = new Dictionary<string, RealVector>(original.realVectors.Comparer);
-        foreach (var param in original.realVectors)
-          realVectors[param.Key] = cloner.Clone(param.Value);
-      }
-      if (original.permutationVectors != null) {
-        permutationVectors = new Dictionary<string, Permutation>(original.permutationVectors.Comparer);
-        foreach (var param in original.permutationVectors)
-          permutationVectors[param.Key] = cloner.Clone(param.Value);
-      }
+    public Individual(IEncoding encoding, IScope scope) {
+      Encoding = encoding;
+      Scope = scope;
     }
 
-    public object Clone() {
-      return Clone(new Cloner());
+    [StorableConstructor]
+    protected Individual(bool deserializing) : base(deserializing) { }
+
+    public override IDeepCloneable Clone(Cloner cloner) { return new Individual(this, cloner); }
+    protected Individual(Individual original, Cloner cloner)
+      : base(original, cloner) {
+      Encoding = cloner.Clone(original.Encoding);
+      Scope = cloner.Clone(original.Scope);
     }
 
-    public IDeepCloneable Clone(Cloner cloner) {
-      return new Individual(this, cloner);
+    public virtual IItem this[string name] {
+      get { return ExtractScopeValue(name, Encoding.Name, Scope); }
+      set { SetScopeValue(name, Encoding.Name, Scope, value); }
     }
 
-    public BinaryVector BinaryVector(string name) {
-      return binaryVectors[name];
+    internal virtual Individual Copy(IScope scope) {
+      var individual = Encoding.CreateIndividual(scope);
+      individual[Encoding.Name] = this[Encoding.Name];
+      return individual;
     }
 
-    public bool BinaryValue(string name, int index) {
-      return binaryVectors[name][index];
+    protected static IItem ExtractScopeValue(string name, string encodingName, IScope scope) {
+      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
+      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format("Encoding part {0} cannot be found in the provided scope.", name));
+      var value = scope.Variables[name].Value;
+      if (value == null) throw new ArgumentException(string.Format("Encoding part {0} is null.", name));
+      return value;
     }
 
-    public IEnumerable<string> BinaryNames {
-      get { return binaryVectors != null ? binaryVectors.Keys : Enumerable.Empty<string>(); }
-    }
+    protected static void SetScopeValue(string name, string encodingName, IScope scope, IItem value) {
+      if (name != encodingName) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
+      if (value == null) throw new ArgumentNullException("value");
 
-    public IntegerVector IntegerVector(string name) {
-      return integerVectors[name];
-    }
-
-    public int IntegerValue(string name, int index) {
-      return integerVectors[name][index];
-    }
-
-    public IEnumerable<string> IntegerNames {
-      get { return integerVectors != null ? integerVectors.Keys : Enumerable.Empty<string>(); }
-    }
-
-    public RealVector RealVector(string name) {
-      return realVectors[name];
-    }
-
-    public double RealValue(string name, int index) {
-      return realVectors[name][index];
-    }
-
-    public IEnumerable<string> RealNames {
-      get { return realVectors != null ? realVectors.Keys : Enumerable.Empty<string>(); }
-    }
-
-    public Permutation Permutation(string name) {
-      return permutationVectors[name];
-    }
-
-    public IEnumerable<string> PermutationNames {
-      get { return permutationVectors != null ? permutationVectors.Keys : Enumerable.Empty<string>(); }
+      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
+      else scope.Variables[name].Value = value;
+      var variable = scope.Variables[name];
     }
   }
+
+  #region extension methods
+  public static class IndividualExtensionMethods {
+    public static BinaryVector BinaryVector(this Individual individual, string name) {
+      return (BinaryVector)individual[name];
+    }
+    public static IntegerVector IntegerVector(this Individual individual, string name) {
+      return (IntegerVector)individual[name];
+    }
+    public static Permutation Permutation(this Individual individual, string name) {
+      return (Permutation)individual[name];
+    }
+    public static RealVector RealVector(this Individual individual, string name) {
+      return (RealVector)individual[name];
+    }
+  }
+  #endregion
 }
