[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;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3872] | 24 | using HeuristicLab.Core;
|
---|
[4068] | 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[3872] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace 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 | }
|
---|
[3895] | 39 | public IValueParameter<IntValue> RetryParameter {
|
---|
| 40 | get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
|
---|
| 41 | }
|
---|
[3872] | 42 |
|
---|
| 43 | private IEvaluationChannel Channel {
|
---|
| 44 | get { return ChannelParameter.Value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[4722] | 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 | }
|
---|
[3872] | 54 | public EvaluationServiceClient()
|
---|
| 55 | : base() {
|
---|
| 56 | Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
|
---|
[3895] | 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)));
|
---|
[3872] | 58 | }
|
---|
| 59 |
|
---|
| 60 | #region IEvaluationServiceClient Members
|
---|
[4068] | 61 |
|
---|
[3872] | 62 | public QualityMessage Evaluate(SolutionMessage solution) {
|
---|
[3895] | 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;
|
---|
[4068] | 73 | }
|
---|
| 74 | catch (InvalidOperationException) {
|
---|
[3895] | 75 | throw;
|
---|
[4068] | 76 | }
|
---|
| 77 | catch {
|
---|
[3895] | 78 | if (tries >= maxTries)
|
---|
| 79 | throw;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return result;
|
---|
[3872] | 83 | }
|
---|
| 84 |
|
---|
| 85 | public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
[3895] | 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;
|
---|
[4068] | 94 | }
|
---|
| 95 | catch (InvalidOperationException) {
|
---|
[3895] | 96 | throw;
|
---|
[4068] | 97 | }
|
---|
| 98 | catch {
|
---|
[3895] | 99 | if (tries >= maxTries)
|
---|
| 100 | throw;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[3872] | 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();
|
---|
[4068] | 113 | }
|
---|
| 114 | catch (Exception e) {
|
---|
[3872] | 115 | throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private void ReceiveAsync(object callback) {
|
---|
[3895] | 121 | QualityMessage message = null;
|
---|
| 122 | try {
|
---|
| 123 | message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
[4068] | 124 | }
|
---|
| 125 | catch { }
|
---|
[3872] | 126 | ((Action<QualityMessage>)callback).Invoke(message);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|