Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/FeatureSelection Network/FeatureSelectionNetwork.cs @ 13799

Last change on this file since 13799 was 12324, checked in by gkronber, 9 years ago

#2205: renamed classes and files (hard coded network and connector)

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
23using System.Linq;
24using System.Threading;
25using HeuristicLab.Algorithms.DataAnalysis;
26using HeuristicLab.Algorithms.GeneticAlgorithm;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Networks;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.BinaryVectorEncoding;
32using HeuristicLab.Networks.FeatureSelection_Network;
33using HeuristicLab.Networks.Programmable;
34using HeuristicLab.Problems.DataAnalysis;
35using HeuristicLab.Problems.Instances.DataAnalysis;
36using HeuristicLab.Random;
37
38namespace HeuristicLab.Networks {
39  [Creatable("Optimization Networks")]
40  [Item("Feature Selection Network", "An optimization network which contains a binary GA and a LR.")]
41  public sealed class FeatureSelectionNetwork : Network {
42    private FeatureSelectionNetwork(FeatureSelectionNetwork original, Cloner cloner) : base(original, cloner) { }
43    public FeatureSelectionNetwork()
44      : base() {
45      if (Nodes.Count == 0)
46        Initialize();
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new FeatureSelectionNetwork(this, cloner);
51    }
52
53    public void Initialize() {
54
55
56      #region ParametersNode
57      var paramsNode = new UserDefinedNode("ParametersNode");
58      Nodes.Add(paramsNode);
59
60      var paramsPort = new MessagePort("Parameters");
61      paramsNode.Ports.Add(paramsPort);
62
63      var rand = new MersenneTwister();
64      var normRand = new NormalDistributedRandom(rand, 0, 1);
65      var instanceProvider = new FeatureSelectionInstanceProvider();
66      var problemData = instanceProvider.LoadData(new FeatureSelection(100, 0.1, 0.2, rand, normRand));
67
68      paramsPort.Parameters.Add(new PortParameter<IRegressionProblemData>("ProblemData") {
69        Type = PortParameterType.Output,
70        DefaultValue = problemData
71      });
72      paramsPort.Parameters.Add(new PortParameter<IntValue>("Length") {
73        Type = PortParameterType.Output,
74        DefaultValue = new IntValue(problemData.AllowedInputVariables.Count())
75      });
76      #endregion
77
78      #region Selection Node
79      var selectionNode = new AlgorithmNode("SelectionNode");
80      Nodes.Add(selectionNode);
81
82      var configPort = new ConfigurationPort("ConfigureSelection");
83      selectionNode.Ports.Add(configPort);
84
85      configPort.Parameters.Add(new PortParameter<IRegressionProblemData>("ProblemData") {
86        Type = PortParameterType.Input
87      });
88
89      var evaluatePort = new MessagePort("EvaluateSelection");
90      selectionNode.Ports.Add(evaluatePort);
91
92      evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("Selection") {
93        Type = PortParameterType.Output
94      });
95      evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
96        Type = PortParameterType.Output | PortParameterType.Input
97      });
98
99      Func<BinaryVector, double> eval = vector => {
100        // hook returns test quality!, LR optimizes training quality only!
101
102        var msg = evaluatePort.PrepareMessage();
103        msg["Selection"] = vector;
104        evaluatePort.SendMessage(msg, new CancellationToken(false));
105
106        return ((DoubleValue)msg["Quality"]).Value;
107      };
108
109      var selectionGa = new GeneticAlgorithm();
110      var selectionProb = new SelectionProblem(eval, false);
111
112      selectionGa.Problem = selectionProb;
113      selectionGa.MaximumGenerations.Value = 50;
114      selectionGa.PopulationSize.Value = 10;
115      selectionGa.Mutator = selectionGa.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
116      selectionNode.Algorithm = selectionGa;
117
118      #endregion
119
120      #region RegressionNode
121      var regressionNode = new AlgorithmNode("Regression");
122      Nodes.Add(regressionNode);
123
124      var executePort = new ExecutionPort("Execute");
125      regressionNode.Ports.Add(executePort);
126
127      executePort.Parameters.Add(new PortParameter<IRegressionProblemData>("ProblemData") {
128        Type = PortParameterType.Input
129      });
130      executePort.Parameters.Add(new PortParameter<IRegressionSolution>("Linear regression solution") {
131        Type = PortParameterType.Output
132      });
133
134      var linReg = new LinearRegression();
135      regressionNode.Algorithm = linReg;
136      #endregion
137
138      #region Connector
139      var featureSelectionConnector = (INode)new FeatureSelectionConnector();
140      Nodes.Add(featureSelectionConnector);
141      #endregion
142
143      #region Wire Ports
144      configPort.ConnectedPort = paramsPort;
145      evaluatePort.ConnectedPort = (IMessagePort)featureSelectionConnector.Ports["Selection Connector"];
146      ((IMessagePort)featureSelectionConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
147      ((IMessagePort)featureSelectionConnector.Ports["Regression Connector"]).ConnectedPort = executePort;
148
149      paramsPort.SendMessage(paramsPort.PrepareMessage());
150      #endregion
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.