Changeset 12325 for branches/OptimizationNetworks
- Timestamp:
- 04/17/15 19:08:21 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks/3.3/FeatureSelection Network/FeatureSelectionConnector.cs
r12324 r12325 1 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 2 4 using System.Linq; 3 5 using System.Threading; … … 49 51 } 50 52 53 private Dictionary<BinaryVector, double> solutionCache = new Dictionary<BinaryVector, double>(new BinaryVectorComparer()); 54 51 55 private void Selection_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { 52 56 // get parameters … … 55 59 parametersPort.SendMessage(parameters, e.Value2); 56 60 var problemData = (IRegressionProblemData)parameters["ProblemData"]; 57 problemData = (IRegressionProblemData)problemData.Clone();58 61 59 // build coordinates62 // filter allowed variables 60 63 var selectionMsg = e.Value; 61 64 var selection = (BinaryVector)selectionMsg["Selection"]; 62 var allowedVariables = problemData.InputVariables; 63 foreach (var t in selection.Zip(allowedVariables, Tuple.Create)) { 64 problemData.InputVariables.SetItemCheckedState(t.Item2, t.Item1); 65 66 // if possible return the cached answer 67 double solutionQuality; 68 if (solutionCache.TryGetValue(selection, out solutionQuality)) { 69 selectionMsg["Quality"] = new DoubleValue(solutionQuality); 70 } else { 71 var allowedVariables = problemData.AllowedInputVariables; 72 73 var selectedInputVariables = from t in selection.Zip(allowedVariables, Tuple.Create) 74 where t.Item1 75 select t.Item2; 76 77 var clonedProblemData = new RegressionProblemData(problemData.Dataset, selectedInputVariables, problemData.TargetVariable); 78 79 // solve Regression 80 var regressionConPort = (IMessagePort)Ports["Regression Connector"]; 81 var regressionMsg = regressionConPort.PrepareMessage(); 82 regressionMsg["ProblemData"] = clonedProblemData; 83 regressionConPort.SendMessage(regressionMsg, e.Value2); 84 var solution = (IRegressionSolution)regressionMsg["Linear regression solution"]; 85 86 selectionMsg["Quality"] = new DoubleValue(solution.TestNormalizedMeanSquaredError); 87 // cache solution quality 88 solutionCache.Add(selection, solution.TestNormalizedMeanSquaredError); 65 89 } 90 } 91 } 66 92 67 // solve Regression 68 var regressionConPort = (IMessagePort)Ports["Regression Connector"]; 69 var regressionMsg = regressionConPort.PrepareMessage(); 70 regressionMsg["ProblemData"] = problemData; 71 regressionConPort.SendMessage(regressionMsg, e.Value2); 72 var solution = (IRegressionSolution)regressionMsg["Linear regression solution"]; 93 internal class BinaryVectorComparer : IEqualityComparer<BinaryVector> { 94 public bool Equals(BinaryVector x, BinaryVector y) { 95 return x.Length == y.Length && 96 x.Zip(y, Tuple.Create).All(t => t.Item1 == t.Item2); 97 } 73 98 74 selectionMsg["Quality"] = new DoubleValue(solution.TestNormalizedMeanSquaredError); 99 public int GetHashCode(BinaryVector obj) { 100 // return number of set bits 101 return obj.Count(t => t); 75 102 } 76 103 }
Note: See TracChangeset
for help on using the changeset viewer.