[3852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3852] | 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 |
|
---|
[3861] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[11892] | 26 | using System.Threading;
|
---|
| 27 | using Google.ProtocolBuffers;
|
---|
[3862] | 28 | using HeuristicLab.Common;
|
---|
[3852] | 29 | using HeuristicLab.Core;
|
---|
[3861] | 30 | using HeuristicLab.Data;
|
---|
[11893] | 31 | using HeuristicLab.Optimization;
|
---|
[3861] | 32 | using HeuristicLab.Parameters;
|
---|
[3852] | 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
[13257] | 36 | [Item("External Evaluation Problem (multi-objective)", "A multi-objective problem that is evaluated in a different process.")]
|
---|
[13180] | 37 | [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 200)]
|
---|
[3852] | 38 | [StorableClass]
|
---|
[13336] | 39 | public class MultiObjectiveExternalEvaluationProblem : MultiObjectiveProblem<IEncoding>, IExternalEvaluationProblem {
|
---|
[4419] | 40 |
|
---|
[7201] | 41 | public static new Image StaticItemImage {
|
---|
[5287] | 42 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[3859] | 43 | }
|
---|
[4419] | 44 |
|
---|
[11899] | 45 | #region Parameters
|
---|
[11892] | 46 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
| 47 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
[3861] | 48 | }
|
---|
[6189] | 49 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
| 50 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
[3859] | 51 | }
|
---|
[11892] | 52 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
| 53 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
[3859] | 54 | }
|
---|
[13183] | 55 | public IFixedValueParameter<MultiObjectiveOptimizationSupportScript> SupportScriptParameter {
|
---|
| 56 | get { return (IFixedValueParameter<MultiObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
|
---|
| 57 | }
|
---|
[11899] | 58 | #endregion
|
---|
[3852] | 59 |
|
---|
[11899] | 60 | #region Properties
|
---|
[11892] | 61 | public EvaluationCache Cache {
|
---|
| 62 | get { return CacheParameter.Value; }
|
---|
[3860] | 63 | }
|
---|
[11892] | 64 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 65 | get { return ClientsParameter.Value; }
|
---|
[3860] | 66 | }
|
---|
[11892] | 67 | public SolutionMessageBuilder MessageBuilder {
|
---|
| 68 | get { return MessageBuilderParameter.Value; }
|
---|
[3860] | 69 | }
|
---|
[13183] | 70 | public MultiObjectiveOptimizationSupportScript OptimizationSupportScript {
|
---|
| 71 | get { return SupportScriptParameter.Value; }
|
---|
| 72 | }
|
---|
| 73 | private IMultiObjectiveOptimizationSupport OptimizationSupport {
|
---|
| 74 | get { return SupportScriptParameter.Value; }
|
---|
| 75 | }
|
---|
[11899] | 76 | #endregion
|
---|
[3860] | 77 |
|
---|
[3861] | 78 | [StorableConstructor]
|
---|
[13180] | 79 | protected MultiObjectiveExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
| 80 | protected MultiObjectiveExternalEvaluationProblem(MultiObjectiveExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
[4722] | 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13180] | 82 | return new MultiObjectiveExternalEvaluationProblem(this, cloner);
|
---|
[4722] | 83 | }
|
---|
[13180] | 84 | public MultiObjectiveExternalEvaluationProblem()
|
---|
[3852] | 85 | : base() {
|
---|
[12002] | 86 | Parameters.Remove("Maximization"); // readonly in base class
|
---|
[13180] | 87 | Parameters.Add(new FixedValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
|
---|
[11892] | 88 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[6189] | 89 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[13180] | 90 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
|
---|
[13183] | 91 | Parameters.Add(new FixedValueParameter<MultiObjectiveOptimizationSupportScript>("SupportScript", "A script that can analyze the results of the optimization.", new MultiObjectiveOptimizationSupportScript()));
|
---|
[11892] | 92 | }
|
---|
[3861] | 93 |
|
---|
[13180] | 94 | #region Multi Objective Problem Overrides
|
---|
| 95 | public override bool[] Maximization {
|
---|
| 96 | get {
|
---|
| 97 | return Parameters.ContainsKey("Maximization") ? ((IValueParameter<BoolArray>)Parameters["Maximization"]).Value.ToArray() : new bool[0];
|
---|
| 98 | }
|
---|
[3852] | 99 | }
|
---|
[11892] | 100 |
|
---|
[13180] | 101 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
| 102 | var qualityMessage = Evaluate(BuildSolutionMessage(individual));
|
---|
| 103 | if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_))
|
---|
| 104 | throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage.");
|
---|
| 105 | return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray();
|
---|
[11892] | 106 | }
|
---|
[13180] | 107 | public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
|
---|
| 108 | return Cache == null
|
---|
| 109 | ? EvaluateOnNextAvailableClient(solutionMessage)
|
---|
| 110 | : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
|
---|
| 111 | }
|
---|
[11892] | 112 |
|
---|
[13180] | 113 | public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
[13183] | 114 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
[11893] | 115 | }
|
---|
| 116 |
|
---|
[11899] | 117 | #endregion
|
---|
[11893] | 118 |
|
---|
[13180] | 119 | public virtual ExtensionRegistry GetQualityMessageExtensions() {
|
---|
| 120 | var extensions = ExtensionRegistry.CreateInstance();
|
---|
| 121 | extensions.Add(MultiObjectiveQualityMessage.QualityMessage_);
|
---|
| 122 | return extensions;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #region Evaluation
|
---|
[11899] | 126 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
| 127 | private object clientLock = new object();
|
---|
[13180] | 128 |
|
---|
[11892] | 129 | private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
|
---|
| 130 | IEvaluationServiceClient client = null;
|
---|
| 131 | lock (clientLock) {
|
---|
| 132 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
| 133 | while (client == null && Clients.CheckedItems.Any()) {
|
---|
| 134 | Monitor.Wait(clientLock);
|
---|
| 135 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
[6189] | 136 | }
|
---|
[11892] | 137 | if (client != null)
|
---|
| 138 | activeClients.Add(client);
|
---|
[6189] | 139 | }
|
---|
[11892] | 140 | try {
|
---|
| 141 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
[13180] | 142 | } finally {
|
---|
[11892] | 143 | lock (clientLock) {
|
---|
| 144 | activeClients.Remove(client);
|
---|
| 145 | Monitor.PulseAll(clientLock);
|
---|
| 146 | }
|
---|
[7999] | 147 | }
|
---|
[6189] | 148 | }
|
---|
[3861] | 149 |
|
---|
[13180] | 150 | private SolutionMessage BuildSolutionMessage(Individual individual, int solutionId = 0) {
|
---|
[11892] | 151 | lock (clientLock) {
|
---|
| 152 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
[13180] | 153 | protobufBuilder.SolutionId = solutionId;
|
---|
[11892] | 154 | var scope = new Scope();
|
---|
| 155 | individual.CopyToScope(scope);
|
---|
| 156 | foreach (var variable in scope.Variables) {
|
---|
| 157 | try {
|
---|
| 158 | MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
|
---|
[13180] | 159 | } catch (ArgumentException ex) {
|
---|
| 160 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
|
---|
[12504] | 161 | }
|
---|
[3872] | 162 | }
|
---|
[11892] | 163 | return protobufBuilder.Build();
|
---|
[3872] | 164 | }
|
---|
| 165 | }
|
---|
[11899] | 166 | #endregion
|
---|
[3852] | 167 | }
|
---|
| 168 | }
|
---|