- Timestamp:
- 05/29/20 15:18:44 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3
- Files:
-
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISolutionContext.cs
r17459 r17577 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; … … 29 30 30 31 bool IsEvaluated { get; } 32 IEnumerable<KeyValuePair<string, object>> AdditionalData { get; } 31 33 IEncodedSolution EncodedSolution { get; } 32 34 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r16751 r17577 28 28 namespace HeuristicLab.Optimization { 29 29 public static class ScopeUtil { 30 private const string EvaluationResultName = "EvaluationResult"; 30 31 32 // TODO: Create SolutionContexts that derive from IScope to have a unified datastructure (e.g. #1614) 31 33 public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding, TEncodedSolution solution) 32 34 where TEncodedSolution : class, IEncodedSolution { … … 63 65 } 64 66 67 public static ISingleObjectiveSolutionContext<TEncodedSolution> CreateSolutionContext<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding) 68 where TEncodedSolution : class, IEncodedSolution { 69 var solution = GetEncodedSolution(scope, encoding); 70 var context = new SingleObjectiveSolutionContext<TEncodedSolution>(solution); 71 foreach (var variable in scope.Variables) { 72 if (variable.Name != encoding.Name) 73 context.SetAdditionalData(variable.Name, variable.Value); 74 } 75 if (scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) { 76 context.EvaluationResult = (ISingleObjectiveEvaluationResult)variable2.Value; 77 } 78 return context; 79 } 80 81 public static void CopyToScope<TEncodedSolution>(IScope scope, ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext) 82 where TEncodedSolution : class, IEncodedSolution { 83 foreach (var item in solutionContext.AdditionalData) { 84 if (item.Value is IItem i) { 85 if (!scope.Variables.TryGetValue(item.Key, out var variable)) 86 scope.Variables.Add(new Variable(item.Key, i)); 87 else variable.Value = i; 88 } 89 } 90 if (!scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) { 91 scope.Variables.Add(new Variable(EvaluationResultName, solutionContext.EvaluationResult)); 92 } else variable2.Value = solutionContext.EvaluationResult; 93 } 65 94 } 66 95 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs
r17381 r17577 79 79 80 80 var solutionContexts = scopes.Select(scope => { 81 var solution = ScopeUtil.GetEncodedSolution(scope, encoding); 82 var quality = ((DoubleValue)scope.Variables[QualityParameter.ActualName].Value).Value; 83 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(scope, solution); 84 return solutionContext; 81 return ScopeUtil.CreateSolutionContext(scope, encoding); 85 82 }).ToArray(); 86 83 87 84 Analyze(solutionContexts, results, random); 85 foreach (var s in solutionContexts.Zip(scopes, Tuple.Create)) { 86 ScopeUtil.CopyToScope(s.Item2, s.Item1); 87 } 88 88 return base.Apply(); 89 89 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs
r17366 r17577 63 63 var random = RandomParameter.ActualValue; 64 64 var encoding = EncodingParameter.ActualValue; 65 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 66 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 65 var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding); 67 66 68 67 Evaluate(solutionContext, random, CancellationToken.None); 69 var qualityValue = solutionContext.EvaluationResult.Quality; 68 69 QualityParameter.ActualValue = new DoubleValue(solutionContext.EvaluationResult.Quality); 70 ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext); 70 71 71 QualityParameter.ActualValue = new DoubleValue(qualityValue);72 72 return base.InstrumentedApply(); 73 73 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs
r17366 r17577 90 90 var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; 91 91 var sampleSize = SampleSizeParameter.ActualValue.Value; 92 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 93 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 92 var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding); 94 93 95 94 double quality; … … 102 101 var count = 0; 103 102 for (var i = 0; i < maxAttempts; i++) { 104 TEncodedSolution best = default(TEncodedSolution);103 ISingleObjectiveSolutionContext<TEncodedSolution> best = default; 105 104 var bestQuality = quality; 106 105 foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) { 107 Evaluate(neighbor, random, CancellationToken); 106 if (!neighbor.IsEvaluated) 107 Evaluate(neighbor, random, CancellationToken); 108 108 var q = neighbor.EvaluationResult.Quality; 109 109 count++; 110 110 if (maximize && bestQuality > q || !maximize && bestQuality < q) continue; 111 best = neighbor .EncodedSolution;111 best = neighbor; 112 112 bestQuality = q; 113 113 } 114 114 if (best == null) break; 115 solution = best;115 solutionContext = best; 116 116 quality = bestQuality; 117 117 } … … 119 119 LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count); 120 120 QualityParameter.ActualValue = new DoubleValue(quality); 121 122 ScopeUtil.Copy EncodedSolutionToScope(ExecutionContext.Scope, encoding, solution);121 ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solutionContext.EncodedSolution); 122 ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext); 123 123 return base.Apply(); 124 124 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs
r17366 r17577 70 70 var random = RandomParameter.ActualValue; 71 71 var encoding = EncodingParameter.ActualValue; 72 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 73 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 72 var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding); 74 73 75 Evaluate(solutionContext, random, CancellationToken.None); 74 if (!solutionContext.IsEvaluated) 75 Evaluate(solutionContext, random, CancellationToken.None); 76 76 var qualityValue = solutionContext.EvaluationResult.Quality; 77 77 78 78 MoveQualityParameter.ActualValue = new DoubleValue(qualityValue); 79 ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext); 79 80 return base.Apply(); 80 81 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs
r17363 r17577 68 68 var sampleSize = SampleSizeParameter.ActualValue.Value; 69 69 var encoding = EncodingParameter.ActualValue; 70 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 71 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 70 var solutionContext = ScopeUtil.CreateSolutionContext(ExecutionContext.Scope, encoding); 72 71 73 72 var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList(); … … 76 75 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 77 76 ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i].EncodedSolution); 77 ScopeUtil.CopyToScope(moveScopes[i], nbhood[i]); 78 78 } 79 79 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs
r17381 r17577 35 35 [Storable] 36 36 private readonly Dictionary<string, object> data = new Dictionary<string, object>(); 37 public IEnumerable<KeyValuePair<string, object>> AdditionalData { get { return data; } } 37 38 38 39 IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } } … … 77 78 public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution> 78 79 where TEncodedSolution : class, IEncodedSolution { 79 public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; } 80 public new ISingleObjectiveEvaluationResult EvaluationResult { 81 get { return (ISingleObjectiveEvaluationResult)base.EvaluationResult; } 82 set { base.EvaluationResult = value; } 83 } 80 84 81 85 public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { } … … 98 102 public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution> 99 103 where TEncodedSolution : class, IEncodedSolution { 100 public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; } 104 public new IMultiObjectiveEvaluationResult EvaluationResult { 105 get { return (IMultiObjectiveEvaluationResult)base.EvaluationResult; } 106 set { base.EvaluationResult = value; } 107 } 101 108 102 109 public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r17513 r17577 134 134 <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" /> 135 135 <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" /> 136 <Compile Include="BasicProblems\Operators\SolutionContextScope.cs" />137 136 <Compile Include="BasicProblems\Problem.cs" /> 138 137 <Compile Include="BasicProblems\Encoding.cs" />
Note: See TracChangeset
for help on using the changeset viewer.