Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14327


Ignore:
Timestamp:
10/07/16 11:00:55 (8 years ago)
Author:
gkronber
Message:

#2653: merged r14236,r14314:14315 and r14322 from trunk to stable

Location:
stable
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModelSurrogate.cs

    r14186 r14327  
    2121#endregion
    2222
     23using System;
    2324using System.Collections.Generic;
     25using System.Diagnostics.Eventing.Reader;
    2426using System.Linq;
    2527using HeuristicLab.Common;
     
    3638  public sealed class GradientBoostedTreesModelSurrogate : RegressionModel, IGradientBoostedTreesModel {
    3739    // don't store the actual model!
    38     private IGradientBoostedTreesModel actualModel; // the actual model is only recalculated when necessary
     40    // the actual model is only recalculated when necessary
     41    private readonly Lazy<IGradientBoostedTreesModel> actualModel;
     42    private IGradientBoostedTreesModel ActualModel {
     43      get { return actualModel.Value; }
     44    }
    3945
    4046    [Storable]
     
    5763
    5864    public override IEnumerable<string> VariablesUsedForPrediction {
    59       get { return actualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
     65      get {
     66        return ActualModel.Models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x);
     67      }
    6068    }
    6169
    6270    [StorableConstructor]
    63     private GradientBoostedTreesModelSurrogate(bool deserializing) : base(deserializing) { }
     71    private GradientBoostedTreesModelSurrogate(bool deserializing)
     72      : base(deserializing) {
     73      actualModel = new Lazy<IGradientBoostedTreesModel>(() => RecalculateModel());
     74    }
    6475
    6576    private GradientBoostedTreesModelSurrogate(GradientBoostedTreesModelSurrogate original, Cloner cloner)
    6677      : base(original, cloner) {
    67       if (original.actualModel != null) this.actualModel = cloner.Clone(original.actualModel);
     78      IGradientBoostedTreesModel clonedModel = null;
     79      if (original.ActualModel != null) clonedModel = cloner.Clone(original.ActualModel);
     80      actualModel = new Lazy<IGradientBoostedTreesModel>(CreateLazyInitFunc(clonedModel)); // only capture clonedModel in the closure
    6881
    6982      this.trainingProblemData = cloner.Clone(original.trainingProblemData);
     
    7588      this.m = original.m;
    7689      this.nu = original.nu;
     90    }
     91
     92    private Func<IGradientBoostedTreesModel> CreateLazyInitFunc(IGradientBoostedTreesModel clonedModel) {
     93      return () => {
     94        return clonedModel == null ? RecalculateModel() : clonedModel;
     95      };
    7796    }
    7897
     
    96115      IGradientBoostedTreesModel model)
    97116      : this(trainingProblemData, seed, lossFunction, iterations, maxSize, r, m, nu) {
    98       this.actualModel = model;
     117      actualModel = new Lazy<IGradientBoostedTreesModel>(() => model);
    99118    }
    100119
     
    105124    // forward message to actual model (recalculate model first if necessary)
    106125    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    107       if (actualModel == null) actualModel = RecalculateModel();
    108       return actualModel.GetEstimatedValues(dataset, rows);
     126      return ActualModel.GetEstimatedValues(dataset, rows);
    109127    }
    110128
     
    119137    public IEnumerable<IRegressionModel> Models {
    120138      get {
    121         if (actualModel == null) actualModel = RecalculateModel();
    122         return actualModel.Models;
     139        return ActualModel.Models;
    123140      }
    124141    }
     
    126143    public IEnumerable<double> Weights {
    127144      get {
    128         if (actualModel == null) actualModel = RecalculateModel();
    129         return actualModel.Weights;
     145        return ActualModel.Weights;
    130146      }
    131147    }
  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs

    r14308 r14327  
    3636  public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel {
    3737
     38    private readonly object kdTreeLockObject = new object();
    3839    private alglib.nearestneighbor.kdtree kdTree;
    3940    public alglib.nearestneighbor.kdtree KDTree {
     
    4748      }
    4849    }
     50
    4951
    5052    public override IEnumerable<string> VariablesUsedForPrediction {
     
    200202          x[column] = inputData[row, column];
    201203        }
    202         int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
    203         alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
    204         alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); // gkronber: this call changes the kdTree data structure
     204        int numNeighbours;
     205        lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure
     206          numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
     207          alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
     208          alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
     209        }
    205210
    206211        double distanceWeightedValue = 0.0;
    207212        double distsSum = 0.0;
    208         for (int i = 0; i < actNeighbours; i++) {
     213        for (int i = 0; i < numNeighbours; i++) {
    209214          distanceWeightedValue += neighbours[i, columns] / dists[i];
    210215          distsSum += 1.0 / dists[i];
     
    233238          x[column] = inputData[row, column];
    234239        }
    235         int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
    236         alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
    237         alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
    238 
     240        int numNeighbours;
     241        lock (kdTreeLockObject) {
     242          // gkronber: the following calls change the kdTree data structure
     243          numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
     244          alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
     245          alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
     246        }
    239247        Array.Clear(y, 0, y.Length);
    240         for (int i = 0; i < actNeighbours; i++) {
     248        for (int i = 0; i < numNeighbours; i++) {
    241249          int classValue = (int)Math.Round(neighbours[i, columns]);
    242250          y[classValue]++;
  • stable/HeuristicLab.Problems.DataAnalysis

  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationModel.cs

    r14186 r14327  
    2222using System.Collections.Generic;
    2323namespace HeuristicLab.Problems.DataAnalysis {
     24  /// <summary>
     25  /// Interface for all classification models.
     26  /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks>
     27  /// </summary>
    2428  public interface IClassificationModel : IDataAnalysisModel {
    2529    IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows);
  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisModel.cs

    r14186 r14327  
    2424
    2525namespace HeuristicLab.Problems.DataAnalysis {
     26  /// <summary>
     27  /// Interface for all data-analysis models (regression/classification/clustering).
     28  /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks>
     29  /// </summary>
    2630  public interface IDataAnalysisModel : INamedItem {
    2731    IEnumerable<string> VariablesUsedForPrediction { get; }
  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionModel.cs

    r14186 r14327  
    2323
    2424namespace HeuristicLab.Problems.DataAnalysis {
     25  /// <summary>
     26  /// Interface for all regression models.
     27  /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks>
     28  /// </summary>
    2529  public interface IRegressionModel : IDataAnalysisModel {
    2630    IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows);
  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisModel.cs

    r14186 r14327  
    2222using System.Collections.Generic;
    2323namespace HeuristicLab.Problems.DataAnalysis {
     24  /// <summary>
     25  /// Interface for all time series prognosis models.
     26  /// <remarks>All methods and properties in in this interface must be implemented thread safely</remarks>
     27  /// </summary>
    2428  public interface ITimeSeriesPrognosisModel : IRegressionModel {
    2529    IEnumerable<IEnumerable<double>> GetPrognosedValues(IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons);
Note: See TracChangeset for help on using the changeset viewer.