[3872] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3872] | 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 {
|
---|
[6760] | 33 |
|
---|
[3872] | 34 | public override bool CanChangeName { get { return false; } }
|
---|
| 35 | public override bool CanChangeDescription { get { return false; } }
|
---|
| 36 |
|
---|
[6760] | 37 | #region Parameters
|
---|
[3872] | 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 | }
|
---|
[6760] | 48 | #endregion
|
---|
[3872] | 49 |
|
---|
[4722] | 50 |
|
---|
[6760] | 51 | #region Construction & Cloning
|
---|
[4722] | 52 | [StorableConstructor]
|
---|
| 53 | protected EvaluationServiceClient(bool deserializing) : base(deserializing) { }
|
---|
[6760] | 54 | protected EvaluationServiceClient(EvaluationServiceClient original, Cloner cloner)
|
---|
| 55 | : base(original, cloner) {
|
---|
| 56 | RegisterEvents();
|
---|
[4722] | 57 | }
|
---|
[3872] | 58 | public EvaluationServiceClient()
|
---|
| 59 | : base() {
|
---|
| 60 | Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
|
---|
[3895] | 61 | 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)));
|
---|
[6760] | 62 | RegisterEvents();
|
---|
[3872] | 63 | }
|
---|
[6760] | 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new EvaluationServiceClient(this, cloner);
|
---|
| 66 | }
|
---|
| 67 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 68 | private void AfterDeserialization() {
|
---|
| 69 | ChannelParameter_ValueChanged(this, EventArgs.Empty);
|
---|
| 70 | RegisterEvents();
|
---|
| 71 | }
|
---|
| 72 | #endregion
|
---|
[3872] | 73 |
|
---|
| 74 | #region IEvaluationServiceClient Members
|
---|
| 75 | public QualityMessage Evaluate(SolutionMessage solution) {
|
---|
[3895] | 76 | int tries = 0, maxTries = RetryParameter.Value.Value;
|
---|
| 77 | bool success = false;
|
---|
| 78 | QualityMessage result = null;
|
---|
| 79 | while (!success) {
|
---|
| 80 | try {
|
---|
| 81 | tries++;
|
---|
| 82 | CheckAndOpenChannel();
|
---|
| 83 | Channel.Send(solution);
|
---|
| 84 | result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
| 85 | success = true;
|
---|
[6760] | 86 | } catch (InvalidOperationException) {
|
---|
[3895] | 87 | throw;
|
---|
[6760] | 88 | } catch {
|
---|
[3895] | 89 | if (tries >= maxTries)
|
---|
| 90 | throw;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | return result;
|
---|
[3872] | 94 | }
|
---|
| 95 |
|
---|
| 96 | public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
[3895] | 97 | int tries = 0, maxTries = RetryParameter.Value.Value;
|
---|
| 98 | bool success = false;
|
---|
| 99 | while (!success) {
|
---|
| 100 | try {
|
---|
| 101 | tries++;
|
---|
| 102 | CheckAndOpenChannel();
|
---|
| 103 | Channel.Send(solution);
|
---|
| 104 | success = true;
|
---|
[6760] | 105 | } catch (InvalidOperationException) {
|
---|
[3895] | 106 | throw;
|
---|
[6760] | 107 | } catch {
|
---|
[3895] | 108 | if (tries >= maxTries)
|
---|
| 109 | throw;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[3872] | 112 | System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
|
---|
| 113 | }
|
---|
| 114 | #endregion
|
---|
| 115 |
|
---|
[6760] | 116 | #region Auxiliary Methods
|
---|
[3872] | 117 | private void CheckAndOpenChannel() {
|
---|
| 118 | if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
|
---|
| 119 | if (!Channel.IsInitialized) {
|
---|
| 120 | try {
|
---|
| 121 | Channel.Open();
|
---|
[6760] | 122 | } catch (Exception e) {
|
---|
[3872] | 123 | throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private void ReceiveAsync(object callback) {
|
---|
[3895] | 129 | QualityMessage message = null;
|
---|
| 130 | try {
|
---|
| 131 | message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
[6760] | 132 | } catch { }
|
---|
[3872] | 133 | ((Action<QualityMessage>)callback).Invoke(message);
|
---|
| 134 | }
|
---|
[6760] | 135 |
|
---|
| 136 | private void RegisterEvents() {
|
---|
| 137 | ChannelParameter.ValueChanged += new EventHandler(ChannelParameter_ValueChanged);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void ChannelParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 141 | if (ChannelParameter.Value == null)
|
---|
| 142 | name = "Empty EvaluationServiceClient";
|
---|
| 143 | else
|
---|
| 144 | name = String.Format("{0} ServiceClient", ChannelParameter.Value.Name);
|
---|
| 145 | OnNameChanged();
|
---|
| 146 | }
|
---|
| 147 | #endregion
|
---|
[3872] | 148 | }
|
---|
| 149 | }
|
---|