Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs @ 5698

Last change on this file since 5698 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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    }
37    public IValueLookupParameter<IEvaluationServiceClient> ClientParameter {
38      get { return (IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]; }
39    }
40
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
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    }
55    public ExternalEvaluator()
56      : base() {
57      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
58      Parameters.Add(new ValueLookupParameter<IEvaluationServiceClient>("Client", "The client that communicates with the external process."));
59      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
60    }
61
62    public override IOperation Apply() {
63      IEvaluationServiceClient client = ClientParameter.ActualValue;
64      SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
65      protobufBuilder.SolutionId = 0;
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 {
72            MessageBuilder.AddToMessage(value, name, protobufBuilder);
73          }
74          catch (ArgumentException ex) {
75            throw new InvalidOperationException("ERROR in " + Name + ": Parameter " + name + " cannot be added to the message.", ex);
76          }
77        }
78      }
79      QualityMessage answer = client.Evaluate(protobufBuilder.Build());
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}
Note: See TracBrowser for help on using the repository browser.