Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/19 15:54:31 (5 years ago)
Author:
mkommend
Message:

#2521: Fixed bugs in solution context and adapted programmable problem template to include a cancellation token in the evaluate function.

Location:
branches/2521_ProblemRefactoring
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs

    r17363 r17366  
    2121
    2222using System;
     23using System.Threading;
    2324using HEAL.Attic;
    2425using HeuristicLab.Core;
     
    2829  internal interface ISingleObjectiveEvaluationOperator<TEncodedSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TEncodedSolution>
    2930  where TEncodedSolution : class, IEncodedSolution {
    30     Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
     31    Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
    3132  }
    3233}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs

    r17363 r17366  
    2121
    2222using System;
     23using System.Threading;
    2324using HEAL.Attic;
    2425using HeuristicLab.Common;
     
    4647    }
    4748
    48     public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
     49    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
    4950
    5051    [StorableConstructor]
     
    6566      var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution);
    6667
    67       Evaluate(solutionContext, random);
     68      Evaluate(solutionContext, random, CancellationToken.None);
    6869      var qualityValue = solutionContext.EvaluationResult.Quality;
    6970
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs

    r17363 r17366  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using HEAL.Attic;
    2627using HeuristicLab.Common;
     
    6364    }
    6465
    65     public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
     66    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
    6667    public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
    6768
     
    9495      double quality;
    9596      if (QualityParameter.ActualValue == null) {
    96         if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random);
     97        if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random, CancellationToken.None);
    9798
    9899        quality = solutionContext.EvaluationResult.Quality;
     
    104105        var bestQuality = quality;
    105106        foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) {
    106           Evaluate(neighbor, random);
     107          Evaluate(neighbor, random, CancellationToken);
    107108          var q = neighbor.EvaluationResult.Quality;
    108109          count++;
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs

    r17363 r17366  
    2121
    2222using System;
     23using System.Threading;
    2324using HEAL.Attic;
    2425using HeuristicLab.Common;
     
    5051    }
    5152
    52     public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
     53    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, CancellationToken> Evaluate { get; set; }
    5354
    5455    [StorableConstructor]
     
    7273      var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution);
    7374
    74       Evaluate(solutionContext, random);
     75      Evaluate(solutionContext, random, CancellationToken.None);
    7576      var qualityValue = solutionContext.EvaluationResult.Quality;
    7677
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r17363 r17366  
    113113    }
    114114
    115     public virtual double Evaluate(TEncodedSolution solution, IRandom random) {
     115    public double Evaluate(TEncodedSolution solution, IRandom random) {
    116116      return Evaluate(solution, random, CancellationToken.None);
    117117    }
    118118    public abstract double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken);
    119119
    120     public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
     120    public void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) {
    121121      Evaluate(solutionContext, random, CancellationToken.None);
    122122    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs

    r17363 r17366  
    6363    }
    6464
    65     public void SetAdditionalData(string identifier, object o) {
     65    public virtual void SetAdditionalData(string identifier, object o) {
    6666      data[identifier] = o;
    6767    }
    68     public object GetAdditionalData(string identifier) {
     68    public virtual object GetAdditionalData(string identifier) {
    6969      return data[identifier];
    7070    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemDefinitionScript.cs

    r17363 r17366  
    6363    }
    6464    void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
    65       double quality = CompiledProblemDefinition.Evaluate(solutionContext.EncodedSolution, random, cancellationToken);
    66       solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality);
     65      CompiledProblemDefinition.Evaluate(solutionContext, random, cancellationToken);
    6766    }
    6867
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveCombinedEncodingProblem_Template.cs

    r16801 r17366  
    22using System.Linq;
    33using System.Collections.Generic;
     4using System.Threading;
    45using HeuristicLab.Common;
    56using HeuristicLab.Core;
     
    2829    }
    2930
    30     public override double Evaluate(CombinedSolution solution, IRandom random) {
     31    public override double Evaluate(CombinedSolution solution, IRandom random, CancellationToken cancellationToken) {
    3132      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    3233      var quality = 0.0;
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveProblem_Template.cs

    r13373 r17366  
    22using System.Linq;
    33using System.Collections.Generic;
     4using System.Threading;
    45using HeuristicLab.Common;
    56using HeuristicLab.Core;
     
    2021    }
    2122
    22     public override double Evaluate(SOLUTION_CLASS solution, IRandom random) {
     23    public override double Evaluate(SOLUTION_CLASS solution, IRandom random, CancellationToken cancellationToken) {
    2324      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    2425      var quality = 0.0;
Note: See TracChangeset for help on using the changeset viewer.