Changeset 11619 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs
- Timestamp:
- 12/02/14 11:44:18 (10 years ago)
- 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 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 using HeuristicLab.Common;26 23 using HeuristicLab.Core; 27 24 using HeuristicLab.Encodings.BinaryVectorEncoding; … … 29 26 using HeuristicLab.Encodings.PermutationEncoding; 30 27 using HeuristicLab.Encodings.RealVectorEncoding; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;32 28 33 29 namespace HeuristicLab.Problems.Programmable { 34 [StorableClass] 35 public class Individual : Item { 36 [Storable] 30 public abstract class Individual { 37 31 public IEncoding Encoding { get; private set; } 38 [Storable]39 32 protected IScope Scope { get; private set; } 40 33 41 public Individual(IEncoding encoding, IScope scope) { 34 public string Name { get { return Encoding.Name; } } 35 36 protected Individual(IEncoding encoding, IScope scope) { 42 37 Encoding = encoding; 43 38 Scope = scope; 44 39 } 45 40 46 [StorableConstructor] 47 protected Individual(bool deserializing) : base(deserializing) { } 41 public abstract IItem this[string name] { get; set; } 48 42 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()); 54 45 } 46 internal abstract Individual Copy(IScope scope); 55 47 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)); 70 50 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)); 72 52 return value; 73 53 } 74 54 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) { 77 56 if (value == null) throw new ArgumentNullException("value"); 78 79 57 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value)); 80 58 else scope.Variables[name].Value = value; 81 var variable = scope.Variables[name];82 59 } 83 60 }
Note: See TracChangeset
for help on using the changeset viewer.