Changeset 8298 for trunk/sources/HeuristicLab.Problems.ExternalEvaluation
- Timestamp:
- 07/18/12 03:22:03 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationChannel.cs
r7259 r8298 51 51 public abstract void Send(IMessage message); 52 52 53 public abstract IMessage Receive(IBuilder builder );53 public abstract IMessage Receive(IBuilder builder, ExtensionRegistry extensions); 54 54 55 55 public virtual void Close() { -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationProcessChannel.cs
r7259 r8298 111 111 try { 112 112 streamingChannel.Send(message); 113 } 114 catch { 113 } catch { 115 114 Close(); 116 115 throw; … … 118 117 } 119 118 120 public override IMessage Receive(IBuilder builder ) {119 public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) { 121 120 try { 122 return streamingChannel.Receive(builder); 123 } 124 catch { 121 return streamingChannel.Receive(builder, extensions); 122 } catch { 125 123 Close(); 126 124 throw; … … 138 136 process.WaitForExit(1000); 139 137 process.Close(); 140 } 141 catch { } 138 } catch { } 142 139 } 143 140 // for some reasons the event process_Exited does not fire -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationServiceClient.cs
r7259 r8298 21 21 22 22 using System; 23 using System.IO; 24 using Google.ProtocolBuffers; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; … … 73 75 74 76 #region IEvaluationServiceClient Members 75 public QualityMessage Evaluate(SolutionMessage solution ) {77 public QualityMessage Evaluate(SolutionMessage solution, ExtensionRegistry qualityExtensions) { 76 78 int tries = 0, maxTries = RetryParameter.Value.Value; 77 79 bool success = false; … … 82 84 CheckAndOpenChannel(); 83 85 Channel.Send(solution); 84 result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder() );86 result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder(), qualityExtensions); 85 87 success = true; 86 88 } catch (InvalidOperationException) { … … 91 93 } 92 94 } 95 if (result != null && result.SolutionId != solution.SolutionId) throw new InvalidDataException(Name + ": Received a quality for a different solution."); 93 96 return result; 94 97 } 95 98 96 public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {99 public void EvaluateAsync(SolutionMessage solution, ExtensionRegistry qualityExtensions, Action<QualityMessage> callback) { 97 100 int tries = 0, maxTries = RetryParameter.Value.Value; 98 101 bool success = false; … … 110 113 } 111 114 } 112 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);115 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), new ReceiveAsyncInfo(solution, callback, qualityExtensions)); 113 116 } 114 117 #endregion … … 126 129 } 127 130 128 private void ReceiveAsync(object callback) { 131 private void ReceiveAsync(object callbackInfo) { 132 var info = (ReceiveAsyncInfo)callbackInfo; 129 133 QualityMessage message = null; 130 134 try { 131 message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder() );135 message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder(), info.QualityExtensions); 132 136 } catch { } 133 ((Action<QualityMessage>)callback).Invoke(message); 137 if (message != null && message.SolutionId != info.SolutionMessage.SolutionId) throw new InvalidDataException(Name + ": Received a quality for a different solution."); 138 info.CallbackDelegate.Invoke(message); 134 139 } 135 140 … … 146 151 } 147 152 #endregion 153 154 private class ReceiveAsyncInfo { 155 public SolutionMessage SolutionMessage { get; set; } 156 public Action<QualityMessage> CallbackDelegate { get; set; } 157 public ExtensionRegistry QualityExtensions { get; set; } 158 public ReceiveAsyncInfo(SolutionMessage solutionMessage, Action<QualityMessage> callbackDelegate, ExtensionRegistry qualityExtensions) { 159 SolutionMessage = solutionMessage; 160 CallbackDelegate = callbackDelegate; 161 QualityExtensions = qualityExtensions; 162 } 163 } 148 164 } 149 165 } -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationStreamChannel.cs
r7259 r8298 60 60 } 61 61 62 public override IMessage Receive(IBuilder builder ) {62 public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) { 63 63 QualityMessage message; 64 64 lock (input) { // only one thread can read from the stream at one time 65 message = QualityMessage.ParseDelimitedFrom(input );65 message = QualityMessage.ParseDelimitedFrom(input, extensions); 66 66 } 67 67 return message; -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationTCPChannel.cs
r7259 r8298 88 88 #endregion 89 89 90 90 91 91 92 92 #region IExternalEvaluationChannel Members … … 105 105 byte[] buffer = EncodeDelimited(message); 106 106 socket.Send(buffer); 107 } 108 catch (SocketException) { 109 Close(); 110 throw; 111 } 112 catch (ObjectDisposedException) { 107 } catch (SocketException) { 108 Close(); 109 throw; 110 } catch (ObjectDisposedException) { 113 111 socket = null; 114 112 Close(); … … 138 136 } 139 137 140 public override IMessage Receive(IBuilder builder ) {138 public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) { 141 139 try { 142 140 byte[] buffer = GetMessageBuffer(); 143 return builder.WeakMergeFrom(ByteString.CopyFrom(buffer)).WeakBuild(); 144 } 145 catch (SocketException) { 146 Close(); 147 throw; 148 } 149 catch (ObjectDisposedException) { 141 return builder.WeakMergeFrom(ByteString.CopyFrom(buffer), extensions).WeakBuild(); 142 } catch (SocketException) { 143 Close(); 144 throw; 145 } catch (ObjectDisposedException) { 150 146 socket = null; 151 147 Close(); … … 186 182 socket.Disconnect(false); 187 183 socket.Close(); 188 } 189 catch { } 184 } catch { } 190 185 socket = null; 191 186 } -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs
r7259 r8298 24 24 using System.Linq; 25 25 using System.Threading; 26 using Google.ProtocolBuffers; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; … … 35 36 [StorableClass] 36 37 public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator { 38 protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>(); 39 protected object clientLock = new object(); 37 40 38 41 #region Parameters … … 48 51 #endregion 49 52 50 #region Parameter Values53 #region Parameter Properties 51 54 protected SolutionMessageBuilder MessageBuilder { 52 55 get { return MessageBuilderParameter.Value; } … … 55 58 get { return ClientsParameter.ActualValue; } 56 59 } 57 #endregion58 59 #region Fields60 protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();61 protected object clientLock = new object();62 60 #endregion 63 61 … … 103 101 } 104 102 105 protected QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {103 protected virtual QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) { 106 104 IEvaluationServiceClient client = null; 107 105 lock (clientLock) { … … 115 113 } 116 114 try { 117 return client.Evaluate(message );115 return client.Evaluate(message, GetQualityMessageExtensions()); 118 116 } finally { 119 117 lock (clientLock) { … … 124 122 } 125 123 126 protected SolutionMessage BuildSolutionMessage() { 124 protected virtual ExtensionRegistry GetQualityMessageExtensions() { 125 return ExtensionRegistry.CreateInstance(); 126 } 127 128 protected virtual SolutionMessage BuildSolutionMessage() { 127 129 lock (clientLock) { 128 130 SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder(); -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLab.Problems.ExternalEvaluation-3.3.csproj
r8295 r8298 253 253 set ProjectDir=$(ProjectDir) 254 254 set SolutionDir=$(SolutionDir) 255 255 256 call "$(ProjectDir)\Protos\ProcessProtos.cmd" 256 257 257 call PreBuildEvent.cmd 258 258 </PreBuildEvent> -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IEvaluationChannel.cs
r7259 r8298 48 48 /// <param name="builder">The builder that must match the message type that is to be received.</param> 49 49 /// <returns>The received message.</returns> 50 IMessage Receive(IBuilder builder );50 IMessage Receive(IBuilder builder, ExtensionRegistry extensions); 51 51 /// <summary> 52 52 /// Tells the channel to close down and terminate open connections. -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IEvaluationServiceClient.cs
r7259 r8298 21 21 22 22 using System; 23 using Google.ProtocolBuffers; 23 24 using HeuristicLab.Core; 24 25 … … 29 30 /// </summary> 30 31 /// <param name="solution">The solution message that should be evaluated.</param> 32 /// <param name="qualityExtensions">An extension registry for the quality message that specifies additional custom fields.</param> 31 33 /// <returns>The resulting quality message from the external process.</returns> 32 QualityMessage Evaluate(SolutionMessage solution );34 QualityMessage Evaluate(SolutionMessage solution, ExtensionRegistry qualityExtensions); 33 35 /// <summary> 34 36 /// Evaluates a given solution in a non-blocking manner. 35 37 /// </summary> 36 38 /// <param name="solution">The solution message that should be evaluated.</param> 39 /// <param name="qualityExtensions">An extension registry for the quality message that specifies additional custom fields.</param> 37 40 /// <param name="callback">The callback method that is invoked when evaluation is finished.</param> 38 void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback);41 void EvaluateAsync(SolutionMessage solution, ExtensionRegistry qualityExtensions, Action<QualityMessage> callback); 39 42 } 40 43 } -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Protos/ExternalEvaluationMessages.proto
r3881 r8298 67 67 required int32 solutionId = 1; 68 68 required double quality = 2; 69 70 extensions 1000 to max; 69 71 } 70 72
Note: See TracChangeset
for help on using the changeset viewer.