Ignore:
Timestamp:
12/02/14 11:44:18 (7 years ago)
Author:
mkommend
Message:

#2174: Refactored individuals.

Location:
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs

    r11614 r11619  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using HeuristicLab.Common;
    2623using HeuristicLab.Core;
    2724using HeuristicLab.Encodings.BinaryVectorEncoding;
     
    2926using HeuristicLab.Encodings.PermutationEncoding;
    3027using HeuristicLab.Encodings.RealVectorEncoding;
    31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3228
    3329namespace HeuristicLab.Problems.Programmable {
    34   [StorableClass]
    35   public class Individual : Item {
    36     [Storable]
     30  public abstract class Individual {
    3731    public IEncoding Encoding { get; private set; }
    38     [Storable]
    3932    protected IScope Scope { get; private set; }
    4033
    41     public Individual(IEncoding encoding, IScope scope) {
     34    public string Name { get { return Encoding.Name; } }
     35
     36    protected Individual(IEncoding encoding, IScope scope) {
    4237      Encoding = encoding;
    4338      Scope = scope;
    4439    }
    4540
    46     [StorableConstructor]
    47     protected Individual(bool deserializing) : base(deserializing) { }
     41    public abstract IItem this[string name] { get; set; }
    4842
    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);
     43    public Individual Copy() {
     44      return Copy(new Scope());
    5445    }
     46    internal abstract Individual Copy(IScope scope);
    5547
    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));
     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));
    7050      var value = scope.Variables[name].Value;
    71       if (value == null) throw new ArgumentException(string.Format("Encoding part {0} is null.", name));
     51      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
    7252      return value;
    7353    }
    7454
    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));
     55    protected static void SetScopeValue(string name, IScope scope, IItem value) {
    7756      if (value == null) throw new ArgumentNullException("value");
    78 
    7957      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
    8058      else scope.Variables[name].Value = value;
    81       var variable = scope.Variables[name];
    8259    }
    8360  }
Note: See TracChangeset for help on using the changeset viewer.