[3859] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Core;
|
---|
[3862] | 24 | using HeuristicLab.Data;
|
---|
[3859] | 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
| 30 | [Item("ExternalEvaluationValuesCollector", "Creates a solution message, and communicates it via the driver to receive a quality message.")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator {
|
---|
| 33 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 34 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 35 | }
|
---|
[3872] | 36 | public IValueLookupParameter<IEvaluationServiceClient> ClientParameter {
|
---|
| 37 | get { return (IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]; }
|
---|
[3859] | 38 | }
|
---|
| 39 |
|
---|
[3881] | 40 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
| 41 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | private SolutionMessageBuilder MessageBuilder {
|
---|
| 45 | get { return MessageBuilderParameter.Value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[3859] | 48 | public ExternalEvaluator()
|
---|
| 49 | : base() {
|
---|
| 50 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
|
---|
[3872] | 51 | Parameters.Add(new ValueLookupParameter<IEvaluationServiceClient>("Client", "The client that communicates with the external process."));
|
---|
[3881] | 52 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
|
---|
[3859] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public override IOperation Apply() {
|
---|
[3872] | 56 | IEvaluationServiceClient client = ClientParameter.ActualValue;
|
---|
[3881] | 57 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
| 58 | protobufBuilder.SolutionId = 0;
|
---|
[3859] | 59 | foreach (IParameter param in CollectedValues) {
|
---|
| 60 | IItem value = param.ActualValue;
|
---|
| 61 | if (value != null) {
|
---|
| 62 | ILookupParameter lookupParam = param as ILookupParameter;
|
---|
| 63 | string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
|
---|
| 64 | try {
|
---|
[3881] | 65 | MessageBuilder.AddToMessage(value, name, protobufBuilder);
|
---|
[3859] | 66 | } catch (ArgumentException ex) {
|
---|
| 67 | throw new InvalidOperationException("ERROR in " + Name + ": Parameter " + name + " cannot be added to the message.", ex);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[3881] | 71 | QualityMessage answer = client.Evaluate(protobufBuilder.Build());
|
---|
[3859] | 72 | if (QualityParameter.ActualValue == null)
|
---|
| 73 | QualityParameter.ActualValue = new DoubleValue(answer.Quality);
|
---|
| 74 | else QualityParameter.ActualValue.Value = answer.Quality;
|
---|
| 75 |
|
---|
| 76 | return base.Apply();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|