Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14625 was 14625, checked in by mkommend, 7 years ago

#2205: Added network for integrated machine learning.

File size: 5.7 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;
33using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
34
35namespace HeuristicLab.Networks.IntegratedOptimization.MachineLearning {
36  [StorableClass]
37  public sealed class FeatureSelectionOrchestrator : OrchestratorNode {
38    private const string REGRESSION_ORCHESTRATION_PORT_NAME = "Regression algorithm orchestration port";
39    private const string FEATURE_SELECTION_EVALUATION_PORT_NAME = "Feature selection evaluation port";
40    private const string REGRESSION_PROBLEM_PARAMETER_NAME = "Regression Problem Data";
41
42    public IMessagePort RegressionOrchestrationPort {
43      get { return (IMessagePort)Ports[REGRESSION_ORCHESTRATION_PORT_NAME]; }
44    }
45    public IMessagePort FeatureSelectionEvaluationPort {
46      get { return (IMessagePort)Ports[FEATURE_SELECTION_EVALUATION_PORT_NAME]; }
47    }
48
49    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
50      get { return (IValueParameter<IRegressionProblemData>)Parameters[REGRESSION_PROBLEM_PARAMETER_NAME]; }
51    }
52
53    public IRegressionProblemData RegressionProblemData {
54      get { return ProblemDataParameter.Value; }
55      set { ProblemDataParameter.Value = value; }
56    }
57
58    [StorableConstructor]
59    private FeatureSelectionOrchestrator(bool deserializing) : base(deserializing) { }
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
62      RegisterPortEvents();
63    }
64
65    private FeatureSelectionOrchestrator(FeatureSelectionOrchestrator original, Cloner cloner)
66      : base(original, cloner) {
67      network = cloner.Clone(original.network);
68      RegisterPortEvents();
69    }
70    public override IDeepCloneable Clone(Cloner cloner) { return new FeatureSelectionOrchestrator(this, cloner); }
71
72    //TODO remove network reference;
73    //TODO move regression problem to network
74    [Storable]
75    private readonly FeatureSelectionNetwork network;
76
77    public FeatureSelectionOrchestrator(FeatureSelectionNetwork network)
78      : base() {
79      var featureSelectionPort = CreateEvaluationPort<BinaryVector>(FEATURE_SELECTION_EVALUATION_PORT_NAME, "BinaryVector", "Quality");
80      Ports.Add(featureSelectionPort);
81
82      var regressionPort = CreateOrchestrationPort<IRegressionProblem>(REGRESSION_ORCHESTRATION_PORT_NAME);
83      Ports.Add(regressionPort);
84
85      this.network = network;
86
87      Parameters.Add(new ValueParameter<IRegressionProblemData>(REGRESSION_PROBLEM_PARAMETER_NAME, "", new RegressionProblemData()));
88      RegisterPortEvents();
89    }
90
91    private void RegisterPortEvents() {
92      FeatureSelectionEvaluationPort.MessageReceived += (s, e) => FeatureSelectionEvaluationPort_MessageReceived(e.Value, e.Value2);
93    }
94
95    private void FeatureSelectionEvaluationPort_MessageReceived(IMessage evaluationMessage, CancellationToken token) {
96      var binaryVector = (BinaryVector)evaluationMessage["BinaryVector"];
97      var problemData = (IRegressionProblemData)RegressionProblemData.Clone();
98
99      var allowedVariables = problemData.InputVariables.CheckedItems.Zip(binaryVector,
100        (variable, allowed) => new { VariableName = variable.Value, Allowed = allowed });
101      foreach (var allowedVariable in allowedVariables)
102        problemData.InputVariables.SetItemCheckedState(allowedVariable.VariableName, allowedVariable.Allowed);
103
104      var orchestrationMessage = RegressionOrchestrationPort.PrepareMessage();
105      orchestrationMessage["Problem"] = new SymbolicRegressionSingleObjectiveProblem() { ProblemData = problemData };
106      orchestrationMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare);
107      RegressionOrchestrationPort.SendMessage(orchestrationMessage, token);
108
109      var startMessage = RegressionOrchestrationPort.PrepareMessage();
110      startMessage["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
111      RegressionOrchestrationPort.SendMessage(startMessage, token);
112
113      var results = (ResultCollection)startMessage["Results"];
114
115      var regressionSolution = results.Select(r => r.Value).OfType<IRegressionSolution>().First();
116      double quality = regressionSolution.TestMeanAbsoluteError;
117      evaluationMessage["Quality"] = new DoubleValue(quality);
118    }
119
120
121    //TODO Remove methods
122    public override void Pause() { network.Pause(); }
123    public override void Prepare(bool clearRuns = false) { network.Prepare(clearRuns); }
124    public override void Start() { network.Start(); }
125    public override void Stop() { network.Stop(); }
126  }
127}
Note: See TracBrowser for help on using the repository browser.