[3872] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[3895] | 29 | using HeuristicLab.Data;
|
---|
[3872] | 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
| 32 | [Item("EvaluationServiceClient", "An RPC client that evaluates a solution.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public class EvaluationServiceClient : ParameterizedNamedItem, IEvaluationServiceClient {
|
---|
| 35 | public override bool CanChangeName { get { return false; } }
|
---|
| 36 | public override bool CanChangeDescription { get { return false; } }
|
---|
| 37 |
|
---|
| 38 | public IValueParameter<IEvaluationChannel> ChannelParameter {
|
---|
| 39 | get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
|
---|
| 40 | }
|
---|
[3895] | 41 | public IValueParameter<IntValue> RetryParameter {
|
---|
| 42 | get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
|
---|
| 43 | }
|
---|
[3872] | 44 |
|
---|
| 45 | private IEvaluationChannel Channel {
|
---|
| 46 | get { return ChannelParameter.Value; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public EvaluationServiceClient()
|
---|
| 50 | : base() {
|
---|
| 51 | Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
|
---|
[3895] | 52 | 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)));
|
---|
[3872] | 53 | }
|
---|
| 54 |
|
---|
| 55 | #region IEvaluationServiceClient Members
|
---|
| 56 |
|
---|
| 57 | public QualityMessage Evaluate(SolutionMessage solution) {
|
---|
[3895] | 58 | int tries = 0, maxTries = RetryParameter.Value.Value;
|
---|
| 59 | bool success = false;
|
---|
| 60 | QualityMessage result = null;
|
---|
| 61 | while (!success) {
|
---|
| 62 | try {
|
---|
| 63 | tries++;
|
---|
| 64 | CheckAndOpenChannel();
|
---|
| 65 | Channel.Send(solution);
|
---|
| 66 | result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
| 67 | success = true;
|
---|
| 68 | } catch (InvalidOperationException) {
|
---|
| 69 | throw;
|
---|
| 70 | } catch {
|
---|
| 71 | if (tries >= maxTries)
|
---|
| 72 | throw;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | return result;
|
---|
[3872] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
[3895] | 79 | int tries = 0, maxTries = RetryParameter.Value.Value;
|
---|
| 80 | bool success = false;
|
---|
| 81 | while (!success) {
|
---|
| 82 | try {
|
---|
| 83 | tries++;
|
---|
| 84 | CheckAndOpenChannel();
|
---|
| 85 | Channel.Send(solution);
|
---|
| 86 | success = true;
|
---|
| 87 | } catch (InvalidOperationException) {
|
---|
| 88 | throw;
|
---|
| 89 | } catch {
|
---|
| 90 | if (tries >= maxTries)
|
---|
| 91 | throw;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[3872] | 94 | System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | #endregion
|
---|
| 98 |
|
---|
| 99 | private void CheckAndOpenChannel() {
|
---|
| 100 | if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
|
---|
| 101 | if (!Channel.IsInitialized) {
|
---|
| 102 | try {
|
---|
| 103 | Channel.Open();
|
---|
[3895] | 104 | } catch (Exception e) {
|
---|
[3872] | 105 | throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void ReceiveAsync(object callback) {
|
---|
[3895] | 111 | QualityMessage message = null;
|
---|
| 112 | try {
|
---|
| 113 | message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
| 114 | } catch { }
|
---|
[3872] | 115 | ((Action<QualityMessage>)callback).Invoke(message);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|