1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
36 | [Item("External Evaluation Problem (multi-objective)", "A multi-objective problem that is evaluated in a different process.")]
|
---|
37 | [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 200)]
|
---|
38 | [StorableType("CCA50199-A6AB-4C84-B4FA-0262CAF416EC")]
|
---|
39 | public class MultiObjectiveExternalEvaluationProblem<TEncoding, TEncodedSolution> : MultiObjectiveProblem<TEncoding, TEncodedSolution>, IExternalEvaluationProblem
|
---|
40 | where TEncoding : class, IEncoding
|
---|
41 | where TEncodedSolution : class, IEncodedSolution {
|
---|
42 |
|
---|
43 | public static new Image StaticItemImage {
|
---|
44 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | #region Parameters
|
---|
48 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
49 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
50 | }
|
---|
51 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
52 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
|
---|
55 | get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
|
---|
56 | }
|
---|
57 | public IFixedValueParameter<MultiObjectiveOptimizationSupportScript<TEncodedSolution>> SupportScriptParameter {
|
---|
58 | get { return (IFixedValueParameter<MultiObjectiveOptimizationSupportScript<TEncodedSolution>>)Parameters["SupportScript"]; }
|
---|
59 | }
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | #region Properties
|
---|
63 | public new TEncoding Encoding {
|
---|
64 | get { return base.Encoding; }
|
---|
65 | set { base.Encoding = value; }
|
---|
66 | }
|
---|
67 | public EvaluationCache Cache {
|
---|
68 | get { return CacheParameter.Value; }
|
---|
69 | }
|
---|
70 | public CheckedItemCollection<IEvaluationServiceClient> Clients {
|
---|
71 | get { return ClientsParameter.Value; }
|
---|
72 | }
|
---|
73 | public SolutionMessageBuilder MessageBuilder {
|
---|
74 | get { return MessageBuilderParameter.Value; }
|
---|
75 | }
|
---|
76 | public MultiObjectiveOptimizationSupportScript<TEncodedSolution> OptimizationSupportScript {
|
---|
77 | get { return SupportScriptParameter.Value; }
|
---|
78 | }
|
---|
79 | private IMultiObjectiveOptimizationSupport<TEncodedSolution> OptimizationSupport {
|
---|
80 | get { return SupportScriptParameter.Value; }
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | protected MultiObjectiveExternalEvaluationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
86 | protected MultiObjectiveExternalEvaluationProblem(MultiObjectiveExternalEvaluationProblem<TEncoding, TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
|
---|
87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
88 | return new MultiObjectiveExternalEvaluationProblem<TEncoding, TEncodedSolution>(this, cloner);
|
---|
89 | }
|
---|
90 | public MultiObjectiveExternalEvaluationProblem(TEncoding encoding)
|
---|
91 | : base(encoding) {
|
---|
92 | MaximizationParameter.ReadOnly = false;
|
---|
93 | MaximizationParameter.Value = new BoolArray();
|
---|
94 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
95 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
96 | Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()) { Hidden = true });
|
---|
97 | Parameters.Add(new FixedValueParameter<MultiObjectiveOptimizationSupportScript<TEncodedSolution>>("SupportScript", "A script that can analyze the results of the optimization.", new MultiObjectiveOptimizationSupportScript<TEncodedSolution>()));
|
---|
98 | }
|
---|
99 |
|
---|
100 | #region Multi Objective Problem Overrides
|
---|
101 | public virtual void SetMaximization(bool[] maximization) {
|
---|
102 | ((IStringConvertibleArray)MaximizationParameter.Value).Length = maximization.Length;
|
---|
103 | var array = MaximizationParameter.Value;
|
---|
104 | for (var i = 0; i < maximization.Length; i++)
|
---|
105 | array[i] = maximization[i];
|
---|
106 | }
|
---|
107 |
|
---|
108 | public override double[] Evaluate(TEncodedSolution individual, IRandom random, CancellationToken cancellationToken) {
|
---|
109 | var qualityMessage = Evaluate(BuildSolutionMessage(individual), cancellationToken);
|
---|
110 | if (!qualityMessage.HasExtension(MultiObjectiveQualityMessage.QualityMessage_))
|
---|
111 | throw new InvalidOperationException("The received message is not a MultiObjectiveQualityMessage.");
|
---|
112 | return qualityMessage.GetExtension(MultiObjectiveQualityMessage.QualityMessage_).QualitiesList.ToArray();
|
---|
113 | }
|
---|
114 | public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) {
|
---|
115 | return Cache == null
|
---|
116 | ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken)
|
---|
117 | : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public override void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) {
|
---|
121 | OptimizationSupport.Analyze(individuals, qualities, results, random);
|
---|
122 | }
|
---|
123 |
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | public virtual ExtensionRegistry GetQualityMessageExtensions() {
|
---|
127 | var extensions = ExtensionRegistry.CreateInstance();
|
---|
128 | extensions.Add(MultiObjectiveQualityMessage.QualityMessage_);
|
---|
129 | return extensions;
|
---|
130 | }
|
---|
131 |
|
---|
132 | #region Evaluation
|
---|
133 | private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
|
---|
134 | private readonly object clientLock = new object();
|
---|
135 |
|
---|
136 | private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message, CancellationToken cancellationToken) {
|
---|
137 | IEvaluationServiceClient client = null;
|
---|
138 | lock (clientLock) {
|
---|
139 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
140 | while (client == null && Clients.CheckedItems.Any()) {
|
---|
141 | Monitor.Wait(clientLock);
|
---|
142 | client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
|
---|
143 | }
|
---|
144 | if (client != null)
|
---|
145 | activeClients.Add(client);
|
---|
146 | }
|
---|
147 | try {
|
---|
148 | return client.Evaluate(message, GetQualityMessageExtensions());
|
---|
149 | } finally {
|
---|
150 | lock (clientLock) {
|
---|
151 | activeClients.Remove(client);
|
---|
152 | Monitor.PulseAll(clientLock);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private SolutionMessage BuildSolutionMessage(TEncodedSolution solution, int solutionId = 0) {
|
---|
158 | lock (clientLock) {
|
---|
159 | SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
|
---|
160 | protobufBuilder.SolutionId = solutionId;
|
---|
161 | var scope = new Scope();
|
---|
162 | ScopeUtil.CopyEncodedSolutionToScope(scope, Encoding, solution);
|
---|
163 | foreach (var variable in scope.Variables) {
|
---|
164 | try {
|
---|
165 | MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
|
---|
166 | } catch (ArgumentException ex) {
|
---|
167 | throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", Name), ex);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | return protobufBuilder.Build();
|
---|
171 | }
|
---|
172 | }
|
---|
173 | #endregion
|
---|
174 | }
|
---|
175 | }
|
---|