[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]
|
---|
[13180] | 39 | public class MultiObjectiveExternalEvaluationProblem : MultiObjectiveBasicProblem<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
|
---|
[13491] | 61 | public new IEncoding Encoding {
|
---|
| 62 | get { return base.Encoding; }
|
---|
| 63 | set { base.Encoding = value; }
|
---|
| 64 | }
|
---|
[11892] | 65 | public EvaluationCache Cache {
|
---|
| 66 | get { return CacheParameter.Value; }
|
---|
[3860] | 67 | }
|
---|
[11892] | 68 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 69 | get { return ClientsParameter.Value; }
|
---|
[3860] | 70 | }
|
---|
[11892] | 71 | public SolutionMessageBuilder MessageBuilder {
|
---|
| 72 | get { return MessageBuilderParameter.Value; }
|
---|
[3860] | 73 | }
|
---|
[13183] | 74 | public MultiObjectiveOptimizationSupportScript OptimizationSupportScript {
|
---|
| 75 | get { return SupportScriptParameter.Value; }
|
---|
| 76 | }
|
---|
| 77 | private IMultiObjectiveOptimizationSupport OptimizationSupport {
|
---|
| 78 | get { return SupportScriptParameter.Value; }
|
---|
| 79 | }
|
---|
[11899] | 80 | #endregion
|
---|
[3860] | 81 |
|
---|
[3861] | 82 | [StorableConstructor]
|
---|
[13180] | 83 | protected MultiObjectiveExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
| 84 | protected MultiObjectiveExternalEvaluationProblem(MultiObjectiveExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
[4722] | 85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13180] | 86 | return new MultiObjectiveExternalEvaluationProblem(this, cloner);
|
---|
[4722] | 87 | }
|
---|
[13180] | 88 | public MultiObjectiveExternalEvaluationProblem()
|
---|
[3852] | 89 | : base() {
|
---|
[12002] | 90 | Parameters.Remove("Maximization"); // readonly in base class
|
---|
[13180] | 91 | Parameters.Add(new FixedValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
|
---|
[11892] | 92 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[6189] | 93 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[13180] | 94 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
|
---|
[13183] | 95 | Parameters.Add(new FixedValueParameter<MultiObjectiveOptimizationSupportScript>("SupportScript", "A script that can analyze the results of the optimization.", new MultiObjectiveOptimizationSupportScript()));
|
---|
[11892] | 96 | }
|
---|
[3861] | 97 |
|
---|
[13180] | 98 | #region Multi Objective Problem Overrides
|
---|
| 99 | public override bool[] Maximization {
|
---|
| 100 | get {
|
---|
| 101 | return Parameters.ContainsKey("Maximization") ? ((IValueParameter<BoolArray>)Parameters["Maximization"]).Value.ToArray() : new bool[0];
|
---|
| 102 | }
|
---|
[3852] | 103 | }
|
---|
[11892] | 104 |
|
---|
[13180] | 105 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
| 106 | var qualityMessage = Evaluate(BuildSolutionMessage(individual));
|
---|
| 107 | if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_))
|
---|
| 108 | throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage.");
|
---|
| 109 | return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray();
|
---|
[11892] | 110 | }
|
---|
[13180] | 111 | public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
|
---|
| 112 | return Cache == null
|
---|
| 113 | ? EvaluateOnNextAvailableClient(solutionMessage)
|
---|
| 114 | : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
|
---|
| 115 | }
|
---|
[11892] | 116 |
|
---|
[13180] | 117 | public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
[13183] | 118 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
[11893] | 119 | }
|
---|
| 120 |
|
---|
[11899] | 121 | #endregion
|
---|
[11893] | 122 |
|
---|
[13180] | 123 | public virtual ExtensionRegistry GetQualityMessageExtensions() {
|
---|
| 124 | var extensions = ExtensionRegistry.CreateInstance();
|
---|
| 125 | extensions.Add(MultiObjectiveQualityMessage.QualityMessage_);
|
---|
| 126 | return extensions;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | #region Evaluation
|
---|
[11899] | 130 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
| 131 | private object clientLock = new object();
|
---|
[13180] | 132 |
|
---|
[11892] | 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));
|
---|
[6189] | 140 | }
|
---|
[11892] | 141 | if (client != null)
|
---|
| 142 | activeClients.Add(client);
|
---|
[6189] | 143 | }
|
---|
[11892] | 144 | try {
|
---|
| 145 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
[13180] | 146 | } finally {
|
---|
[11892] | 147 | lock (clientLock) {
|
---|
| 148 | activeClients.Remove(client);
|
---|
| 149 | Monitor.PulseAll(clientLock);
|
---|
| 150 | }
|
---|
[7999] | 151 | }
|
---|
[6189] | 152 | }
|
---|
[3861] | 153 |
|
---|
[13180] | 154 | private SolutionMessage BuildSolutionMessage(Individual individual, int solutionId = 0) {
|
---|
[11892] | 155 | lock (clientLock) {
|
---|
| 156 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
[13180] | 157 | protobufBuilder.SolutionId = solutionId;
|
---|
[11892] | 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);
|
---|
[13180] | 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);
|
---|
[12504] | 165 | }
|
---|
[3872] | 166 | }
|
---|
[11892] | 167 | return protobufBuilder.Build();
|
---|
[3872] | 168 | }
|
---|
| 169 | }
|
---|
[11899] | 170 | #endregion
|
---|
[3852] | 171 | }
|
---|
| 172 | }
|
---|