[3852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) 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;
|
---|
[16565] | 33 | using HEAL.Attic;
|
---|
[3852] | 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)]
|
---|
[16565] | 38 | [StorableType("CCA50199-A6AB-4C84-B4FA-0262CAF416EC")]
|
---|
[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 | }
|
---|
[13500] | 58 |
|
---|
| 59 | private IFixedValueParameter<BoolArray> MaximizationParameter {
|
---|
| 60 | get { return (IFixedValueParameter<BoolArray>)Parameters["Maximization"]; }
|
---|
| 61 | }
|
---|
[11899] | 62 | #endregion
|
---|
[3852] | 63 |
|
---|
[11899] | 64 | #region Properties
|
---|
[13491] | 65 | public new IEncoding Encoding {
|
---|
| 66 | get { return base.Encoding; }
|
---|
| 67 | set { base.Encoding = value; }
|
---|
| 68 | }
|
---|
[11892] | 69 | public EvaluationCache Cache {
|
---|
| 70 | get { return CacheParameter.Value; }
|
---|
[3860] | 71 | }
|
---|
[11892] | 72 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 73 | get { return ClientsParameter.Value; }
|
---|
[3860] | 74 | }
|
---|
[11892] | 75 | public SolutionMessageBuilder MessageBuilder {
|
---|
| 76 | get { return MessageBuilderParameter.Value; }
|
---|
[3860] | 77 | }
|
---|
[13183] | 78 | public MultiObjectiveOptimizationSupportScript OptimizationSupportScript {
|
---|
| 79 | get { return SupportScriptParameter.Value; }
|
---|
| 80 | }
|
---|
| 81 | private IMultiObjectiveOptimizationSupport OptimizationSupport {
|
---|
| 82 | get { return SupportScriptParameter.Value; }
|
---|
| 83 | }
|
---|
[11899] | 84 | #endregion
|
---|
[3860] | 85 |
|
---|
[3861] | 86 | [StorableConstructor]
|
---|
[16565] | 87 | protected MultiObjectiveExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[13180] | 88 | protected MultiObjectiveExternalEvaluationProblem(MultiObjectiveExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
[4722] | 89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13180] | 90 | return new MultiObjectiveExternalEvaluationProblem(this, cloner);
|
---|
[4722] | 91 | }
|
---|
[13180] | 92 | public MultiObjectiveExternalEvaluationProblem()
|
---|
[3852] | 93 | : base() {
|
---|
[12002] | 94 | Parameters.Remove("Maximization"); // readonly in base class
|
---|
[13180] | 95 | Parameters.Add(new FixedValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", new BoolArray()));
|
---|
[11892] | 96 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[6189] | 97 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[13180] | 98 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
|
---|
[13183] | 99 | Parameters.Add(new FixedValueParameter<MultiObjectiveOptimizationSupportScript>("SupportScript", "A script that can analyze the results of the optimization.", new MultiObjectiveOptimizationSupportScript()));
|
---|
[11892] | 100 | }
|
---|
[3861] | 101 |
|
---|
[13180] | 102 | #region Multi Objective Problem Overrides
|
---|
| 103 | public override bool[] Maximization {
|
---|
| 104 | get {
|
---|
| 105 | return Parameters.ContainsKey("Maximization") ? ((IValueParameter<BoolArray>)Parameters["Maximization"]).Value.ToArray() : new bool[0];
|
---|
| 106 | }
|
---|
[3852] | 107 | }
|
---|
[11892] | 108 |
|
---|
[13500] | 109 | public virtual void SetMaximization(bool[] maximization) {
|
---|
| 110 | ((IStringConvertibleArray)MaximizationParameter.Value).Length = maximization.Length;
|
---|
| 111 | var array = MaximizationParameter.Value;
|
---|
| 112 | for (var i = 0; i < maximization.Length; i++)
|
---|
| 113 | array[i] = maximization[i];
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[13180] | 116 | public override double[] Evaluate(Individual individual, IRandom random) {
|
---|
| 117 | var qualityMessage = Evaluate(BuildSolutionMessage(individual));
|
---|
| 118 | if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_))
|
---|
| 119 | throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage.");
|
---|
| 120 | return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray();
|
---|
[11892] | 121 | }
|
---|
[13180] | 122 | public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
|
---|
| 123 | return Cache == null
|
---|
| 124 | ? EvaluateOnNextAvailableClient(solutionMessage)
|
---|
| 125 | : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
|
---|
| 126 | }
|
---|
[11892] | 127 |
|
---|
[13180] | 128 | public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
[13183] | 129 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
[11893] | 130 | }
|
---|
| 131 |
|
---|
[11899] | 132 | #endregion
|
---|
[11893] | 133 |
|
---|
[13180] | 134 | public virtual ExtensionRegistry GetQualityMessageExtensions() {
|
---|
| 135 | var extensions = ExtensionRegistry.CreateInstance();
|
---|
| 136 | extensions.Add(MultiObjectiveQualityMessage.QualityMessage_);
|
---|
| 137 | return extensions;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | #region Evaluation
|
---|
[11899] | 141 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
| 142 | private object clientLock = new object();
|
---|
[13180] | 143 |
|
---|
[11892] | 144 | private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
|
---|
| 145 | IEvaluationServiceClient client = null;
|
---|
| 146 | lock (clientLock) {
|
---|
| 147 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
| 148 | while (client == null && Clients.CheckedItems.Any()) {
|
---|
| 149 | Monitor.Wait(clientLock);
|
---|
| 150 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
[6189] | 151 | }
|
---|
[11892] | 152 | if (client != null)
|
---|
| 153 | activeClients.Add(client);
|
---|
[6189] | 154 | }
|
---|
[11892] | 155 | try {
|
---|
| 156 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
[14877] | 157 | }
|
---|
| 158 | finally {
|
---|
[11892] | 159 | lock (clientLock) {
|
---|
| 160 | activeClients.Remove(client);
|
---|
| 161 | Monitor.PulseAll(clientLock);
|
---|
| 162 | }
|
---|
[7999] | 163 | }
|
---|
[6189] | 164 | }
|
---|
[3861] | 165 |
|
---|
[13180] | 166 | private SolutionMessage BuildSolutionMessage(Individual individual, int solutionId = 0) {
|
---|
[11892] | 167 | lock (clientLock) {
|
---|
| 168 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
[13180] | 169 | protobufBuilder.SolutionId = solutionId;
|
---|
[14877] | 170 | foreach (var variable in individual.Values) {
|
---|
[11892] | 171 | try {
|
---|
[14877] | 172 | MessageBuilder.AddToMessage(variable.Value, variable.Key, protobufBuilder);
|
---|
| 173 | }
|
---|
| 174 | catch (ArgumentException ex) {
|
---|
[13180] | 175 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
|
---|
[12504] | 176 | }
|
---|
[3872] | 177 | }
|
---|
[11892] | 178 | return protobufBuilder.Build();
|
---|
[3872] | 179 | }
|
---|
| 180 | }
|
---|
[11899] | 181 | #endregion
|
---|
[3852] | 182 | }
|
---|
| 183 | }
|
---|