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

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

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

File size: 5.8 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      //TODO 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      RegressionAlgorithmNode = new OrchestratedAlgorithmNode("Regression");
72      RegressionAlgorithmNode.Algorithm = regressionAlgorithm;
73      Orchestrator.RegressionOrchestrationPort.ConnectedPort = RegressionAlgorithmNode.OrchestrationPort;
74      RegressionAlgorithmNode.OrchestrationPort.CloneParametersFromPort(Orchestrator.RegressionOrchestrationPort);
75      Nodes.Add(RegressionAlgorithmNode);
76    }
77
78    private IAlgorithm CreateFeatureSelectionAlgorithm() {
79      var problem = new OrchestratedBinaryProblem(Orchestrator, 0, false);
80      problem.Encoding.SolutionCreator = new RandomBinaryVectorCreator() { TrueProbability = new DoubleValue(0.2) };
81
82      var osga = new OffspringSelectionGeneticAlgorithm();
83      osga.Problem = problem;
84      osga.PopulationSize.Value = 100;
85      osga.ComparisonFactorLowerBound.Value = 1;
86      osga.ComparisonFactorUpperBound.Value = 1;
87      osga.SuccessRatio.Value = 1.0;
88      osga.MutationProbability.Value = 0.15;
89      osga.Mutator = osga.MutatorParameter.ValidValues.OfType<SomePositionsBitflipManipulator>().First();
90      return osga;
91    }
92
93    private static IAlgorithm CreateRegressionAlgorithm() {
94      var linreg = new LinearRegression();
95      return linreg;
96    }
97
98
99    public void Prepare() { Prepare(false); }
100    public void Prepare(bool clearRuns) {
101      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
102      if (clearRuns)
103        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns);
104      else
105        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare);
106      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
107    }
108
109    public void Start() {
110      var problem = (OrchestratedBinaryProblem)FeatureSelectionAlgorithmNode.Algorithm.Problem;
111      problem.Encoding.Length = Orchestrator.RegressionProblemData.AllowedInputVariables.Count();
112
113      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
114      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
115      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
116    }
117    public void Pause() {
118      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
119      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
120      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
121    }
122    public void Stop() {
123      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
124      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
125      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
126      RegressionAlgorithmNode.Algorithm.Runs.Clear();
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.