Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/09 11:58:21 (15 years ago)
Author:
gkronber
Message:

Updated LibSVM project to latest version. #774

Location:
trunk/sources/HeuristicLab.SupportVectorMachines/3.2
Files:
6 edited

Legend:

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

    r2414 r2415  
    7979      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex,
    8080        start, end, minTimeOffset, maxTimeOffset);
    81       Problem scaledProblem = SVM.Scaling.Scale(p, transform);
     81      Problem scaledProblem = transform.Scale(p);
    8282
    8383      int targetVariableIndex = input.GetVariableIndex(targetVariable);
     
    155155      }
    156156    }
     157
     158    public static void Export(Predictor p, Stream s) {
     159      StreamWriter writer = new StreamWriter(s);
     160      writer.Write("Targetvariable: "); writer.WriteLine(p.targetVariable);
     161      writer.Write("LowerPredictionLimit: "); writer.WriteLine(p.LowerPredictionLimit.ToString());
     162      writer.Write("UpperPredictionLimit: "); writer.WriteLine(p.UpperPredictionLimit.ToString());
     163      writer.Write("MaxTimeOffset: "); writer.WriteLine(p.MaxTimeOffset.ToString());
     164      writer.Write("MinTimeOffset: "); writer.WriteLine(p.MinTimeOffset.ToString());
     165      writer.Write("InputVariables :");
     166      writer.Write(p.GetInputVariables().First());
     167      foreach (string variable in p.GetInputVariables().Skip(1)) {
     168        writer.Write("; "); writer.Write(variable);
     169      }
     170      writer.WriteLine();
     171      writer.Flush();
     172      using (MemoryStream memStream = new MemoryStream()) {
     173        SVMModel.Export(p.Model, memStream);
     174        memStream.WriteTo(s);
     175      }
     176    }
     177
     178    public static Predictor Import(Stream s) {
     179      Predictor p = new Predictor();
     180      StreamReader reader = new StreamReader(s);
     181      string[] targetVariableLine = reader.ReadLine().Split(':');
     182      string[] lowerPredictionLimitLine = reader.ReadLine().Split(':');
     183      string[] upperPredictionLimitLine = reader.ReadLine().Split(':');
     184      string[] maxTimeOffsetLine = reader.ReadLine().Split(':');
     185      string[] minTimeOffsetLine = reader.ReadLine().Split(':');
     186      string[] inputVariableLine = reader.ReadLine().Split(':', ';');
     187
     188      p.targetVariable = targetVariableLine[1].Trim();
     189      p.LowerPredictionLimit = double.Parse(lowerPredictionLimitLine[1]);
     190      p.UpperPredictionLimit = double.Parse(upperPredictionLimitLine[1]);
     191      p.maxTimeOffset = int.Parse(maxTimeOffsetLine[1]);
     192      p.minTimeOffset = int.Parse(minTimeOffsetLine[1]);
     193      int i = 1;
     194      foreach (string inputVariable in inputVariableLine.Skip(1)) {
     195        p.variableNames[inputVariable.Trim()] = i++;
     196      }
     197      p.svmModel = SVMModel.Import(s);
     198      return p;
     199    }
    157200  }
    158201}
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/PredictorView.cs

    r2413 r2415  
    2929using System.Windows.Forms;
    3030using HeuristicLab.Core;
     31using System.IO;
    3132
    3233namespace HeuristicLab.SupportVectorMachines {
     
    5051    }
    5152
    52     protected override string GetModelString() {
    53       StringBuilder builder = new StringBuilder();
    54       builder.Append("LowerPredictionLimit: ").AppendLine(predictor.LowerPredictionLimit.ToString());
    55       builder.Append("UpperPredictionLimit: ").AppendLine(predictor.UpperPredictionLimit.ToString());
    56       builder.Append("MaxTimeOffset: ").AppendLine(predictor.MaxTimeOffset.ToString());
    57       builder.Append("MinTimeOffset: ").AppendLine(predictor.MinTimeOffset.ToString());
    58       builder.Append("InputVariables :");
    59       builder.Append(predictor.GetInputVariables().First());
    60       foreach (string variable in predictor.GetInputVariables().Skip(1)) {
    61         builder.Append("; ").Append(variable);
     53    protected override void UpdateControls() {
     54      base.UpdateControls();
     55      textBox.Text = GetModelString();
     56    }
     57
     58    private string GetModelString() {
     59      if (predictor == null) return "";
     60      using (MemoryStream s = new MemoryStream()) {
     61        Predictor.Export(predictor, s);
     62        s.Flush();
     63        s.Seek(0, System.IO.SeekOrigin.Begin);
     64        StreamReader reader = new StreamReader(s);
     65        return reader.ReadToEnd();
    6266      }
    63       builder.AppendLine();
    64       builder.Append(base.GetModelString());
    65       return builder.ToString();
    6667    }
    6768  }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMModel.cs

    r2322 r2415  
    116116      }
    117117    }
     118
     119    public static void Export(SVMModel model, Stream s) {
     120      StreamWriter writer = new StreamWriter(s);
     121      writer.WriteLine("RangeTransform:");
     122      using (MemoryStream memStream = new MemoryStream()) {
     123        SVM.RangeTransform.Write(memStream, model.RangeTransform);
     124        memStream.Seek(0, SeekOrigin.Begin);
     125        memStream.WriteTo(s);
     126      }
     127      writer.WriteLine("Model:");
     128
     129      using (MemoryStream memStream = new MemoryStream()) {
     130        SVM.Model.Write(memStream, model.Model);
     131        memStream.Seek(0, SeekOrigin.Begin);
     132        memStream.WriteTo(s);
     133      }
     134      s.Flush();
     135    }
     136
     137    public static SVMModel Import(Stream s) {
     138      SVMModel model = new SVMModel();
     139      StreamReader reader = new StreamReader(s);
     140      while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform";
     141      model.RangeTransform = SVM.RangeTransform.Read(s);
     142
     143      // read until "Model:"
     144      while (reader.ReadLine().Trim() != "Model:") ;
     145      model.Model = SVM.Model.Read(s);
     146      return model;
     147    }
    118148  }
    119149}
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMModelView.cs

    r2413 r2415  
    4949      kernelType.DataBindings.Add(new Binding("Text", model.Model.Parameter, "KernelType"));
    5050      gamma.DataBindings.Add(new Binding("Text", model.Model.Parameter, "Gamma"));
     51      UpdateControls();
    5152    }
    5253
     
    5657    }
    5758
    58     protected virtual string GetModelString() {
    59       StringBuilder builder = new StringBuilder();
    60       builder.AppendLine("RangeTransform:");
    61       using (MemoryStream stream = new MemoryStream()) {
    62         SVM.RangeTransform.Write(stream, model.RangeTransform);
    63         stream.Seek(0, System.IO.SeekOrigin.Begin);
    64         StreamReader reader = new StreamReader(stream);
    65         builder.AppendLine(reader.ReadToEnd());
     59    private string GetModelString() {
     60      using (MemoryStream s = new MemoryStream()) {
     61        SVMModel.Export(model, s);
     62        s.Flush();
     63        s.Seek(0, System.IO.SeekOrigin.Begin);
     64        StreamReader reader = new StreamReader(s);
     65        return reader.ReadToEnd();
    6666      }
    67       builder.AppendLine("Model:");
    68       using (MemoryStream stream = new MemoryStream()) {
    69         SVM.Model.Write(stream, model.Model);
    70         stream.Seek(0, System.IO.SeekOrigin.Begin);
    71         StreamReader reader = new StreamReader(stream);
    72         builder.AppendLine(reader.ReadToEnd());
    73       }
    74       return builder.ToString();
    7567    }
    7668  }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs

    r2349 r2415  
    2828using HeuristicLab.DataAnalysis;
    2929using System.Threading;
     30using SVM;
    3031
    3132namespace HeuristicLab.SupportVectorMachines {
     
    8586      parameter.Nu = GetVariableValue<DoubleData>("SVMNu", scope, true).Data;
    8687      parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
     88      parameter.CacheSize = 500;
     89      parameter.Probability = false;
    8790
    8891      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset);
    89       SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem);
    90       SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform);
     92      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
     93      SVM.Problem scaledProblem = rangeTransform.Scale(problem);
    9194
    9295      SVM.Model model = StartTraining(scaledProblem, parameter);
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs

    r2357 r2415  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.DataAnalysis;
     29using SVM;
    2930
    3031namespace HeuristicLab.SupportVectorMachines {
     
    5657      SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true);
    5758      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset);
    58       SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, modelData.RangeTransform);
     59      SVM.Problem scaledProblem = modelData.RangeTransform.Scale(problem);
    5960
    6061      double[,] values = new double[scaledProblem.Count, 2];
Note: See TracChangeset for help on using the changeset viewer.