Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.MachineLearning/FeatureSelectionNetwork.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: 6.2 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.Algorithms.DataAnalysis;
25using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Networks;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Networks.IntegratedOptimization.MachineLearning {
35  [Item("Feature Selection Network 2", "")]
36  [Creatable("Optimization Networks")]
37  [StorableClass]
38  public sealed class FeatureSelectionNetwork : Network {
39    [StorableConstructor]
40    private FeatureSelectionNetwork(bool deserializing) : base(deserializing) { }
41
42    private FeatureSelectionNetwork(FeatureSelectionNetwork original, Cloner cloner)
43      : base(original, cloner) {
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new FeatureSelectionNetwork(this, cloner);
47    }
48
49    [Storable]
50    private readonly OrchestratedAlgorithmNode FeatureSelectionAlgorithmNode;
51    [Storable]
52    private readonly OrchestratedAlgorithmNode RegressionAlgorithmNode;
53    [Storable]
54    private readonly FeatureSelectionOrchestrator Orchestrator;
55
56    public FeatureSelectionNetwork()
57      : base() {
58      Orchestrator = new FeatureSelectionOrchestrator(this);
59      Nodes.Add(Orchestrator);
60
61      var featureSelectionAlgorithm = CreateFeatureSelectionAlgorithm();
62      //TOO configure FeatureSelectionProblem
63
64      FeatureSelectionAlgorithmNode = new OrchestratedAlgorithmNode("Feature Selection");
65      FeatureSelectionAlgorithmNode.Algorithm = featureSelectionAlgorithm;
66      FeatureSelectionAlgorithmNode.EvaluationPort.ConnectedPort = Orchestrator.FeatureSelectionEvaluationPort;
67      FeatureSelectionAlgorithmNode.EvaluationPort.CloneParametersFromPort(Orchestrator.FeatureSelectionEvaluationPort);
68      Nodes.Add(FeatureSelectionAlgorithmNode);
69
70      var regressionAlgorithm = CreateRegressionAlgorithm();
71      //TODO set empty regresion problem
72
73      RegressionAlgorithmNode = new OrchestratedAlgorithmNode("Regression");
74      RegressionAlgorithmNode.Algorithm = regressionAlgorithm;
75      RegressionAlgorithmNode.OrchestrationPort.ConnectedPort = Orchestrator.RegressionOrchestrationPort;
76      RegressionAlgorithmNode.OrchestrationPort.CloneParametersFromPort(Orchestrator.RegressionOrchestrationPort);
77      Nodes.Add(RegressionAlgorithmNode);
78    }
79
80    private IAlgorithm CreateFeatureSelectionAlgorithm() {
81      var problem = new OrchestratedBinaryProblem(Orchestrator, 0, false);
82      problem.Encoding.SolutionCreator = new RandomBinaryVectorCreator() { TrueProbability = new DoubleValue(0.2) };
83
84
85      var osga = new OffspringSelectionGeneticAlgorithm();
86      osga.Problem = problem;
87      osga.PopulationSize.Value = 100;
88      osga.ComparisonFactorLowerBound.Value = 1;
89      osga.ComparisonFactorUpperBound.Value = 1;
90      osga.SuccessRatio.Value = 1.0;
91      osga.MutationProbability.Value = 0.15;
92      osga.Mutator = osga.MutatorParameter.ValidValues.OfType<SomePositionsBitflipManipulator>().First();
93      return osga;
94    }
95
96    private static IAlgorithm CreateRegressionAlgorithm() {
97      var linreg = new LinearRegression();
98      return linreg;
99    }
100
101
102    public void Prepare() { Prepare(false); }
103    public void Prepare(bool clearRuns) {
104      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
105      msg.Values.Add(new MessageValue<EnumValue<OrchestrationMessage>>("OrchestrationMessage"));
106      if (clearRuns)
107        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns);
108      else
109        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare);
110      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
111    }
112
113    public void Start() {
114      var problem = (OrchestratedBinaryProblem)FeatureSelectionAlgorithmNode.Algorithm.Problem;
115      problem.Encoding.Length = Orchestrator.RegressionProblemData.AllowedInputVariables.Count();
116
117      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
118      msg.Values.Add(new MessageValue<EnumValue<OrchestrationMessage>>("OrchestrationMessage"));
119      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
120      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
121    }
122    public void Pause() {
123      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
124      msg.Values.Add(new MessageValue<EnumValue<OrchestrationMessage>>("OrchestrationMessage"));
125      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
126      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
127    }
128    public void Stop() {
129      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
130      msg.Values.Add(new MessageValue<EnumValue<OrchestrationMessage>>("OrchestrationMessage"));
131      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
132      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken()); ;
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.