Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1848


Ignore:
Timestamp:
05/18/09 23:16:54 (16 years ago)
Author:
gkronber
Message:

Implemented #632 (Abort functionality for Operator SupportVectorCreator).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs

    r1837 r1848  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.DataAnalysis;
     29using System.Threading;
    2930
    3031namespace HeuristicLab.SupportVectorMachines {
    3132  public class SupportVectorCreator : OperatorBase {
     33    private Thread trainingThread;
     34    private object locker = new object();
     35    private bool abortRequested = false;
     36
     37    public override bool SupportsAbort {
     38      get {
     39        return true;
     40      }
     41    }
    3242
    3343    public SupportVectorCreator()
     
    5060      AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.New | VariableKind.Out));
    5161      AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.New | VariableKind.Out));
     62    }
    5263
     64    public override void Abort() {
     65      abortRequested = true;
     66      lock (locker) {
     67        if (trainingThread != null && trainingThread.ThreadState == ThreadState.Running) {
     68          trainingThread.Abort();
     69        }
     70      }
    5371    }
    5472
    5573    public override IOperation Apply(IScope scope) {
     74      abortRequested = false;
    5675      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    5776      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);
     
    7493      SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem);
    7594      SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform);
    76       SVM.Model model = SVM.Training.Train(scaledProblem, parameter);
    7795
    78       //persist variables in scope
    79       SVMModel modelData = new SVMModel();
    80       modelData.Data = model;
    81       scope.AddVariable(new Variable(scope.TranslateName("SVMModel"),modelData));
    82       SVMRangeTransform rangeTransformData = new SVMRangeTransform();
    83       rangeTransformData.Data = rangeTransform;
    84       scope.AddVariable(new Variable(scope.TranslateName("SVMRangeTransform"),rangeTransformData));
    85 
     96      SVM.Model model = StartTraining(scaledProblem, parameter);
     97      if (!abortRequested) {
     98        //persist variables in scope
     99        SVMModel modelData = new SVMModel();
     100        modelData.Data = model;
     101        scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
     102        SVMRangeTransform rangeTransformData = new SVMRangeTransform();
     103        rangeTransformData.Data = rangeTransform;
     104        scope.AddVariable(new Variable(scope.TranslateName("SVMRangeTransform"), rangeTransformData));
     105      }
    86106      return null;
    87107    }
    88108
    89    
     109    private SVM.Model StartTraining(SVM.Problem scaledProblem, SVM.Parameter parameter) {
     110      SVM.Model model = null;
     111      lock (locker) {
     112        if (!abortRequested) {
     113          trainingThread = new Thread(() => {
     114              model = SVM.Training.Train(scaledProblem, parameter);
     115          });
     116          trainingThread.Start();
     117        }
     118      }
     119      trainingThread.Join();
     120      trainingThread = null;
     121      return model;
     122    }
    90123  }
    91124}
Note: See TracChangeset for help on using the changeset viewer.