1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
34 | [Item("ExternalEvaluationValuesCollector", "Creates a solution message, and communicates it via the driver to receive a quality message.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator {
|
---|
37 |
|
---|
38 | #region Parameters
|
---|
39 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
40 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
41 | }
|
---|
42 | public IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
43 | get { return (ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
44 | }
|
---|
45 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
46 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | #region Parameter Values
|
---|
51 | protected SolutionMessageBuilder MessageBuilder {
|
---|
52 | get { return MessageBuilderParameter.Value; }
|
---|
53 | }
|
---|
54 | protected CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
55 | get { return ClientsParameter.ActualValue; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region Fields
|
---|
60 | protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
61 | protected object clientLock = new object();
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Construction & Cloning
|
---|
65 | [StorableConstructor]
|
---|
66 | protected ExternalEvaluator(bool deserializing) : base(deserializing) { }
|
---|
67 | protected ExternalEvaluator(ExternalEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
68 | public ExternalEvaluator()
|
---|
69 | : base() {
|
---|
70 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
|
---|
71 | Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
|
---|
72 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
|
---|
73 | }
|
---|
74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
75 | return new ExternalEvaluator(this, cloner);
|
---|
76 | }
|
---|
77 | [StorableHook(HookType.AfterDeserialization)]
|
---|
78 | private void AfterDeserialization() {
|
---|
79 | // BackwardsCompatibility3.3
|
---|
80 | #region Backwards compatible code, remove with 3.4
|
---|
81 | if (!Parameters.ContainsKey("Clients")) {
|
---|
82 | Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
|
---|
83 | if (Parameters.ContainsKey("Client")) {
|
---|
84 | var client = ((IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
|
---|
85 | if (client != null)
|
---|
86 | ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
|
---|
87 | Parameters.Remove("Client");
|
---|
88 | }
|
---|
89 | }
|
---|
90 | #endregion
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | public override IOperation Apply() {
|
---|
95 |
|
---|
96 | QualityMessage answer = EvaluateOnNextAvailableClient(BuildSolutionMessage());
|
---|
97 |
|
---|
98 | if (QualityParameter.ActualValue == null)
|
---|
99 | QualityParameter.ActualValue = new DoubleValue(answer.Quality);
|
---|
100 | else QualityParameter.ActualValue.Value = answer.Quality;
|
---|
101 |
|
---|
102 | return base.Apply();
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
|
---|
106 | IEvaluationServiceClient client = null;
|
---|
107 | lock (clientLock) {
|
---|
108 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
109 | while (client == null && Clients.CheckedItems.Count() > 0) {
|
---|
110 | Monitor.Wait(clientLock);
|
---|
111 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
112 | }
|
---|
113 | if (client != null)
|
---|
114 | activeClients.Add(client);
|
---|
115 | }
|
---|
116 | try {
|
---|
117 | return client.Evaluate(message);
|
---|
118 | } finally {
|
---|
119 | lock (clientLock) {
|
---|
120 | activeClients.Remove(client);
|
---|
121 | Monitor.PulseAll(clientLock);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | protected SolutionMessage BuildSolutionMessage() {
|
---|
127 | lock (clientLock) {
|
---|
128 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
129 | protobufBuilder.SolutionId = 0;
|
---|
130 | foreach (IParameter param in CollectedValues) {
|
---|
131 | IItem value = param.ActualValue;
|
---|
132 | if (value != null) {
|
---|
133 | ILookupParameter lookupParam = param as ILookupParameter;
|
---|
134 | string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
|
---|
135 | try {
|
---|
136 | MessageBuilder.AddToMessage(value, name, protobufBuilder);
|
---|
137 | } catch (ArgumentException ex) {
|
---|
138 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return protobufBuilder.Build();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|
147 | }
|
---|