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