[3852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 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;
|
---|
[11961] | 28 | using HeuristicLab.Analysis;
|
---|
[3862] | 29 | using HeuristicLab.Common;
|
---|
[3852] | 30 | using HeuristicLab.Core;
|
---|
[3861] | 31 | using HeuristicLab.Data;
|
---|
[11893] | 32 | using HeuristicLab.Optimization;
|
---|
[3861] | 33 | using HeuristicLab.Parameters;
|
---|
[17097] | 34 | using HEAL.Attic;
|
---|
[3852] | 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
[13259] | 37 | [Item("External Evaluation Problem (single-objective)", "A problem that is evaluated in a different process.")]
|
---|
[12708] | 38 | [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 100)]
|
---|
[17097] | 39 | [StorableType("115EB3A5-A8A8-4A2E-9799-9485FE896DEC")]
|
---|
[13259] | 40 | // BackwardsCompatibility3.3
|
---|
| 41 | // Rename class to SingleObjectiveExternalEvaluationProblem
|
---|
| 42 | public class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding>, IExternalEvaluationProblem {
|
---|
[4419] | 43 |
|
---|
[7201] | 44 | public static new Image StaticItemImage {
|
---|
[5287] | 45 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[3859] | 46 | }
|
---|
[4419] | 47 |
|
---|
[11899] | 48 | #region Parameters
|
---|
[11892] | 49 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
| 50 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
[3861] | 51 | }
|
---|
[6189] | 52 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
| 53 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
[3859] | 54 | }
|
---|
[11892] | 55 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
| 56 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
[3859] | 57 | }
|
---|
[11893] | 58 | public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter {
|
---|
| 59 | get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
|
---|
| 60 | }
|
---|
[13631] | 61 |
|
---|
| 62 | private IFixedValueParameter<BoolValue> MaximizationParameter {
|
---|
| 63 | get { return (IFixedValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 64 | }
|
---|
[11899] | 65 | #endregion
|
---|
[3852] | 66 |
|
---|
[11899] | 67 | #region Properties
|
---|
[13631] | 68 | public new IEncoding Encoding {
|
---|
| 69 | get { return base.Encoding; }
|
---|
| 70 | set { base.Encoding = value; }
|
---|
| 71 | }
|
---|
[11892] | 72 | public EvaluationCache Cache {
|
---|
| 73 | get { return CacheParameter.Value; }
|
---|
[3860] | 74 | }
|
---|
[11892] | 75 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 76 | get { return ClientsParameter.Value; }
|
---|
[3860] | 77 | }
|
---|
[11892] | 78 | public SolutionMessageBuilder MessageBuilder {
|
---|
| 79 | get { return MessageBuilderParameter.Value; }
|
---|
[3860] | 80 | }
|
---|
[11893] | 81 | public SingleObjectiveOptimizationSupportScript OptimizationSupportScript {
|
---|
| 82 | get { return SupportScriptParameter.Value; }
|
---|
| 83 | }
|
---|
| 84 | private ISingleObjectiveOptimizationSupport OptimizationSupport {
|
---|
| 85 | get { return SupportScriptParameter.Value; }
|
---|
| 86 | }
|
---|
[11899] | 87 | #endregion
|
---|
[3860] | 88 |
|
---|
[3861] | 89 | [StorableConstructor]
|
---|
[17097] | 90 | protected ExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[13197] | 91 | protected ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
[4722] | 92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 93 | return new ExternalEvaluationProblem(this, cloner);
|
---|
| 94 | }
|
---|
[3852] | 95 | public ExternalEvaluationProblem()
|
---|
| 96 | : base() {
|
---|
[12005] | 97 | Parameters.Remove("Maximization"); // readonly in base class
|
---|
| 98 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
|
---|
[11892] | 99 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[6189] | 100 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[13259] | 101 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
|
---|
[11893] | 102 | Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript()));
|
---|
[11961] | 103 |
|
---|
| 104 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
[11892] | 105 | }
|
---|
[3861] | 106 |
|
---|
[11899] | 107 | #region Single Objective Problem Overrides
|
---|
[11892] | 108 | public override bool Maximization {
|
---|
| 109 | get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; }
|
---|
[3852] | 110 | }
|
---|
[11892] | 111 |
|
---|
[13631] | 112 | public virtual void SetMaximization(bool maximization) {
|
---|
| 113 | MaximizationParameter.Value.Value = maximization;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[11892] | 116 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
[13259] | 117 | var qualityMessage = Evaluate(BuildSolutionMessage(individual));
|
---|
| 118 | if (!qualityMessage.HasExtension(SingleObjectiveQualityMessage.QualityMessage_))
|
---|
| 119 | throw new InvalidOperationException("The received message is not a SingleObjectiveQualityMessage.");
|
---|
| 120 | return qualityMessage.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality;
|
---|
[11892] | 121 | }
|
---|
[13259] | 122 | public virtual QualityMessage Evaluate(SolutionMessage solutionMessage) {
|
---|
| 123 | return Cache == null
|
---|
| 124 | ? EvaluateOnNextAvailableClient(solutionMessage)
|
---|
| 125 | : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions());
|
---|
| 126 | }
|
---|
[11892] | 127 |
|
---|
[11893] | 128 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 129 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 133 | return OptimizationSupport.GetNeighbors(individual, random);
|
---|
| 134 | }
|
---|
[11899] | 135 | #endregion
|
---|
[11893] | 136 |
|
---|
[13259] | 137 | public virtual ExtensionRegistry GetQualityMessageExtensions() {
|
---|
| 138 | var extensions = ExtensionRegistry.CreateInstance();
|
---|
| 139 | extensions.Add(SingleObjectiveQualityMessage.QualityMessage_);
|
---|
| 140 | return extensions;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | #region Evaluation
|
---|
[11899] | 144 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
| 145 | private object clientLock = new object();
|
---|
[13259] | 146 |
|
---|
[11892] | 147 | private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
|
---|
| 148 | IEvaluationServiceClient client = null;
|
---|
| 149 | lock (clientLock) {
|
---|
| 150 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
| 151 | while (client == null && Clients.CheckedItems.Any()) {
|
---|
| 152 | Monitor.Wait(clientLock);
|
---|
| 153 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
[6189] | 154 | }
|
---|
[11892] | 155 | if (client != null)
|
---|
| 156 | activeClients.Add(client);
|
---|
[6189] | 157 | }
|
---|
[11892] | 158 | try {
|
---|
| 159 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
[15116] | 160 | }
|
---|
| 161 | finally {
|
---|
[11892] | 162 | lock (clientLock) {
|
---|
| 163 | activeClients.Remove(client);
|
---|
| 164 | Monitor.PulseAll(clientLock);
|
---|
| 165 | }
|
---|
[7999] | 166 | }
|
---|
[6189] | 167 | }
|
---|
[3861] | 168 |
|
---|
[13259] | 169 | private SolutionMessage BuildSolutionMessage(Individual individual, int solutionId = 0) {
|
---|
[11892] | 170 | lock (clientLock) {
|
---|
| 171 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
[13259] | 172 | protobufBuilder.SolutionId = solutionId;
|
---|
[15116] | 173 | foreach (var variable in individual.Values) {
|
---|
[11892] | 174 | try {
|
---|
[15116] | 175 | MessageBuilder.AddToMessage(variable.Value, variable.Key, protobufBuilder);
|
---|
| 176 | }
|
---|
| 177 | catch (ArgumentException ex) {
|
---|
[13259] | 178 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
|
---|
[12708] | 179 | }
|
---|
[3872] | 180 | }
|
---|
[11892] | 181 | return protobufBuilder.Build();
|
---|
[3872] | 182 | }
|
---|
| 183 | }
|
---|
[11899] | 184 | #endregion
|
---|
[3852] | 185 | }
|
---|
| 186 | }
|
---|