Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12325 for branches


Ignore:
Timestamp:
04/17/15 19:08:21 (10 years ago)
Author:
gkronber
Message:

#2205: added solution cache

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Networks/3.3/FeatureSelection Network/FeatureSelectionConnector.cs

    r12324 r12325  
    11using System;
     2using System.Collections;
     3using System.Collections.Generic;
    24using System.Linq;
    35using System.Threading;
     
    4951    }
    5052
     53    private Dictionary<BinaryVector, double> solutionCache = new Dictionary<BinaryVector, double>(new BinaryVectorComparer());
     54
    5155    private void Selection_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
    5256      // get parameters
     
    5559      parametersPort.SendMessage(parameters, e.Value2);
    5660      var problemData = (IRegressionProblemData)parameters["ProblemData"];
    57       problemData = (IRegressionProblemData)problemData.Clone();
    5861
    59       // build coordinates
     62      // filter allowed variables
    6063      var selectionMsg = e.Value;
    6164      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);
    6589      }
     90    }
     91  }
    6692
    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    }
    7398
    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);
    75102    }
    76103  }
Note: See TracChangeset for help on using the changeset viewer.