- Timestamp:
- 10/04/19 17:31:54 (5 years ago)
- 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 31 31 using System.Threading; 32 32 using Google.ProtocolBuffers; 33 using HEAL.Attic; 33 34 using HeuristicLab.Common; 34 35 using HeuristicLab.Common.Resources; … … 36 37 using HeuristicLab.Data; 37 38 using HeuristicLab.Parameters; 38 using HEAL.Attic;39 39 40 40 namespace HeuristicLab.Problems.ExternalEvaluation { … … 50 50 private byte[] rawMessage; 51 51 52 private object lockObject = new object();52 private readonly object lockObject = new object(); 53 53 54 54 public byte[] RawMessage { … … 116 116 } 117 117 118 public delegate QualityMessage Evaluator(SolutionMessage message );118 public delegate QualityMessage Evaluator(SolutionMessage message, CancellationToken cancellationToken); 119 119 #endregion 120 120 … … 124 124 125 125 private HashSet<string> activeEvaluations = new HashSet<string>(); 126 private object cacheLock = new object();126 private readonly object cacheLock = new object(); 127 127 #endregion 128 128 … … 221 221 } 222 222 223 void CapacityChanged(object sender, EventArgs e) {223 private void CapacityChanged(object sender, EventArgs e) { 224 224 if (Capacity < 0) 225 225 throw new ArgumentOutOfRangeException("Cache capacity cannot be less than zero"); … … 240 240 } 241 241 242 public QualityMessage GetValue(SolutionMessage message, Evaluator evaluate, ExtensionRegistry extensions ) {242 public QualityMessage GetValue(SolutionMessage message, Evaluator evaluate, ExtensionRegistry extensions, CancellationToken cancellationToken) { 243 243 var entry = new CacheEntry(message.ToString()); 244 244 bool lockTaken = false; … … 267 267 OnChanged(); 268 268 try { 269 entry.SetMessage(evaluate(message ));269 entry.SetMessage(evaluate(message, cancellationToken)); 270 270 Monitor.Enter(cacheLock, ref lockTaken); 271 271 index[entry] = list.AddLast(entry); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/Interfaces/IExternalEvaluationProblem.cs
r17226 r17320 20 20 #endregion 21 21 22 using System.Threading; 22 23 using Google.ProtocolBuffers; 24 using HEAL.Attic; 23 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Optimization; 25 using HEAL.Attic;26 27 27 28 namespace HeuristicLab.Problems.ExternalEvaluation { … … 32 33 CheckedItemCollection<IEvaluationServiceClient> Clients { get; } 33 34 34 QualityMessage Evaluate(SolutionMessage solutionMessage );35 QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken); 35 36 ExtensionRegistry GetQualityMessageExtensions(); 36 37 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs
r17317 r17320 106 106 } 107 107 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); 110 110 if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_)) 111 111 throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage."); 112 112 return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray(); 113 113 } 114 public virtual QualityMessage Evaluate(SolutionMessage solutionMessage ) {114 public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) { 115 115 return Cache == null 116 ? EvaluateOnNextAvailableClient(solutionMessage )117 : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions() );116 ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken) 117 : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken); 118 118 } 119 119 … … 132 132 #region Evaluation 133 133 private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>(); 134 private object clientLock = new object();134 private readonly object clientLock = new object(); 135 135 136 private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message ) {136 private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) { 137 137 IEvaluationServiceClient client = null; 138 138 lock (clientLock) { … … 147 147 try { 148 148 return client.Evaluate(message, GetQualityMessageExtensions()); 149 } 150 finally { 149 } finally { 151 150 lock (clientLock) { 152 151 activeClients.Remove(client); … … 165 164 try { 166 165 MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder); 167 } 168 catch (ArgumentException ex) { 166 } catch (ArgumentException ex) { 169 167 throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex); 170 168 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/SingleObjectiveExternalEvaluationProblem.cs
r17279 r17320 103 103 104 104 #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); 107 107 if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_)) 108 108 throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage."); 109 109 return qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality; 110 110 } 111 public virtual QualityMessage Evaluate(SolutionMessage solutionMessage ) {111 public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) { 112 112 return Cache == null 113 ? EvaluateOnNextAvailableClient(solutionMessage )114 : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions() );113 ? EvaluateOnNextAvailableClient(solutionMessage,cancellationToken) 114 : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken); 115 115 } 116 116 … … 132 132 #region Evaluation 133 133 private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>(); 134 private object clientLock = new object();134 private readonly object clientLock = new object(); 135 135 136 private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message ) {136 private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) { 137 137 IEvaluationServiceClient client = null; 138 138 lock (clientLock) {
Note: See TracChangeset
for help on using the changeset viewer.