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