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

Last change on this file since 14675 was 14675, checked in by mkommend, 3 years ago

#2205: Worked on optimization networks for integrated machine learning.

File size: 7.0 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 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);
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
118      UpdatedResults(binaryVector, regressionSolution);
119
120
121      double quality = regressionSolution.TestMeanAbsoluteError;
122      evaluationMessage["Quality"] = new DoubleValue(quality);
123    }
124
125    private void UpdatedResults(BinaryVector binaryVector, IRegressionSolution solution) {
126      if (!Results.ContainsKey("Best Solution Vector")) {
127        Results.Add(new Result("Best Solution Vector", typeof(BinaryVector)));
128        //Results.Add(new Result("Best Solution Variables", typeof(DoubleArray)));
129        Results.Add(new Result("Best Symbolic Solution", typeof(IRegressionSolution)));
130      }
131
132      var previousBestVector = (BinaryVector)Results["Best Solution Vector"].Value;
133
134      //check if better vector has been found
135      if (previousBestVector != null && binaryVector.SequenceEqual(previousBestVector))
136        return;
137
138      Results["Best Solution Vector"].Value = binaryVector;
139      Results["Best Symbolic Solution"].Value = solution;
140
141      //var variableNames = solution.ProblemData.AllowedInputVariables;
142      //alglib.linearmodel lm = BuildModel(best, problemData);
143
144      //double[] coefficients = null;
145      //int nFeatures = -1;
146      //alglib.lrunpack(lm, out coefficients, out nFeatures);
147
148      //var doubleArray = new DoubleArray(coefficients.ToArray());
149      //doubleArray.ElementNames = variableNames;
150      //Results["Best Solution Variables"].Value = doubleArray;
151
152
153    }
154
155
156    //TODO Remove methods
157    public override void Pause() { network.Pause(); }
158    public override void Prepare(bool clearRuns = false) { network.Prepare(clearRuns); }
159    public override void Start() { network.Start(); }
160    public override void Stop() { network.Stop(); }
161  }
162}
Note: See TracBrowser for help on using the repository browser.