Changeset 1848
- Timestamp:
- 05/18/09 23:16:54 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs
r1837 r1848 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.DataAnalysis; 29 using System.Threading; 29 30 30 31 namespace HeuristicLab.SupportVectorMachines { 31 32 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 } 32 42 33 43 public SupportVectorCreator() … … 50 60 AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.New | VariableKind.Out)); 51 61 AddVariableInfo(new VariableInfo("SVMRangeTransform", "The applied transformation during the learning the model", typeof(SVMRangeTransform), VariableKind.New | VariableKind.Out)); 62 } 52 63 64 public override void Abort() { 65 abortRequested = true; 66 lock (locker) { 67 if (trainingThread != null && trainingThread.ThreadState == ThreadState.Running) { 68 trainingThread.Abort(); 69 } 70 } 53 71 } 54 72 55 73 public override IOperation Apply(IScope scope) { 74 abortRequested = false; 56 75 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 57 76 ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true); … … 74 93 SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem); 75 94 SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform); 76 SVM.Model model = SVM.Training.Train(scaledProblem, parameter);77 95 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 } 86 106 return null; 87 107 } 88 108 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 } 90 123 } 91 124 }
Note: See TracChangeset
for help on using the changeset viewer.