Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs @ 4555

Last change on this file since 4555 was 3881, checked in by abeham, 15 years ago

#866

  • Added license headers to designer files
  • Refactored message building, to be user customizable and better extendable
    • A SolutionMessageBuilder holds several Converters
    • A Converter specifies several types to convert and a conversion method
  • ExternalEvaluator has the message builder as parameter and exposes it to the UI through the parameters collection
  • The proto file has been updated to include a length field in the array variables
  • Converters exist for all types in HeuristicLab.Data
File size: 3.5 KB
RevLine 
[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
22using System;
23using HeuristicLab.Core;
[3862]24using HeuristicLab.Data;
[3859]25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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}
Note: See TracBrowser for help on using the repository browser.