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

Last change on this file since 14635 was 14635, checked in by jkarder, 3 years ago

#2205: worked on optimization networks

  • created default parameters for orchestration and evaluation ports
  • adapted FeatureSelectionNetwork
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      //TODO set empty regression problem
72
73      RegressionAlgorithmNode = new OrchestratedAlgorithmNode("Regression");
74      RegressionAlgorithmNode.Algorithm = regressionAlgorithm;
75      Orchestrator.RegressionOrchestrationPort.ConnectedPort = RegressionAlgorithmNode.OrchestrationPort;
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      if (clearRuns)
106        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns);
107      else
108        msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare);
109      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
110    }
111
112    public void Start() {
113      var problem = (OrchestratedBinaryProblem)FeatureSelectionAlgorithmNode.Algorithm.Problem;
114      problem.Encoding.Length = Orchestrator.RegressionProblemData.AllowedInputVariables.Count();
115
116      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
117      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
118      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
119    }
120    public void Pause() {
121      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
122      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
123      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken());
124    }
125    public void Stop() {
126      var msg = FeatureSelectionAlgorithmNode.OrchestrationPort.PrepareMessage();
127      msg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
128      FeatureSelectionAlgorithmNode.OrchestrationPort.ReceiveMessage(msg, new CancellationToken()); ;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.