Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6189


Ignore:
Timestamp:
05/12/11 15:50:06 (14 years ago)
Author:
epitzer
Message:

Replace the ExternalEvaluator's Client property with a CheckedItemCollection of clients and synchronize access so every client is used by only one thread at a time. (#1526)

Location:
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/CachedExternalEvaluator.cs

    r6183 r6189  
    4444      set { QualityParameter.ActualValue = value; }
    4545    }
    46     protected IEvaluationServiceClient Client {
    47       get { return ClientParameter.ActualValue; }
    48     }
    4946    protected EvaluationCache Cache {
    5047      get { return CacheParameter.ActualValue; }
     
    7168
    7269      if (Quality == null) Quality = new DoubleValue(0);
    73       Quality.Value = Cache.GetValue(BuildSolutionMessage(), m => Client.Evaluate(m).Quality);
     70      Quality.Value = Cache.GetValue(BuildSolutionMessage(), m => EvaluateOnNextAvailableClient(m).Quality);
    7471      if (Successor != null)
    7572        return ExecutionContext.CreateOperation(Successor);
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs

    r6183 r6189  
    5353
    5454    #region Parameters
    55     public IValueParameter<IEvaluationServiceClient> ClientParameter {
    56       get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
     55    public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
     56      get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
    5757    }
    5858    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
     
    144144      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
    145145
    146       Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
     146      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
    147147      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
    148148      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
     
    155155      InitializeOperators();
    156156      AttachEventHandlers();
     157    }
     158    [StorableHook(HookType.AfterDeserialization)]
     159    private void AfterDeserialization() {
     160      // BackwardsCompatibility3.3
     161      #region Backwards compatible code, remove with 3.4
     162      if (!Parameters.ContainsKey("Clients")) {
     163        Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
     164        if (Parameters.ContainsKey("Client")) {
     165          var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
     166          if (client != null)
     167            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
     168          Parameters.Remove("Client");
     169        }
     170      }
     171      #endregion
    157172    }
    158173
     
    227242    }
    228243    private void ParameterizeEvaluator() {
    229       Evaluator.ClientParameter.ActualName = ClientParameter.Name;
     244      Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
    230245    }
    231246    private void ParameterizeOperators() {
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs

    r6140 r6189  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
     25using System.Threading;
    2326using HeuristicLab.Common;
    2427using HeuristicLab.Core;
     
    3336  public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator {
    3437
     38    #region Parameters
    3539    public ILookupParameter<DoubleValue> QualityParameter {
    3640      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    3741    }
    38     public IValueLookupParameter<IEvaluationServiceClient> ClientParameter {
    39       get { return (IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]; }
     42    public IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
     43      get { return (ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
    4044    }
    41 
    4245    public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
    4346      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
    4447    }
     48    #endregion
    4549
     50    #region Parameter Values
    4651    protected SolutionMessageBuilder MessageBuilder {
    4752      get { return MessageBuilderParameter.Value; }
    4853    }
     54    protected CheckedItemCollection<IEvaluationServiceClient> Clients {
     55      get { return ClientsParameter.ActualValue; }
     56    }
     57    #endregion
    4958
     59    #region Fields
     60    protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
     61    protected object clientLock = new object();
     62    protected AutoResetEvent clientAvailable = new AutoResetEvent(false);
     63    #endregion
     64
     65    #region Construction & Cloning
    5066    [StorableConstructor]
    5167    protected ExternalEvaluator(bool deserializing) : base(deserializing) { }
    5268    protected ExternalEvaluator(ExternalEvaluator original, Cloner cloner) : base(original, cloner) { }
     69    public ExternalEvaluator()
     70      : base() {
     71      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
     72      Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
     73      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     74    }
    5375    public override IDeepCloneable Clone(Cloner cloner) {
    5476      return new ExternalEvaluator(this, cloner);
    5577    }
    56     public ExternalEvaluator()
    57       : base() {
    58       Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
    59       Parameters.Add(new ValueLookupParameter<IEvaluationServiceClient>("Client", "The client that communicates with the external process."));
    60       Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     78    [StorableHook(HookType.AfterDeserialization)]
     79    private void AfterDeserialization() {
     80      // BackwardsCompatibility3.3
     81      #region Backwards compatible code, remove with 3.4
     82      if (!Parameters.ContainsKey("Clients")) {
     83        Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
     84        if (Parameters.ContainsKey("Client")) {
     85          var client = ((IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
     86          if (client != null)
     87            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
     88          Parameters.Remove("Client");
     89        }
     90      }
     91      #endregion
    6192    }
     93    #endregion
    6294
    6395    public override IOperation Apply() {
    64       SolutionMessage message = BuildSolutionMessage();
    6596
    66       IEvaluationServiceClient client = ClientParameter.ActualValue;
    67       QualityMessage answer = client.Evaluate(message);
     97      QualityMessage answer = EvaluateOnNextAvailableClient(BuildSolutionMessage());
    6898
    6999      if (QualityParameter.ActualValue == null)
     
    74104    }
    75105
    76     protected virtual SolutionMessage BuildSolutionMessage() {
    77       SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
    78       protobufBuilder.SolutionId = 0;
    79       foreach (IParameter param in CollectedValues) {
    80         IItem value = param.ActualValue;
    81         if (value != null) {
    82           ILookupParameter lookupParam = param as ILookupParameter;
    83           string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
    84           try {
    85             MessageBuilder.AddToMessage(value, name, protobufBuilder);
    86           } catch (ArgumentException ex) {
    87             throw new InvalidOperationException("ERROR in " + Name + ": Parameter " + name + " cannot be added to the message.", ex);
     106    protected QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     107      IEvaluationServiceClient client = null;
     108      lock (clientLock) {
     109        client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
     110        while (client == null && Clients.Count > 0) {
     111          Monitor.Exit(clientLock);
     112          clientAvailable.WaitOne();
     113          Monitor.Enter(clientLock);
     114          client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
     115        }
     116        if (client != null)
     117          activeClients.Add(client);
     118      }
     119      try {
     120        return client.Evaluate(message);
     121      } finally {
     122        lock (clientLock) {
     123          activeClients.Remove(client);
     124          clientAvailable.Set();
     125        }
     126      }
     127    }
     128
     129    protected SolutionMessage BuildSolutionMessage() {
     130      lock (clientLock) {
     131        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
     132        protobufBuilder.SolutionId = 0;
     133        foreach (IParameter param in CollectedValues) {
     134          IItem value = param.ActualValue;
     135          if (value != null) {
     136            ILookupParameter lookupParam = param as ILookupParameter;
     137            string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
     138            try {
     139              MessageBuilder.AddToMessage(value, name, protobufBuilder);
     140            } catch (ArgumentException ex) {
     141              throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex);
     142            }
    88143          }
    89144        }
     145        return protobufBuilder.Build();
    90146      }
    91       return protobufBuilder.Build();
    92147    }
     148
    93149  }
    94150}
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IExternalEvaluationProblemEvaluator.cs

    r5445 r6189  
    2525namespace HeuristicLab.Problems.ExternalEvaluation {
    2626  public interface IExternalEvaluationProblemEvaluator : ISingleObjectiveEvaluator {
    27     IValueLookupParameter<IEvaluationServiceClient> ClientParameter { get; }
     27    IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter { get; }
    2828  }
    2929}
Note: See TracChangeset for help on using the changeset viewer.