Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17577


Ignore:
Timestamp:
05/29/20 15:18:44 (4 years ago)
Author:
abeham
Message:

#2521: remove scope-based contexts, add helper methods to ScopeUtil, adapt operators

Location:
branches/2521_ProblemRefactoring
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs

    r17570 r17577  
    7171
    7272    public override void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) {
     73      base.Analyze(solutionContexts, results, random);
    7374      var best = GetBest(solutionContexts);
    7475      var currentBest = BestResultParameter.ActualValue;
    7576      if (currentBest == null || IsBetter(best.EvaluationResult.Quality, currentBest.EvaluationResult.Quality))
    76         BestResultParameter.ActualValue = (ISingleObjectiveSolutionContext<BinaryVector>)best.Clone();
     77        BestResultParameter.ActualValue = new SingleObjectiveSolutionContext<BinaryVector>(
     78          (BinaryVector)best.EncodedSolution.Clone(), (ISingleObjectiveEvaluationResult)best.EvaluationResult.Clone());
    7779    }
    7880
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISolutionContext.cs

    r17459 r17577  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HEAL.Attic;
    2324using HeuristicLab.Core;
     
    2930
    3031    bool IsEvaluated { get; }
     32    IEnumerable<KeyValuePair<string, object>> AdditionalData { get; }
    3133    IEncodedSolution EncodedSolution { get; }
    3234
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs

    r16751 r17577  
    2828namespace HeuristicLab.Optimization {
    2929  public static class ScopeUtil {
     30    private const string EvaluationResultName = "EvaluationResult";
    3031
     32    // TODO: Create SolutionContexts that derive from IScope to have a unified datastructure (e.g. #1614)
    3133    public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding, TEncodedSolution solution)
    3234      where TEncodedSolution : class, IEncodedSolution {
     
    6365    }
    6466
     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    }
    6594  }
    6695}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs

    r17381 r17577  
    7979
    8080      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);
    8582      }).ToArray();
    8683
    8784      Analyze(solutionContexts, results, random);
     85      foreach (var s in solutionContexts.Zip(scopes, Tuple.Create)) {
     86        ScopeUtil.CopyToScope(s.Item2, s.Item1);
     87      }
    8888      return base.Apply();
    8989    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs

    r17366 r17577  
    6363      var random = RandomParameter.ActualValue;
    6464      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);
    6766
    6867      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);
    7071
    71       QualityParameter.ActualValue = new DoubleValue(qualityValue);
    7272      return base.InstrumentedApply();
    7373    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs

    r17366 r17577  
    9090      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
    9191      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);
    9493
    9594      double quality;
     
    102101      var count = 0;
    103102      for (var i = 0; i < maxAttempts; i++) {
    104         TEncodedSolution best = default(TEncodedSolution);
     103        ISingleObjectiveSolutionContext<TEncodedSolution> best = default;
    105104        var bestQuality = quality;
    106105        foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) {
    107           Evaluate(neighbor, random, CancellationToken);
     106          if (!neighbor.IsEvaluated)
     107            Evaluate(neighbor, random, CancellationToken);
    108108          var q = neighbor.EvaluationResult.Quality;
    109109          count++;
    110110          if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
    111           best = neighbor.EncodedSolution;
     111          best = neighbor;
    112112          bestQuality = q;
    113113        }
    114114        if (best == null) break;
    115         solution = best;
     115        solutionContext = best;
    116116        quality = bestQuality;
    117117      }
     
    119119      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
    120120      QualityParameter.ActualValue = new DoubleValue(quality);
    121 
    122       ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solution);
     121      ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solutionContext.EncodedSolution);
     122      ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext);
    123123      return base.Apply();
    124124    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs

    r17366 r17577  
    7070      var random = RandomParameter.ActualValue;
    7171      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);
    7473
    75       Evaluate(solutionContext, random, CancellationToken.None);
     74      if (!solutionContext.IsEvaluated)
     75        Evaluate(solutionContext, random, CancellationToken.None);
    7676      var qualityValue = solutionContext.EvaluationResult.Quality;
    7777
    7878      MoveQualityParameter.ActualValue = new DoubleValue(qualityValue);
     79      ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext);
    7980      return base.Apply();
    8081    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs

    r17363 r17577  
    6868      var sampleSize = SampleSizeParameter.ActualValue.Value;
    6969      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);
    7271
    7372      var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList();
     
    7675        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
    7776        ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i].EncodedSolution);
     77        ScopeUtil.CopyToScope(moveScopes[i], nbhood[i]);
    7878      }
    7979      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs

    r17381 r17577  
    3535    [Storable]
    3636    private readonly Dictionary<string, object> data = new Dictionary<string, object>();
     37    public IEnumerable<KeyValuePair<string, object>> AdditionalData { get { return data; } }
    3738
    3839    IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } }
     
    7778  public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution>
    7879   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    }
    8084
    8185    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
     
    98102  public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution>
    99103   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    }
    101108
    102109    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r17513 r17577  
    134134    <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" />
    135135    <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" />
    136     <Compile Include="BasicProblems\Operators\SolutionContextScope.cs" />
    137136    <Compile Include="BasicProblems\Problem.cs" />
    138137    <Compile Include="BasicProblems\Encoding.cs" />
Note: See TracChangeset for help on using the changeset viewer.