Changeset 8298


Ignore:
Timestamp:
07/18/12 03:22:03 (7 years ago)
Author:
abeham
Message:

#1896:

  • Added tags 1000 to max which can be used as extensions to the quality message
  • Updated the evaluator to allow better subclassing in case extensions are used
  • Updated the method signatures to pipe the extension registry down to the channel
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  
    5151    public abstract void Send(IMessage message);
    5252
    53     public abstract IMessage Receive(IBuilder builder);
     53    public abstract IMessage Receive(IBuilder builder, ExtensionRegistry extensions);
    5454
    5555    public virtual void Close() {
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationProcessChannel.cs

    r7259 r8298  
    111111      try {
    112112        streamingChannel.Send(message);
    113       }
    114       catch {
     113      } catch {
    115114        Close();
    116115        throw;
     
    118117    }
    119118
    120     public override IMessage Receive(IBuilder builder) {
     119    public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) {
    121120      try {
    122         return streamingChannel.Receive(builder);
    123       }
    124       catch {
     121        return streamingChannel.Receive(builder, extensions);
     122      } catch {
    125123        Close();
    126124        throw;
     
    138136              process.WaitForExit(1000);
    139137              process.Close();
    140             }
    141             catch { }
     138            } catch { }
    142139          }
    143140          // for some reasons the event process_Exited does not fire
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationServiceClient.cs

    r7259 r8298  
    2121
    2222using System;
     23using System.IO;
     24using Google.ProtocolBuffers;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    7375
    7476    #region IEvaluationServiceClient Members
    75     public QualityMessage Evaluate(SolutionMessage solution) {
     77    public QualityMessage Evaluate(SolutionMessage solution, ExtensionRegistry qualityExtensions) {
    7678      int tries = 0, maxTries = RetryParameter.Value.Value;
    7779      bool success = false;
     
    8284          CheckAndOpenChannel();
    8385          Channel.Send(solution);
    84           result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
     86          result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder(), qualityExtensions);
    8587          success = true;
    8688        } catch (InvalidOperationException) {
     
    9193        }
    9294      }
     95      if (result != null && result.SolutionId != solution.SolutionId) throw new InvalidDataException(Name + ": Received a quality for a different solution.");
    9396      return result;
    9497    }
    9598
    96     public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
     99    public void EvaluateAsync(SolutionMessage solution, ExtensionRegistry qualityExtensions, Action<QualityMessage> callback) {
    97100      int tries = 0, maxTries = RetryParameter.Value.Value;
    98101      bool success = false;
     
    110113        }
    111114      }
    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));
    113116    }
    114117    #endregion
     
    126129    }
    127130
    128     private void ReceiveAsync(object callback) {
     131    private void ReceiveAsync(object callbackInfo) {
     132      var info = (ReceiveAsyncInfo)callbackInfo;
    129133      QualityMessage message = null;
    130134      try {
    131         message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
     135        message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder(), info.QualityExtensions);
    132136      } 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);
    134139    }
    135140
     
    146151    }
    147152    #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    }
    148164  }
    149165}
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationStreamChannel.cs

    r7259 r8298  
    6060    }
    6161
    62     public override IMessage Receive(IBuilder builder) {
     62    public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) {
    6363      QualityMessage message;
    6464      lock (input) { // only one thread can read from the stream at one time
    65         message = QualityMessage.ParseDelimitedFrom(input);
     65        message = QualityMessage.ParseDelimitedFrom(input, extensions);
    6666      }
    6767      return message;
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationTCPChannel.cs

    r7259 r8298  
    8888    #endregion
    8989
    90    
     90
    9191
    9292    #region IExternalEvaluationChannel Members
     
    105105        byte[] buffer = EncodeDelimited(message);
    106106        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) {
    113111        socket = null;
    114112        Close();
     
    138136    }
    139137
    140     public override IMessage Receive(IBuilder builder) {
     138    public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) {
    141139      try {
    142140        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) {
    150146        socket = null;
    151147        Close();
     
    186182            socket.Disconnect(false);
    187183          socket.Close();
    188         }
    189         catch { }
     184        } catch { }
    190185        socket = null;
    191186      }
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs

    r7259 r8298  
    2424using System.Linq;
    2525using System.Threading;
     26using Google.ProtocolBuffers;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
     
    3536  [StorableClass]
    3637  public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator {
     38    protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
     39    protected object clientLock = new object();
    3740
    3841    #region Parameters
     
    4851    #endregion
    4952
    50     #region Parameter Values
     53    #region Parameter Properties
    5154    protected SolutionMessageBuilder MessageBuilder {
    5255      get { return MessageBuilderParameter.Value; }
     
    5558      get { return ClientsParameter.ActualValue; }
    5659    }
    57     #endregion
    58 
    59     #region Fields
    60     protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
    61     protected object clientLock = new object();
    6260    #endregion
    6361
     
    103101    }
    104102
    105     protected QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     103    protected virtual QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
    106104      IEvaluationServiceClient client = null;
    107105      lock (clientLock) {
     
    115113      }
    116114      try {
    117         return client.Evaluate(message);
     115        return client.Evaluate(message, GetQualityMessageExtensions());
    118116      } finally {
    119117        lock (clientLock) {
     
    124122    }
    125123
    126     protected SolutionMessage BuildSolutionMessage() {
     124    protected virtual ExtensionRegistry GetQualityMessageExtensions() {
     125      return ExtensionRegistry.CreateInstance();
     126    }
     127
     128    protected virtual SolutionMessage BuildSolutionMessage() {
    127129      lock (clientLock) {
    128130        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLab.Problems.ExternalEvaluation-3.3.csproj

    r8295 r8298  
    253253set ProjectDir=$(ProjectDir)
    254254set SolutionDir=$(SolutionDir)
     255
    255256call "$(ProjectDir)\Protos\ProcessProtos.cmd"
    256 
    257257call PreBuildEvent.cmd
    258258</PreBuildEvent>
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IEvaluationChannel.cs

    r7259 r8298  
    4848    /// <param name="builder">The builder that must match the message type that is to be received.</param>
    4949    /// <returns>The received message.</returns>
    50     IMessage Receive(IBuilder builder);
     50    IMessage Receive(IBuilder builder, ExtensionRegistry extensions);
    5151    /// <summary>
    5252    /// Tells the channel to close down and terminate open connections.
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IEvaluationServiceClient.cs

    r7259 r8298  
    2121
    2222using System;
     23using Google.ProtocolBuffers;
    2324using HeuristicLab.Core;
    2425
     
    2930    /// </summary>
    3031    /// <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>
    3133    /// <returns>The resulting quality message from the external process.</returns>
    32     QualityMessage Evaluate(SolutionMessage solution);
     34    QualityMessage Evaluate(SolutionMessage solution, ExtensionRegistry qualityExtensions);
    3335    /// <summary>
    3436    /// Evaluates a given solution in a non-blocking manner.
    3537    /// </summary>
    3638    /// <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>
    3740    /// <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);
    3942  }
    4043}
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Protos/ExternalEvaluationMessages.proto

    r3881 r8298  
    6767  required int32 solutionId = 1;
    6868  required double quality = 2;
     69
     70  extensions 1000 to max;
    6971}
    7072 
Note: See TracChangeset for help on using the changeset viewer.