[3852] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 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;
|
---|
[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;
|
---|
[3852] | 34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
| 37 | [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
|
---|
[12708] | 38 | [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 100)]
|
---|
[3852] | 39 | [StorableClass]
|
---|
[11892] | 40 | public sealed class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding> {
|
---|
[4419] | 41 |
|
---|
[7201] | 42 | public static new Image StaticItemImage {
|
---|
[5287] | 43 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[3859] | 44 | }
|
---|
[4419] | 45 |
|
---|
[11899] | 46 | #region Parameters
|
---|
[11892] | 47 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
| 48 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
[3861] | 49 | }
|
---|
[6189] | 50 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
| 51 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
[3859] | 52 | }
|
---|
[11892] | 53 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
| 54 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
[3859] | 55 | }
|
---|
[11893] | 56 | public IFixedValueParameter<SingleObjectiveOptimizationSupportScript> SupportScriptParameter {
|
---|
| 57 | get { return (IFixedValueParameter<SingleObjectiveOptimizationSupportScript>)Parameters["SupportScript"]; }
|
---|
| 58 | }
|
---|
[11899] | 59 | #endregion
|
---|
[3852] | 60 |
|
---|
[11899] | 61 | #region Properties
|
---|
[11892] | 62 | public EvaluationCache Cache {
|
---|
| 63 | get { return CacheParameter.Value; }
|
---|
[3860] | 64 | }
|
---|
[11892] | 65 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
| 66 | get { return ClientsParameter.Value; }
|
---|
[3860] | 67 | }
|
---|
[11892] | 68 | public SolutionMessageBuilder MessageBuilder {
|
---|
| 69 | get { return MessageBuilderParameter.Value; }
|
---|
[3860] | 70 | }
|
---|
[11893] | 71 | public SingleObjectiveOptimizationSupportScript OptimizationSupportScript {
|
---|
| 72 | get { return SupportScriptParameter.Value; }
|
---|
| 73 | }
|
---|
| 74 | private ISingleObjectiveOptimizationSupport OptimizationSupport {
|
---|
| 75 | get { return SupportScriptParameter.Value; }
|
---|
| 76 | }
|
---|
[11899] | 77 | #endregion
|
---|
[3860] | 78 |
|
---|
[3861] | 79 | [StorableConstructor]
|
---|
[3864] | 80 | private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
[11900] | 81 | private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
[4722] | 82 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 83 | return new ExternalEvaluationProblem(this, cloner);
|
---|
| 84 | }
|
---|
[3852] | 85 | public ExternalEvaluationProblem()
|
---|
| 86 | : base() {
|
---|
[12005] | 87 | Parameters.Remove("Maximization"); // readonly in base class
|
---|
| 88 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue()));
|
---|
[11892] | 89 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
[6189] | 90 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
[11892] | 91 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
|
---|
[11893] | 92 | Parameters.Add(new FixedValueParameter<SingleObjectiveOptimizationSupportScript>("SupportScript", "A script that can provide neighborhood and analyze the results of the optimization.", new SingleObjectiveOptimizationSupportScript()));
|
---|
[11961] | 93 |
|
---|
| 94 | Operators.Add(new BestScopeSolutionAnalyzer());
|
---|
[11892] | 95 | }
|
---|
[3861] | 96 |
|
---|
[11899] | 97 | #region Single Objective Problem Overrides
|
---|
[11892] | 98 | public override bool Maximization {
|
---|
| 99 | get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; }
|
---|
[3852] | 100 | }
|
---|
[11892] | 101 |
|
---|
| 102 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
| 103 | return Cache == null ? EvaluateOnNextAvailableClient(BuildSolutionMessage(individual)).Quality
|
---|
| 104 | : Cache.GetValue(BuildSolutionMessage(individual), m => EvaluateOnNextAvailableClient(m).Quality);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[11893] | 107 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 108 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
|
---|
| 112 | return OptimizationSupport.GetNeighbors(individual, random);
|
---|
| 113 | }
|
---|
[11899] | 114 | #endregion
|
---|
[11893] | 115 |
|
---|
[11899] | 116 | #region Evaluation Helpers
|
---|
| 117 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
| 118 | private object clientLock = new object();
|
---|
[11892] | 119 | private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
|
---|
| 120 | IEvaluationServiceClient client = null;
|
---|
| 121 | lock (clientLock) {
|
---|
| 122 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
| 123 | while (client == null && Clients.CheckedItems.Any()) {
|
---|
| 124 | Monitor.Wait(clientLock);
|
---|
| 125 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
[6189] | 126 | }
|
---|
[11892] | 127 | if (client != null)
|
---|
| 128 | activeClients.Add(client);
|
---|
[6189] | 129 | }
|
---|
[11892] | 130 | try {
|
---|
| 131 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
[12708] | 132 | }
|
---|
| 133 | finally {
|
---|
[11892] | 134 | lock (clientLock) {
|
---|
| 135 | activeClients.Remove(client);
|
---|
| 136 | Monitor.PulseAll(clientLock);
|
---|
| 137 | }
|
---|
[7999] | 138 | }
|
---|
[6189] | 139 | }
|
---|
[3861] | 140 |
|
---|
[11892] | 141 | private ExtensionRegistry GetQualityMessageExtensions() {
|
---|
| 142 | return ExtensionRegistry.CreateInstance();
|
---|
[3861] | 143 | }
|
---|
| 144 |
|
---|
[11892] | 145 | private SolutionMessage BuildSolutionMessage(Individual individual) {
|
---|
| 146 | lock (clientLock) {
|
---|
| 147 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
| 148 | protobufBuilder.SolutionId = 0;
|
---|
| 149 | var scope = new Scope();
|
---|
| 150 | individual.CopyToScope(scope);
|
---|
| 151 | foreach (var variable in scope.Variables) {
|
---|
| 152 | try {
|
---|
| 153 | MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
|
---|
[12708] | 154 | }
|
---|
| 155 | catch (ArgumentException ex) {
|
---|
[11892] | 156 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex);
|
---|
| 157 | }
|
---|
[3872] | 158 | }
|
---|
[11892] | 159 | return protobufBuilder.Build();
|
---|
[3872] | 160 | }
|
---|
| 161 | }
|
---|
[11899] | 162 | #endregion
|
---|
[3852] | 163 | }
|
---|
| 164 | }
|
---|