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