1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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.Linq;
|
---|
23 | using System.Threading;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Core.Networks;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Networks.IntegratedOptimization.MachineLearning {
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class FeatureSelectionOrchestrator : OrchestratorNode {
|
---|
37 | private const string REGRESSION_ORCHESTRATION_PORT_NAME = "Regression algorithm orchestration port";
|
---|
38 | private const string FEATURE_SELECTION_EVALUATION_PORT_NAME = "Feature selection evaluation port";
|
---|
39 | private const string REGRESSION_PROBLEM_PARAMETER_NAME = "Regression Problem Data";
|
---|
40 |
|
---|
41 | public IMessagePort RegressionOrchestrationPort {
|
---|
42 | get { return (IMessagePort)Ports[REGRESSION_ORCHESTRATION_PORT_NAME]; }
|
---|
43 | }
|
---|
44 | public IMessagePort FeatureSelectionEvaluationPort {
|
---|
45 | get { return (IMessagePort)Ports[FEATURE_SELECTION_EVALUATION_PORT_NAME]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IValueParameter<IRegressionProblemData> ProblemDataParameter {
|
---|
49 | get { return (IValueParameter<IRegressionProblemData>)Parameters[REGRESSION_PROBLEM_PARAMETER_NAME]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IRegressionProblemData RegressionProblemData {
|
---|
53 | get { return ProblemDataParameter.Value; }
|
---|
54 | set { ProblemDataParameter.Value = value; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | private FeatureSelectionOrchestrator(bool deserializing) : base(deserializing) { }
|
---|
59 | [StorableHook(HookType.AfterDeserialization)]
|
---|
60 | private void AfterDeserialization() {
|
---|
61 | RegisterPortEvents();
|
---|
62 | }
|
---|
63 |
|
---|
64 | private FeatureSelectionOrchestrator(FeatureSelectionOrchestrator original, Cloner cloner)
|
---|
65 | : base(original, cloner) {
|
---|
66 | network = cloner.Clone(original.network);
|
---|
67 | RegisterPortEvents();
|
---|
68 | }
|
---|
69 | public override IDeepCloneable Clone(Cloner cloner) { return new FeatureSelectionOrchestrator(this, cloner); }
|
---|
70 |
|
---|
71 | //TODO remove network reference;
|
---|
72 | //TODO move regression problem to network
|
---|
73 | [Storable]
|
---|
74 | private readonly FeatureSelectionNetwork network;
|
---|
75 |
|
---|
76 | public FeatureSelectionOrchestrator(FeatureSelectionNetwork network)
|
---|
77 | : base() {
|
---|
78 | var featureSelectionPort = CreateEvaluationPort<BinaryVector>(FEATURE_SELECTION_EVALUATION_PORT_NAME, "BinaryVector", "Quality");
|
---|
79 | Ports.Add(featureSelectionPort);
|
---|
80 |
|
---|
81 | var regressionPort = CreateOrchestrationPort<IRegressionProblem>(REGRESSION_ORCHESTRATION_PORT_NAME);
|
---|
82 | Ports.Add(regressionPort);
|
---|
83 |
|
---|
84 | this.network = network;
|
---|
85 |
|
---|
86 | Parameters.Add(new ValueParameter<IRegressionProblemData>(REGRESSION_PROBLEM_PARAMETER_NAME, "", new RegressionProblemData()));
|
---|
87 | RegisterPortEvents();
|
---|
88 | }
|
---|
89 |
|
---|
90 | private void RegisterPortEvents() {
|
---|
91 | FeatureSelectionEvaluationPort.MessageReceived += (s, e) => FeatureSelectionEvaluationPort_MessageReceived(e.Value, e.Value2);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void FeatureSelectionEvaluationPort_MessageReceived(IMessage evaluationMessage, CancellationToken token) {
|
---|
95 | var problemData = (IRegressionProblemData)RegressionProblemData.Clone();
|
---|
96 | var binaryVector = (BinaryVector)evaluationMessage["BinaryVector"];
|
---|
97 | binaryVector.ElementNames = problemData.InputVariables.CheckedItems.Select(variable => variable.Value.Value);
|
---|
98 |
|
---|
99 | var allowedVariables = problemData.InputVariables.CheckedItems.Zip(binaryVector,
|
---|
100 | (variable, allowed) => new { VariableName = variable.Value, Allowed = allowed });
|
---|
101 |
|
---|
102 | foreach (var allowedVariable in allowedVariables)
|
---|
103 | problemData.InputVariables.SetItemCheckedState(allowedVariable.VariableName, allowedVariable.Allowed);
|
---|
104 |
|
---|
105 | var orchestrationMessage = RegressionOrchestrationPort.PrepareMessage();
|
---|
106 | orchestrationMessage["Problem"] = new RegressionProblem() { ProblemData = problemData };
|
---|
107 | orchestrationMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns);
|
---|
108 | RegressionOrchestrationPort.SendMessage(orchestrationMessage, token);
|
---|
109 |
|
---|
110 | var startMessage = RegressionOrchestrationPort.PrepareMessage();
|
---|
111 | startMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
|
---|
112 | RegressionOrchestrationPort.SendMessage(startMessage, token);
|
---|
113 |
|
---|
114 | var results = (ResultCollection)startMessage["Results"];
|
---|
115 |
|
---|
116 | var regressionSolution = results.Select(r => r.Value).OfType<IRegressionSolution>().First();
|
---|
117 | double quality = regressionSolution.TestMeanAbsoluteError;
|
---|
118 |
|
---|
119 | UpdatedResults(binaryVector, regressionSolution);
|
---|
120 | evaluationMessage["Quality"] = new DoubleValue(quality);
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void UpdatedResults(BinaryVector binaryVector, IRegressionSolution solution) {
|
---|
124 | if (!Results.ContainsKey("Best Vector")) Results.Add(new Result("Best Vector", typeof(BinaryVector)));
|
---|
125 | if (!Results.ContainsKey("Best Solution")) Results.Add(new Result("Best Solution", typeof(IRegressionSolution)));
|
---|
126 |
|
---|
127 | var previousBestVector = (BinaryVector)Results["Best Vector"].Value;
|
---|
128 | var prevoiusBestSolution = (IRegressionSolution)Results["Best Solution"].Value;
|
---|
129 |
|
---|
130 | //check if better vector & solution has been found
|
---|
131 | if (prevoiusBestSolution != null && prevoiusBestSolution.TestMeanAbsoluteError <= solution.TestMeanAbsoluteError)
|
---|
132 | return;
|
---|
133 |
|
---|
134 | Results["Best Vector"].Value = binaryVector;
|
---|
135 | Results["Best Solution"].Value = solution;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | //TODO Remove methods
|
---|
140 | public override void Pause() { network.Pause(); }
|
---|
141 | public override void Prepare(bool clearRuns = false) { network.Prepare(clearRuns); Results.Clear(); }
|
---|
142 | public override void Start() { network.Start(); }
|
---|
143 | public override void Stop() { network.Stop(); }
|
---|
144 | }
|
---|
145 | }
|
---|