source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs @ 13180

Last change on this file since 13180 was 13180, checked in by pfleck, 4 years ago

#1674

  • ProtoBuff QualityMessages
    • Changed the QualityMessage to be able to be used for single- and multi-objective problems.
    • The new SingleObjectiveQualityMessage and MultiObjectiveQualityMessage now "inherits" from QualityMessages to be able to be used in a polymorph manner.
    • This is done via protobuf's nested extensions (see http://www.indelible.org/ink/protobuf-polymorphism/).
  • EvaluationCache
    • The EvaluationCache now now stores the QualityMessage instead of a single double. This way the cache can be used for single- and multi-objective problems and additionally opens the possibility to extend the quality message with any data. (previously extended data was lost when the cache was persisted and loaded again)
    • When deserializing an older version of the cache a new single objective quality message is created from the double value. This way, no compatibility is broken and version must not be incremented.
  • Adapted the ExternalEvaluationProblem to the changes of the EvaluationCache and the QualityMessage.
  • Added a MultiObjectiveExternalEvaluationProblem on basis of the ExternalEvaluationProblem and added a IExternalEvaluationProblem interface.
File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Threading;
27using Google.ProtocolBuffers;
28using HeuristicLab.Analysis;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35
36namespace HeuristicLab.Problems.ExternalEvaluation {
37  [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
38  [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 100)]
39  [StorableClass]
40  public class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding>, IExternalEvaluationProblem {
41
42    public static new Image StaticItemImage {
43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
44    }
45
46    #region Parameters
47    public OptionalValueParameter<EvaluationCache> CacheParameter {
48      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
49    }
50    public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
51      get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
52    }
53    public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
54      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
55    }
56    public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter {
57      get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
58    }
59    #endregion
60
61    #region Properties
62    public EvaluationCache Cache {
63      get { return CacheParameter.Value; }
64    }
65    public CheckedItemCollection<IEvaluationServiceClient> Clients {
66      get { return ClientsParameter.Value; }
67    }
68    public SolutionMessageBuilder MessageBuilder {
69      get { return MessageBuilderParameter.Value; }
70    }
71    public SingleObjectiveOptimizationSupportScript OptimizationSupportScript {
72      get { return SupportScriptParameter.Value; }
73    }
74    private ISingleObjectiveOptimizationSupport OptimizationSupport {
75      get { return SupportScriptParameter.Value; }
76    }
77    #endregion
78
79    [StorableConstructor]
80    protected ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
81    protected ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new ExternalEvaluationProblem(this, cloner);
84    }
85    public ExternalEvaluationProblem()
86      : base() {
87      Parameters.Remove("Maximization"); // readonly in base class
88      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
89      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
90      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
91      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
92      Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript()));
93
94      Operators.Add(new BestScopeSolutionAnalyzer());
95    }
96
97    #region Single Objective Problem Overrides
98    public override bool Maximization {
99      get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; }
100    }
101
102    public override double Evaluate(Individual individual, IRandom random) {
103      var qualityMessage = Evaluate(BuildSolutionMessage(individual));
104      if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_))
105        throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage.");
106      return qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality;
107    }
108    public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
109      return Cache == null
110        ? EvaluateOnNextAvailableClient(solutionMessage)
111        : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
112    }
113
114    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
115      OptimizationSupport.Analyze(individuals, qualities, results, random);
116    }
117
118    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
119      return OptimizationSupport.GetNeighbors(individual, random);
120    }
121    #endregion
122
123    public virtual ExtensionRegistry GetQualityMessageExtensions() {
124      var extensions = ExtensionRegistry.CreateInstance();
125      extensions.Add(SingleObjectiveQualityMessage.QualityMessage_);
126      return extensions;
127    }
128
129    #region Evaluation
130    private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
131    private object clientLock = new object();
132
133    private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
134      IEvaluationServiceClient client = null;
135      lock (clientLock) {
136        client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
137        while (client == null && Clients.CheckedItems.Any()) {
138          Monitor.Wait(clientLock);
139          client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
140        }
141        if (client != null)
142          activeClients.Add(client);
143      }
144      try {
145        return client.Evaluate(message, GetQualityMessageExtensions());
146      } finally {
147        lock (clientLock) {
148          activeClients.Remove(client);
149          Monitor.PulseAll(clientLock);
150        }
151      }
152    }
153
154    private SolutionMessage BuildSolutionMessage(Individual individual, int solutionId = 0) {
155      lock (clientLock) {
156        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
157        protobufBuilder.SolutionId = solutionId;
158        var scope = new Scope();
159        individual.CopyToScope(scope);
160        foreach (var variable in scope.Variables) {
161          try {
162            MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
163          } catch (ArgumentException ex) {
164            throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
165          }
166        }
167        return protobufBuilder.Build();
168      }
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.