source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.MachineLearning/FeatureSelectionOrchestrator.cs @ 14631

Last change on this file since 14631 was 14631, checked in by mkommend, 4 years ago

#2205: Updated OrchestratedAlgorithmNode to work with problems as well.

File size: 5.6 KB
Line 
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
22using System.Linq;
23using System.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace 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 binaryVector = (BinaryVector)evaluationMessage["BinaryVector"];
96      var problemData = (IRegressionProblemData)RegressionProblemData.Clone();
97
98      var allowedVariables = problemData.InputVariables.CheckedItems.Zip(binaryVector,
99        (variable, allowed) => new { VariableName = variable.Value, Allowed = allowed });
100      foreach (var allowedVariable in allowedVariables)
101        problemData.InputVariables.SetItemCheckedState(allowedVariable.VariableName, allowedVariable.Allowed);
102
103      var orchestrationMessage = RegressionOrchestrationPort.PrepareMessage();
104      orchestrationMessage["Problem"] = new RegressionProblem() { ProblemData = problemData };
105      orchestrationMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare);
106      RegressionOrchestrationPort.SendMessage(orchestrationMessage, token);
107
108      var startMessage = RegressionOrchestrationPort.PrepareMessage();
109      startMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
110      RegressionOrchestrationPort.SendMessage(startMessage, token);
111
112      var results = (ResultCollection)startMessage["Results"];
113
114      var regressionSolution = results.Select(r => r.Value).OfType<IRegressionSolution>().First();
115      double quality = regressionSolution.TestMeanAbsoluteError;
116      evaluationMessage["Quality"] = new DoubleValue(quality);
117    }
118
119
120    //TODO Remove methods
121    public override void Pause() { network.Pause(); }
122    public override void Prepare(bool clearRuns = false) { network.Prepare(clearRuns); }
123    public override void Start() { network.Start(); }
124    public override void Stop() { network.Stop(); }
125  }
126}
Note: See TracBrowser for help on using the repository browser.