Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationServiceClient.cs @ 5681

Last change on this file since 5681 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.ExternalEvaluation {
30  [Item("EvaluationServiceClient", "An RPC client that evaluates a solution.")]
31  [StorableClass]
32  public class EvaluationServiceClient : ParameterizedNamedItem, IEvaluationServiceClient {
33    public override bool CanChangeName { get { return false; } }
34    public override bool CanChangeDescription { get { return false; } }
35
36    public IValueParameter<IEvaluationChannel> ChannelParameter {
37      get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
38    }
39    public IValueParameter<IntValue> RetryParameter {
40      get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
41    }
42
43    private IEvaluationChannel Channel {
44      get { return ChannelParameter.Value; }
45    }
46
47
48    [StorableConstructor]
49    protected EvaluationServiceClient(bool deserializing) : base(deserializing) { }
50    protected EvaluationServiceClient(EvaluationServiceClient original, Cloner cloner) : base(original, cloner) { }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new EvaluationServiceClient(this, cloner);
53    }
54    public EvaluationServiceClient()
55      : base() {
56      Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
57      Parameters.Add(new ValueParameter<IntValue>("Retry", "How many times the client should retry obtaining a quality in case there is an exception. Note that it immediately aborts when the channel cannot be opened.", new IntValue(10)));
58    }
59
60    #region IEvaluationServiceClient Members
61
62    public QualityMessage Evaluate(SolutionMessage solution) {
63      int tries = 0, maxTries = RetryParameter.Value.Value;
64      bool success = false;
65      QualityMessage result = null;
66      while (!success) {
67        try {
68          tries++;
69          CheckAndOpenChannel();
70          Channel.Send(solution);
71          result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
72          success = true;
73        }
74        catch (InvalidOperationException) {
75          throw;
76        }
77        catch {
78          if (tries >= maxTries)
79            throw;
80        }
81      }
82      return result;
83    }
84
85    public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
86      int tries = 0, maxTries = RetryParameter.Value.Value;
87      bool success = false;
88      while (!success) {
89        try {
90          tries++;
91          CheckAndOpenChannel();
92          Channel.Send(solution);
93          success = true;
94        }
95        catch (InvalidOperationException) {
96          throw;
97        }
98        catch {
99          if (tries >= maxTries)
100            throw;
101        }
102      }
103      System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
104    }
105
106    #endregion
107
108    private void CheckAndOpenChannel() {
109      if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
110      if (!Channel.IsInitialized) {
111        try {
112          Channel.Open();
113        }
114        catch (Exception e) {
115          throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
116        }
117      }
118    }
119
120    private void ReceiveAsync(object callback) {
121      QualityMessage message = null;
122      try {
123        message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
124      }
125      catch { }
126      ((Action<QualityMessage>)callback).Invoke(message);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.