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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
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 |
|
---|
34 | public override bool CanChangeName { get { return false; } }
|
---|
35 | public override bool CanChangeDescription { get { return false; } }
|
---|
36 |
|
---|
37 | #region Parameters
|
---|
38 | public IValueParameter<IEvaluationChannel> ChannelParameter {
|
---|
39 | get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
|
---|
40 | }
|
---|
41 | public IValueParameter<IntValue> RetryParameter {
|
---|
42 | get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private IEvaluationChannel Channel {
|
---|
46 | get { return ChannelParameter.Value; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 |
|
---|
51 | #region Construction & Cloning
|
---|
52 | [StorableConstructor]
|
---|
53 | protected EvaluationServiceClient(bool deserializing) : base(deserializing) { }
|
---|
54 | protected EvaluationServiceClient(EvaluationServiceClient original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | RegisterEvents();
|
---|
57 | }
|
---|
58 | public EvaluationServiceClient()
|
---|
59 | : base() {
|
---|
60 | Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
|
---|
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)));
|
---|
62 | RegisterEvents();
|
---|
63 | }
|
---|
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
|
---|
73 |
|
---|
74 | #region IEvaluationServiceClient Members
|
---|
75 | public QualityMessage Evaluate(SolutionMessage solution) {
|
---|
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;
|
---|
86 | } catch (InvalidOperationException) {
|
---|
87 | throw;
|
---|
88 | } catch {
|
---|
89 | if (tries >= maxTries)
|
---|
90 | throw;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return result;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
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;
|
---|
105 | } catch (InvalidOperationException) {
|
---|
106 | throw;
|
---|
107 | } catch {
|
---|
108 | if (tries >= maxTries)
|
---|
109 | throw;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | #region Auxiliary Methods
|
---|
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();
|
---|
122 | } catch (Exception e) {
|
---|
123 | throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void ReceiveAsync(object callback) {
|
---|
129 | QualityMessage message = null;
|
---|
130 | try {
|
---|
131 | message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
132 | } catch { }
|
---|
133 | ((Action<QualityMessage>)callback).Invoke(message);
|
---|
134 | }
|
---|
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
|
---|
148 | }
|
---|
149 | }
|
---|