Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/04/19 17:31:54 (5 years ago)
Author:
mkommend
Message:

#2521: Added cancellation token to evaluate function of problems.

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/EvaluationCache.cs

    r17226 r17320  
    3131using System.Threading;
    3232using Google.ProtocolBuffers;
     33using HEAL.Attic;
    3334using HeuristicLab.Common;
    3435using HeuristicLab.Common.Resources;
     
    3637using HeuristicLab.Data;
    3738using HeuristicLab.Parameters;
    38 using HEAL.Attic;
    3939
    4040namespace HeuristicLab.Problems.ExternalEvaluation {
     
    5050      private byte[] rawMessage;
    5151
    52       private object lockObject = new object();
     52      private readonly object lockObject = new object();
    5353
    5454      public byte[] RawMessage {
     
    116116    }
    117117
    118     public delegate QualityMessage Evaluator(SolutionMessage message);
     118    public delegate QualityMessage Evaluator(SolutionMessage message, CancellationToken cancellationToken);
    119119    #endregion
    120120
     
    124124
    125125    private HashSet<string> activeEvaluations = new HashSet<string>();
    126     private object cacheLock = new object();
     126    private readonly object cacheLock = new object();
    127127    #endregion
    128128
     
    221221    }
    222222
    223     void CapacityChanged(object sender, EventArgs e) {
     223    private void CapacityChanged(object sender, EventArgs e) {
    224224      if (Capacity < 0)
    225225        throw new ArgumentOutOfRangeException("Cache capacity cannot be less than zero");
     
    240240    }
    241241
    242     public QualityMessage GetValue(SolutionMessage message, Evaluator evaluate, ExtensionRegistry extensions) {
     242    public QualityMessage GetValue(SolutionMessage message, Evaluator evaluate, ExtensionRegistry extensions, CancellationToken cancellationToken) {
    243243      var entry = new CacheEntry(message.ToString());
    244244      bool lockTaken = false;
     
    267267              OnChanged();
    268268              try {
    269                 entry.SetMessage(evaluate(message));
     269                entry.SetMessage(evaluate(message, cancellationToken));
    270270                Monitor.Enter(cacheLock, ref lockTaken);
    271271                index[entry] = list.AddLast(entry);
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Interfaces/IExternalEvaluationProblem.cs

    r17226 r17320  
    2020#endregion
    2121
     22using System.Threading;
    2223using Google.ProtocolBuffers;
     24using HEAL.Attic;
    2325using HeuristicLab.Core;
    2426using HeuristicLab.Optimization;
    25 using HEAL.Attic;
    2627
    2728namespace HeuristicLab.Problems.ExternalEvaluation {
     
    3233    CheckedItemCollection<IEvaluationServiceClient> Clients { get; }
    3334
    34     QualityMessage Evaluate(SolutionMessage solutionMessage);
     35    QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken);
    3536    ExtensionRegistry GetQualityMessageExtensions();
    3637  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs

    r17317 r17320  
    106106    }
    107107
    108     public override double[] Evaluate(TEncodedSolution individual, IRandom random) {
    109       var qualityMessage = Evaluate(BuildSolutionMessage(individual));
     108    public override double[] Evaluate(TEncodedSolution individual, IRandom random, CancellationToken cancellationToken) {
     109      var qualityMessage = Evaluate(BuildSolutionMessage(individual), cancellationToken);
    110110      if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_))
    111111        throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage.");
    112112      return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray();
    113113    }
    114     public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
     114    public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) {
    115115      return Cache == null
    116         ? EvaluateOnNextAvailableClient(solutionMessage)
    117         : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
     116        ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken)
     117        : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken);
    118118    }
    119119
     
    132132    #region Evaluation
    133133    private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
    134     private object clientLock = new object();
     134    private readonly object clientLock = new object();
    135135
    136     private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     136    private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) {
    137137      IEvaluationServiceClient client = null;
    138138      lock (clientLock) {
     
    147147      try {
    148148        return client.Evaluate(message, GetQualityMessageExtensions());
    149       }
    150       finally {
     149      } finally {
    151150        lock (clientLock) {
    152151          activeClients.Remove(client);
     
    165164          try {
    166165            MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
    167           }
    168           catch (ArgumentException ex) {
     166          } catch (ArgumentException ex) {
    169167            throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
    170168          }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/SingleObjectiveExternalEvaluationProblem.cs

    r17279 r17320  
    103103
    104104    #region Single Objective Problem Overrides
    105     public override double Evaluate(TEncodedSolution solution, IRandom random) {
    106       var qualityMessage = Evaluate(BuildSolutionMessage(solution));
     105    public override double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken) {
     106      var qualityMessage = Evaluate(BuildSolutionMessage(solution), cancellationToken);
    107107      if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_))
    108108        throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage.");
    109109      return qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality;
    110110    }
    111     public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
     111    public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) {
    112112      return Cache == null
    113         ? EvaluateOnNextAvailableClient(solutionMessage)
    114         : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
     113        ? EvaluateOnNextAvailableClient(solutionMessage,cancellationToken)
     114        : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken);
    115115    }
    116116
     
    132132    #region Evaluation
    133133    private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
    134     private object clientLock = new object();
     134    private readonly object clientLock = new object();
    135135
    136     private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     136    private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) {
    137137      IEvaluationServiceClient client = null;
    138138      lock (clientLock) {
Note: See TracChangeset for help on using the changeset viewer.