1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Collections.Generic;
|
---|
23 | using System.Diagnostics.CodeAnalysis;
|
---|
24 | using System.Linq;
|
---|
25 | using Google.ProtocolBuffers;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.ExternalEvaluation.RepetitionsExtension;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.ExternalEvaluation.MyExtension {
|
---|
35 |
|
---|
36 | [Item("ExtendedExternalEvaluationProblem", "Creates a solution message, and communicates it via the driver to receive a quality message.")]
|
---|
37 | [StorableClass]
|
---|
38 | public class ExtendedExternalEvaluationProblem : ExternalEvaluationProblem, IStatefulItem {
|
---|
39 |
|
---|
40 | private IDictionary<SolutionMessage, QualityMessage> messages;
|
---|
41 |
|
---|
42 | [Storable(Name = "Messages")]
|
---|
43 | [SuppressMessage("ReSharper", "InconsistentlySynchronizedField")]
|
---|
44 | [SuppressMessage("ReSharper", "InconsistentNaming")]
|
---|
45 | private IEnumerable<KeyValuePair<byte[], byte[]>> Messages_Persistence {
|
---|
46 | get {
|
---|
47 | return messages.ToDictionary(
|
---|
48 | kvp => kvp.Key.ToByteArray(),
|
---|
49 | kvp => kvp.Value.ToByteArray());
|
---|
50 | }
|
---|
51 | set {
|
---|
52 | messages = value.ToDictionary(
|
---|
53 | kvp => SolutionMessage.ParseFrom(ByteString.CopyFrom(kvp.Key)),
|
---|
54 | kvp => QualityMessage.ParseFrom(ByteString.CopyFrom(kvp.Value), GetQualityMessageExtensions()));
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | #region Construction & Cloning
|
---|
59 | [StorableConstructor]
|
---|
60 | private ExtendedExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
61 |
|
---|
62 | private ExtendedExternalEvaluationProblem(ExtendedExternalEvaluationProblem original, Cloner cloner)
|
---|
63 | : base(original, cloner) {
|
---|
64 | Messages_Persistence = original.Messages_Persistence;
|
---|
65 | }
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new ExtendedExternalEvaluationProblem(this, cloner);
|
---|
68 | }
|
---|
69 | public ExtendedExternalEvaluationProblem()
|
---|
70 | : base() {
|
---|
71 | messages = new Dictionary<SolutionMessage, QualityMessage>();
|
---|
72 |
|
---|
73 | Encoding = new RealVectorEncoding("r", length: 40, min: 0, max: 10);
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | public override QualityMessage Evaluate(SolutionMessage solutionMessage) {
|
---|
78 | var qualityMessage = base.Evaluate(solutionMessage);
|
---|
79 | lock (messages) {
|
---|
80 | messages.Add(solutionMessage, qualityMessage);
|
---|
81 | }
|
---|
82 | return qualityMessage;
|
---|
83 | }
|
---|
84 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
85 | base.Analyze(individuals, qualities, results, random);
|
---|
86 |
|
---|
87 | lock (messages) {
|
---|
88 | ExtendedAnalyze(messages.Keys.ToArray(), messages.Values.ToArray(), results, Encoding, random);
|
---|
89 | messages.Clear();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public virtual void ExtendedAnalyze(SolutionMessage[] solutionMessages, QualityMessage[] qualityMessages, ResultCollection results, IEncoding encoding, IRandom random) {
|
---|
94 | if (!results.ContainsKey("SolutionsHistory")) {
|
---|
95 | var dataTable = new DataTable("SolutionsHistory");
|
---|
96 | int solutionLength = ((RealVectorEncoding)encoding).Length;
|
---|
97 | for (int i = 0; i < solutionLength; i++)
|
---|
98 | dataTable.Rows.Add(new DataRow("a" + i, "Parameter " + i));
|
---|
99 | dataTable.Rows.Add(new DataRow("Quality"));
|
---|
100 | dataTable.Rows.Add(new DataRow("Repetitions"));
|
---|
101 | dataTable.Rows.Add(new DataRow("Variance"));
|
---|
102 | dataTable.Rows.Add(new DataRow("CanceledRuns"));
|
---|
103 | results.Add(new Result("SolutionsHistory", dataTable));
|
---|
104 | }
|
---|
105 |
|
---|
106 | var solutions =
|
---|
107 | from result in solutionMessages.Zip(qualityMessages, (sm, qm) => new { sm, qm })
|
---|
108 | select new {
|
---|
109 | inputs = result.sm.GetDoubleArrayVars(0).DataList,
|
---|
110 | quality = result.qm.GetExtension(SingleObjectiveQualityMessage.QualityMessage_).Quality,
|
---|
111 | repititions = GetExtension(result.qm, RepetitionsResponse.Repetitions, 1),
|
---|
112 | variance = GetExtension(result.qm, RepetitionsResponse.Variance, 0),
|
---|
113 | canceledRuns = GetExtension(result.qm, RepetitionsResponse.NumFailed, 0)
|
---|
114 | };
|
---|
115 |
|
---|
116 | var solutionsHistory = (DataTable)results["SolutionsHistory"].Value;
|
---|
117 |
|
---|
118 | foreach (var solution in solutions.OrderBy(s => s.quality)) {
|
---|
119 | for (int i = 0; i < solution.inputs.Count; i++)
|
---|
120 | solutionsHistory.Rows["a" + i].Values.Add(solution.inputs[i]);
|
---|
121 | solutionsHistory.Rows["Quality"].Values.Add(solution.quality);
|
---|
122 | solutionsHistory.Rows["Repetitions"].Values.Add(solution.repititions);
|
---|
123 | solutionsHistory.Rows["Variance"].Values.Add(solution.variance);
|
---|
124 | solutionsHistory.Rows["CanceledRuns"].Values.Add(solution.canceledRuns);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | private T GetExtension<T>(QualityMessage msg, GeneratedExtensionBase<T> extension, T @default = default(T)) {
|
---|
128 | return msg.HasExtension(extension) ? msg.GetExtension(extension) : @default;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override ExtensionRegistry GetQualityMessageExtensions() {
|
---|
132 | var extensions = base.GetQualityMessageExtensions();
|
---|
133 | RepetitionsQualityMessage.RegisterAllExtensions(extensions);
|
---|
134 | return extensions;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void InitializeState() {
|
---|
138 | }
|
---|
139 | public void ClearState() {
|
---|
140 | lock (messages)
|
---|
141 | messages.Clear();
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|