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