Changeset 13359 for branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
- Timestamp:
- 11/24/15 15:24:12 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r13351 r13359 23 23 24 24 using System; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 … … 30 31 public static TSolution CopySolutionToScope<TSolution>(IScope scope, IEncoding<TSolution> encoding, TSolution solution) 31 32 where TSolution : class,ISolution { 32 var name = encoding.Name; 33 return CopySolutionToScope(scope, encoding.Name, solution); 34 } 35 36 public static TSolution CopySolutionToScope<TSolution>(IScope scope, string name, TSolution solution) 37 where TSolution : class,ISolution { 33 38 var copy = (TSolution)solution.Clone(); 34 39 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, copy)); … … 42 47 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 43 48 var value = scope.Variables[name].Value as TSolution; 44 if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null .", name));49 if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(TSolution).GetPrettyName())); 45 50 return value; 46 51 } 47 52 48 53 public static ISolution GetSolution(IScope scope, IEncoding encoding) { 49 var name = encoding.Name; 50 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 51 var value = scope.Variables[name].Value as ISolution; 52 if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name)); 53 return value; 54 return GetSolution(scope, encoding.Name); 55 } 56 57 public static ISolution GetSolution(IScope scope, string name) { 58 IVariable variable; 59 if (!scope.Variables.TryGetValue(name, out variable)) throw new ArgumentException(string.Format("{0} cannot be found in the provided scope.", name)); 60 var solution = variable.Value as ISolution; 61 if (solution == null) throw new InvalidOperationException(string.Format("{0} is null or not of type ISolution.", name)); 62 return solution; 54 63 } 55 64
Note: See TracChangeset
for help on using the changeset viewer.