Changeset 15256 for branches/PerformanceComparison/HeuristicLab.Optimization/3.3/BasicProblems/Individuals
- Timestamp:
- 07/15/17 14:25:44 (7 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.Optimization
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Optimization
- Property svn:mergeinfo changed
-
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs
r14600 r15256 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Core; 24 26 … … 27 29 protected IEncoding Encoding { get; private set; } 28 30 protected IScope Scope { get; private set; } 29 30 31 public string Name { get { return Encoding.Name; } } 31 32 … … 35 36 } 36 37 37 public abstract IItem this[string name] { get; set; } 38 public IItem this[string name] { 39 get { return ExtractScopeValue(name, Scope); } 40 set { SetScopeValue(name, Scope, value); } 41 } 42 43 public IEnumerable<KeyValuePair<string, IItem>> Values { 44 get { return Scope.Variables.Select(v => new KeyValuePair<string, IItem>(v.Name, v.Value)); } 45 } 38 46 public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding; 39 47 40 public Individual Copy() { 41 return CopyToScope(new Scope()); 48 public abstract Individual Copy(); 49 internal void CopyToScope(IScope scope) { 50 foreach (var val in Values) 51 SetScopeValue(val.Key, scope, val.Value); 42 52 } 43 53 44 public abstract Individual CopyToScope(IScope scope); 45 46 protected static IItem ExtractScopeValue(string name, IScope scope) { 54 private static IItem ExtractScopeValue(string name, IScope scope) { 55 if (scope == null) throw new ArgumentNullException("scope"); 47 56 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 48 57 var value = scope.Variables[name].Value; … … 51 60 } 52 61 53 protected static void SetScopeValue(string name, IScope scope, IItem value) { 62 private static void SetScopeValue(string name, IScope scope, IItem value) { 63 if (scope == null) throw new ArgumentNullException("scope"); 54 64 if (value == null) throw new ArgumentNullException("value"); 65 55 66 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value)); 56 67 else scope.Variables[name].Value = value; -
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiEncodingIndividual.cs
r14600 r15256 32 32 } 33 33 34 private readonly IEnumerable<Individual> individuals; 34 public MultiEncodingIndividual(MultiEncoding encoding, IScope scope) 35 : base(encoding, scope) { } 35 36 36 public MultiEncodingIndividual(MultiEncoding encoding, IScope scope) 37 : base(encoding, scope) { 38 individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray(); 37 private MultiEncodingIndividual(MultiEncodingIndividual copy) : base(copy.Encoding, new Scope()) { 38 copy.CopyToScope(Scope); 39 39 } 40 41 private MultiEncodingIndividual(MultiEncoding encoding, IScope scope, IEnumerable<Individual> individuals) 42 : base(encoding, scope) { 43 this.individuals = individuals; 44 } 45 46 47 public override IItem this[string name] { 48 get { 49 var individual = individuals.SingleOrDefault(i => i.Name == name); 50 if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name)); 51 return individual[name]; 52 } 53 set { 54 var individual = individuals.SingleOrDefault(i => i.Name == name); 55 if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name)); 56 individual[name] = value; 57 } 40 public override Individual Copy() { 41 return new MultiEncodingIndividual(this); 58 42 } 59 43 … … 62 46 try { 63 47 encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding); 64 } catch (InvalidOperationException) { 48 } 49 catch (InvalidOperationException) { 65 50 throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName())); 66 51 } … … 68 53 return encoding; 69 54 } 70 71 public override Individual CopyToScope(IScope scope) {72 var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray();73 return new MultiEncodingIndividual(Encoding, scope, copies);74 }75 55 } 76 56 } -
branches/PerformanceComparison/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/SingleEncodingIndividual.cs
r14600 r15256 32 32 } 33 33 34 public override IItem this[string name] { 35 get { 36 if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name)); 37 return ExtractScopeValue(Name, Scope); 38 } 39 set { 40 if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name)); 41 SetScopeValue(Name, Scope, value); 42 } 34 private SingleEncodingIndividual(SingleEncodingIndividual copy) : base(copy.Encoding, new Scope()) { 35 copy.CopyToScope(Scope); 36 } 37 public override Individual Copy() { 38 return new SingleEncodingIndividual(this); 43 39 } 44 40 … … 48 44 return encoding; 49 45 } 50 51 public override Individual CopyToScope(IScope scope) {52 SetScopeValue(Name, scope, (IItem)this[Name].Clone());53 return new SingleEncodingIndividual(Encoding, scope);54 }55 46 } 56 47
Note: See TracChangeset
for help on using the changeset viewer.