[3859] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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;
|
---|
[4722] | 26 | using HeuristicLab.Common;
|
---|
[3859] | 27 | using HeuristicLab.Core;
|
---|
[3862] | 28 | using HeuristicLab.Data;
|
---|
[3859] | 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 {
|
---|
[6140] | 37 |
|
---|
[6189] | 38 | #region Parameters
|
---|
[3859] | 39 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 40 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 41 | }
|
---|
[6189] | 42 | public IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
| 43 | get { return (ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
[3859] | 44 | }
|
---|
[3881] | 45 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
| 46 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
| 47 | }
|
---|
[6189] | 48 | #endregion
|
---|
[3881] | 49 |
|
---|
[6189] | 50 | #region Parameter Values
|
---|
[6140] | 51 | protected SolutionMessageBuilder MessageBuilder {
|
---|
[3881] | 52 | get { return MessageBuilderParameter.Value; }
|
---|
| 53 | }
|
---|
[6189] | 54 | protected CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 55 | get { return ClientsParameter.ActualValue; }
|
---|
| 56 | }
|
---|
| 57 | #endregion
|
---|
[3881] | 58 |
|
---|
[6189] | 59 | #region Fields
|
---|
| 60 | protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
[6519] | 61 | protected object clientLock = new object();
|
---|
[6189] | 62 | #endregion
|
---|
| 63 |
|
---|
| 64 | #region Construction & Cloning
|
---|
[4722] | 65 | [StorableConstructor]
|
---|
| 66 | protected ExternalEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 67 | protected ExternalEvaluator(ExternalEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3859] | 68 | public ExternalEvaluator()
|
---|
| 69 | : base() {
|
---|
| 70 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
|
---|
[6189] | 71 | Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
|
---|
[3881] | 72 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
|
---|
[3859] | 73 | }
|
---|
[6189] | 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
|
---|
[3859] | 93 |
|
---|
| 94 | public override IOperation Apply() {
|
---|
[6140] | 95 |
|
---|
[6189] | 96 | QualityMessage answer = EvaluateOnNextAvailableClient(BuildSolutionMessage());
|
---|
[6140] | 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 |
|
---|
[6189] | 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.Count > 0) {
|
---|
[6460] | 110 | Monitor.Wait(clientLock);
|
---|
[6189] | 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);
|
---|
[6460] | 121 | Monitor.PulseAll(clientLock);
|
---|
[6189] | 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 | }
|
---|
[3859] | 140 | }
|
---|
| 141 | }
|
---|
[6189] | 142 | return protobufBuilder.Build();
|
---|
[3859] | 143 | }
|
---|
| 144 | }
|
---|
[6189] | 145 |
|
---|
[3859] | 146 | }
|
---|
| 147 | }
|
---|