Changeset 14877 for trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs
- Timestamp:
- 04/19/17 14:42:41 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs
r14185 r14877 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;
Note: See TracChangeset
for help on using the changeset viewer.