1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Core.Networks;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
9 | using HeuristicLab.Problems.DataAnalysis;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Networks.FeatureSelection_Network {
|
---|
12 | [Item("FeatureSelectionConnector", "")]
|
---|
13 | public sealed class FeatureSelectionConnector : Node {
|
---|
14 | private FeatureSelectionConnector(FeatureSelectionConnector original, Cloner cloner) : base(original, cloner) { }
|
---|
15 | public FeatureSelectionConnector()
|
---|
16 | : base() {
|
---|
17 | if (Ports.Count == 0)
|
---|
18 | Initialize();
|
---|
19 | }
|
---|
20 |
|
---|
21 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
22 | return new FeatureSelectionConnector(this, cloner);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void Initialize() {
|
---|
26 | var parameters = new MessagePort("Parameters");
|
---|
27 | Ports.Add(parameters);
|
---|
28 | parameters.Parameters.Add(new PortParameter<IRegressionProblemData>("ProblemData") { Type = PortParameterType.Input });
|
---|
29 |
|
---|
30 | var selectionPort = new MessagePort("Selection Connector");
|
---|
31 | Ports.Add(selectionPort);
|
---|
32 | selectionPort.Parameters.Add(new PortParameter<BinaryVector>("Selection") { Type = PortParameterType.Input });
|
---|
33 | selectionPort.Parameters.Add(new PortParameter<DoubleValue>("Quality") { Type = PortParameterType.Input | PortParameterType.Output });
|
---|
34 |
|
---|
35 | var regressionPort = new MessagePort("Regression Connector");
|
---|
36 | Ports.Add(regressionPort);
|
---|
37 | regressionPort.Parameters.Add(new PortParameter<IRegressionProblemData>("ProblemData") { Type = PortParameterType.Output });
|
---|
38 | regressionPort.Parameters.Add(new PortParameter<IRegressionSolution>("Linear regression solution") { Type = PortParameterType.Input });
|
---|
39 | RegisterEvents();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void RegisterEvents() {
|
---|
43 | var selection = (IMessagePort)Ports["Selection Connector"];
|
---|
44 | selection.MessageReceived += Selection_MessageReceived;
|
---|
45 | }
|
---|
46 | public void DeregisterEvents() {
|
---|
47 | var selection = (IMessagePort)Ports["Selection Connector"];
|
---|
48 | selection.MessageReceived -= Selection_MessageReceived;
|
---|
49 | }
|
---|
50 |
|
---|
51 | private void Selection_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
|
---|
52 | // get parameters
|
---|
53 | var parametersPort = (IMessagePort)Ports["Parameters"];
|
---|
54 | var parameters = parametersPort.PrepareMessage();
|
---|
55 | parametersPort.SendMessage(parameters, e.Value2);
|
---|
56 | var problemData = (IRegressionProblemData)parameters["ProblemData"];
|
---|
57 | problemData = (IRegressionProblemData)problemData.Clone();
|
---|
58 |
|
---|
59 | // build coordinates
|
---|
60 | var selectionMsg = e.Value;
|
---|
61 | var selection = (BinaryVector)selectionMsg["Selection"];
|
---|
62 | var allowedVariables = problemData.InputVariables;
|
---|
63 | foreach (var t in selection.Zip(allowedVariables, Tuple.Create)) {
|
---|
64 | problemData.InputVariables.SetItemCheckedState(t.Item2, t.Item1);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // solve Regression
|
---|
68 | var regressionConPort = (IMessagePort)Ports["Regression Connector"];
|
---|
69 | var regressionMsg = regressionConPort.PrepareMessage();
|
---|
70 | regressionMsg["ProblemData"] = problemData;
|
---|
71 | regressionConPort.SendMessage(regressionMsg, e.Value2);
|
---|
72 | var solution = (IRegressionSolution)regressionMsg["Linear regression solution"];
|
---|
73 |
|
---|
74 | selectionMsg["Quality"] = new DoubleValue(solution.TestNormalizedMeanSquaredError);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|