Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/13 13:13:41 (12 years ago)
Author:
spimming
Message:

#1888:

  • Merged revisions from trunk
Location:
branches/OaaS
Files:
35 edited
11 copied

Legend:

Unmodified
Added
Removed
  • branches/OaaS

  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis

  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.user
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/CrossValidation.cs

    r7738 r9363  
    2424using System.Drawing;
    2525using System.Linq;
     26using System.Threading;
    2627using HeuristicLab.Collections;
    2728using HeuristicLab.Common;
     
    4445
    4546      executionState = ExecutionState.Stopped;
    46       runs = new RunCollection();
     47      runs = new RunCollection { OptimizerName = name };
    4748      runsCounter = 0;
    4849
     
    119120          }
    120121          OnAlgorithmChanged();
    121           if (algorithm != null) OnProblemChanged();
    122122          Prepare();
    123123        }
     
    245245    }
    246246    #endregion
     247
     248    protected override void OnNameChanged() {
     249      base.OnNameChanged();
     250      Runs.OptimizerName = Name;
     251    }
    247252
    248253    public void Prepare() {
     
    306311            if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared ||
    307312                clonedAlgorithm.ExecutionState == ExecutionState.Paused) {
    308               clonedAlgorithm.Start();
    309               startedAlgorithms++;
     313
     314              // start and wait until the alg is started
     315              using (var signal = new ManualResetEvent(false)) {
     316                EventHandler signalSetter = (sender, args) => { signal.Set(); };
     317                clonedAlgorithm.Started += signalSetter;
     318                clonedAlgorithm.Start();
     319                signal.WaitOne();
     320                clonedAlgorithm.Started -= signalSetter;
     321
     322                startedAlgorithms++;
     323              }
    310324            }
    311325          }
     
    447461        problemDataClone.TestPartition.Start = SamplesStart.Value; problemDataClone.TestPartition.End = SamplesEnd.Value;
    448462        // clone models
    449         var ensembleSolution = new ClassificationEnsembleSolution(
    450           solutions.Value.Select(x => cloner.Clone(x.Model)),
    451           problemDataClone,
    452           solutions.Value.Select(x => cloner.Clone(x.ProblemData.TrainingPartition)),
    453           solutions.Value.Select(x => cloner.Clone(x.ProblemData.TestPartition)));
     463        var ensembleSolution = new ClassificationEnsembleSolution(problemDataClone);
     464        ensembleSolution.AddClassificationSolutions(solutions.Value);
    454465
    455466        aggregatedResults.Add(new Result(solutions.Key + " (ensemble)", ensembleSolution));
     
    509520    private void RegisterEvents() {
    510521      Folds.ValueChanged += new EventHandler(Folds_ValueChanged);
    511       SamplesStart.ValueChanged += new EventHandler(SamplesStart_ValueChanged);
    512       SamplesEnd.ValueChanged += new EventHandler(SamplesEnd_ValueChanged);
    513522      RegisterClonedAlgorithmsEvents();
    514523    }
     
    518527    }
    519528
    520     private bool samplesChanged = false;
    521     private void SamplesStart_ValueChanged(object sender, EventArgs e) {
    522       samplesChanged = true;
    523       if (Problem != null) Problem.ProblemData.TrainingPartition.Start = SamplesStart.Value;
    524       samplesChanged = false;
    525     }
    526     private void SamplesEnd_ValueChanged(object sender, EventArgs e) {
    527       samplesChanged = true;
    528       if (Problem != null) Problem.ProblemData.TrainingPartition.End = SamplesEnd.Value;
    529       samplesChanged = false;
    530     }
    531529
    532530    #region template algorithms events
     
    541539      algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
    542540      algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
     541      if (Problem != null) Problem.Reset += new EventHandler(Problem_Reset);
    543542    }
    544543    private void DeregisterAlgorithmEvents() {
    545544      algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
    546545      algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
     546      if (Problem != null) Problem.Reset -= new EventHandler(Problem_Reset);
    547547    }
    548548    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
     
    551551        throw new ArgumentException("A cross validation algorithm can only contain DataAnalysisProblems.");
    552552      }
    553       algorithm.Problem.Reset += (x, y) => OnProblemChanged();
     553      if (problem != null) problem.Reset -= new EventHandler(Problem_Reset);
     554      if (Problem != null) Problem.Reset += new EventHandler(Problem_Reset);
    554555      problem = (IDataAnalysisProblem)algorithm.Problem;
    555556      OnProblemChanged();
     
    559560      EventHandler handler = ProblemChanged;
    560561      if (handler != null) handler(this, EventArgs.Empty);
    561       if (samplesChanged) return;
    562 
     562      ConfigureProblem();
     563    }
     564
     565    private void Problem_Reset(object sender, EventArgs e) {
     566      ConfigureProblem();
     567    }
     568
     569    private void ConfigureProblem() {
    563570      SamplesStart.Value = 0;
    564571      if (Problem != null) {
     
    581588      } else
    582589        SamplesEnd.Value = 0;
    583 
    584       SamplesStart_ValueChanged(this, EventArgs.Empty);
    585       SamplesEnd_ValueChanged(this, EventArgs.Empty);
    586590    }
    587591
     
    677681      lock (locker) {
    678682        if (!stopPending && ExecutionState == ExecutionState.Started) {
    679           IAlgorithm preparedAlgorithm = clonedAlgorithms.Where(alg => alg.ExecutionState == ExecutionState.Prepared ||
    680                                                                        alg.ExecutionState == ExecutionState.Paused).FirstOrDefault();
     683          IAlgorithm preparedAlgorithm = clonedAlgorithms.FirstOrDefault(alg => alg.ExecutionState == ExecutionState.Prepared ||
     684                                                                                alg.ExecutionState == ExecutionState.Paused);
    681685          if (preparedAlgorithm != null) preparedAlgorithm.Start();
    682686        }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs

    r7259 r9363  
    9696    private void Run(object state) {
    9797      CancellationToken cancellationToken = (CancellationToken)state;
    98       lastUpdateTime = DateTime.Now;
     98      lastUpdateTime = DateTime.UtcNow;
    9999      System.Timers.Timer timer = new System.Timers.Timer(250);
    100100      timer.AutoReset = true;
     
    107107        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    108108        timer.Stop();
    109         ExecutionTime += DateTime.Now - lastUpdateTime;
     109        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    110110      }
    111111
     
    121121      System.Timers.Timer timer = (System.Timers.Timer)sender;
    122122      timer.Enabled = false;
    123       DateTime now = DateTime.Now;
     123      DateTime now = DateTime.UtcNow;
    124124      ExecutionTime += now - lastUpdateTime;
    125125      lastUpdateTime = now;
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r7825 r9363  
    101101  </PropertyGroup>
    102102  <ItemGroup>
    103     <Reference Include="ALGLIB-3.5.0, Version=3.5.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    104       <HintPath>..\..\bin\ALGLIB-3.5.0.dll</HintPath>
     103    <Reference Include="ALGLIB-3.7.0, Version=3.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     104      <HintPath>..\..\bin\ALGLIB-3.7.0.dll</HintPath>
    105105      <Private>False</Private>
    106106    </Reference>
    107     <Reference Include="LibSVM-1.6.3, Version=1.6.3.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    108       <HintPath>..\..\bin\LibSVM-1.6.3.dll</HintPath>
     107    <Reference Include="AutoDiff-1.0, Version=1.0.0.14388, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     108      <SpecificVersion>False</SpecificVersion>
     109      <HintPath>..\..\bin\AutoDiff-1.0.dll</HintPath>
     110    </Reference>
     111    <Reference Include="LibSVM-3.12, Version=3.12.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     112      <HintPath>..\..\bin\LibSVM-3.12.dll</HintPath>
    109113      <Private>False</Private>
    110114    </Reference>
     
    113117      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    114118    </Reference>
    115     <Reference Include="System.Data" />
    116119    <Reference Include="System.Drawing" />
    117     <Reference Include="System.Xml" />
    118120  </ItemGroup>
    119121  <ItemGroup>
     
    122124    </Compile>
    123125    <Compile Include="FixedDataAnalysisAlgorithm.cs" />
     126    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceNeuralNetwork.cs" />
     127    <Compile Include="GaussianProcess\GaussianProcessBase.cs" />
     128    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceConst.cs">
     129      <SubType>Code</SubType>
     130    </Compile>
     131    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceLinear.cs">
     132      <SubType>Code</SubType>
     133    </Compile>
     134    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceLinearArd.cs">
     135      <SubType>Code</SubType>
     136    </Compile>
     137    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceMask.cs">
     138      <SubType>Code</SubType>
     139    </Compile>
     140    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceMaternIso.cs">
     141      <SubType>Code</SubType>
     142    </Compile>
     143    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceNoise.cs">
     144      <SubType>Code</SubType>
     145    </Compile>
     146    <Compile Include="GaussianProcess\CovarianceFunctions\CovariancePeriodic.cs">
     147      <SubType>Code</SubType>
     148    </Compile>
     149    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceProduct.cs">
     150      <SubType>Code</SubType>
     151    </Compile>
     152    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceRationalQuadraticArd.cs">
     153      <SubType>Code</SubType>
     154    </Compile>
     155    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceRationalQuadraticIso.cs">
     156      <SubType>Code</SubType>
     157    </Compile>
     158    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceScale.cs">
     159      <SubType>Code</SubType>
     160    </Compile>
     161    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSquaredExponentialArd.cs">
     162      <SubType>Code</SubType>
     163    </Compile>
     164    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSquaredExponentialIso.cs" />
     165    <Compile Include="GaussianProcess\CovarianceFunctions\CovarianceSum.cs">
     166      <SubType>Code</SubType>
     167    </Compile>
     168    <Compile Include="GaussianProcess\GaussianProcessClassificationSolutionCreator.cs" />
     169    <Compile Include="GaussianProcess\GaussianProcessClassificationModelCreator.cs" />
     170    <Compile Include="GaussianProcess\GaussianProcessClassification.cs" />
     171    <Compile Include="GaussianProcess\MeanFunctions\MeanConst.cs" />
     172    <Compile Include="GaussianProcess\MeanFunctions\MeanLinear.cs" />
     173    <Compile Include="GaussianProcess\MeanFunctions\MeanProduct.cs">
     174      <SubType>Code</SubType>
     175    </Compile>
     176    <Compile Include="GaussianProcess\MeanFunctions\MeanSum.cs">
     177      <SubType>Code</SubType>
     178    </Compile>
     179    <Compile Include="GaussianProcess\MeanFunctions\MeanZero.cs" />
     180    <Compile Include="GaussianProcess\GaussianProcessHyperparameterInitializer.cs" />
     181    <Compile Include="GaussianProcess\GaussianProcessRegressionSolutionCreator.cs" />
     182    <Compile Include="GaussianProcess\GaussianProcessRegressionModelCreator.cs" />
     183    <Compile Include="GaussianProcess\GaussianProcessModelCreator.cs">
     184      <SubType>Code</SubType>
     185    </Compile>
     186    <Compile Include="GaussianProcess\Util.cs" />
     187    <Compile Include="GaussianProcess\IMeanFunction.cs" />
     188    <Compile Include="GaussianProcess\GaussianProcessModel.cs" />
     189    <Compile Include="GaussianProcess\GaussianProcessRegression.cs" />
     190    <Compile Include="GaussianProcess\GaussianProcessRegressionSolution.cs" />
     191    <Compile Include="GaussianProcess\ICovarianceFunction.cs" />
     192    <Compile Include="Interfaces\IGaussianProcessClassificationModelCreator.cs" />
     193    <Compile Include="Interfaces\IGaussianProcessRegressionModelCreator.cs" />
     194    <Compile Include="Interfaces\IGaussianProcessModelCreator.cs" />
     195    <Compile Include="Interfaces\IGaussianProcessModel.cs" />
     196    <Compile Include="Interfaces\IGaussianProcessSolution.cs" />
     197    <Compile Include="Interfaces\INcaClassificationSolution.cs" />
     198    <Compile Include="Interfaces\INcaModel.cs" />
    124199    <Compile Include="Interfaces\INearestNeighbourClassificationSolution.cs" />
    125200    <Compile Include="Interfaces\INearestNeighbourRegressionSolution.cs" />
     
    144219    </Compile>
    145220    <Compile Include="Linear\AlglibUtil.cs" />
     221    <Compile Include="Linear\Scaling.cs" />
    146222    <Compile Include="Linear\LinearDiscriminantAnalysis.cs" />
    147223    <Compile Include="Linear\LinearRegression.cs">
     
    151227    <Compile Include="Linear\MultinomialLogitClassificationSolution.cs" />
    152228    <Compile Include="Linear\MultinomialLogitModel.cs" />
     229    <Compile Include="Nca\Initialization\INcaInitializer.cs" />
     230    <Compile Include="Nca\Initialization\LdaInitializer.cs" />
     231    <Compile Include="Nca\Initialization\NcaInitializer.cs" />
     232    <Compile Include="Nca\Initialization\PcaInitializer.cs" />
     233    <Compile Include="Nca\Initialization\RandomInitializer.cs" />
     234    <Compile Include="Nca\Matrix.cs" />
     235    <Compile Include="Nca\ModelCreation\INcaModelCreator.cs" />
     236    <Compile Include="Nca\ModelCreation\NcaModelCreator.cs" />
     237    <Compile Include="Nca\NcaAlgorithm.cs" />
     238    <Compile Include="Nca\NcaClassificationSolution.cs" />
     239    <Compile Include="Nca\NcaGradientCalculator.cs" />
     240    <Compile Include="Nca\NcaModel.cs" />
     241    <Compile Include="Nca\SolutionCreation\INcaSolutionCreator.cs" />
     242    <Compile Include="Nca\SolutionCreation\NcaSolutionCreator.cs" />
    153243    <Compile Include="NearestNeighbour\NearestNeighbourClassification.cs" />
    154244    <Compile Include="NearestNeighbour\NearestNeighbourClassificationSolution.cs" />
     
    185275      <SubType>Code</SubType>
    186276    </Compile>
     277    <Compile Include="TimeSeries\AutoregressiveModeling.cs" />
    187278  </ItemGroup>
    188279  <ItemGroup>
     280    <ProjectReference Include="..\..\HeuristicLab.Algorithms.GradientDescent\3.3\HeuristicLab.Algorithms.GradientDescent-3.3.csproj">
     281      <Project>{1256B945-EEA9-4BE4-9880-76B5B113F089}</Project>
     282      <Name>HeuristicLab.Algorithms.GradientDescent</Name>
     283      <Private>False</Private>
     284    </ProjectReference>
     285    <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">
     286      <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>
     287      <Name>HeuristicLab.Analysis-3.3</Name>
     288      <Private>False</Private>
     289    </ProjectReference>
    189290    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
    190291      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     
    212313      <Private>False</Private>
    213314    </ProjectReference>
     315    <ProjectReference Include="..\..\HeuristicLab.Encodings.RealVectorEncoding\3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj">
     316      <Project>{BB6D334A-4BB6-4674-9883-31A6EBB32CAB}</Project>
     317      <Name>HeuristicLab.Encodings.RealVectorEncoding-3.3</Name>
     318      <Private>False</Private>
     319    </ProjectReference>
    214320    <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
    215321      <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
     
    217323      <Private>False</Private>
    218324    </ProjectReference>
     325    <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
     326      <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
     327      <Name>HeuristicLab.Operators-3.3</Name>
     328      <Private>False</Private>
     329    </ProjectReference>
    219330    <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
    220331      <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>
     
    247358      <Private>False</Private>
    248359    </ProjectReference>
     360    <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4.csproj">
     361      <Project>{07486E68-1517-4B9D-A58D-A38E99AE71AB}</Project>
     362      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4</Name>
     363    </ProjectReference>
    249364    <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj">
    250365      <Project>{3D28463F-EC96-4D82-AFEE-38BE91A0CA00}</Project>
     
    260375      <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project>
    261376      <Name>HeuristicLab.Problems.Instances-3.3</Name>
     377      <Private>False</Private>
     378    </ProjectReference>
     379    <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
     380      <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
     381      <Name>HeuristicLab.Random-3.3</Name>
    262382      <Private>False</Private>
    263383    </ProjectReference>
     
    294414  -->
    295415  <PropertyGroup>
    296     <PreBuildEvent>set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     416    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    297417set ProjectDir=$(ProjectDir)
    298418set SolutionDir=$(SolutionDir)
     
    301421call PreBuildEvent.cmd
    302422</PreBuildEvent>
     423    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     424export ProjectDir=$(ProjectDir)
     425export SolutionDir=$(SolutionDir)
     426
     427$SolutionDir/PreBuildEvent.sh
     428</PreBuildEvent>
    303429  </PropertyGroup>
    304430</Project>
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/ISupportVectorMachineModel.cs

    r7259 r9363  
    2020#endregion
    2121
    22 using HeuristicLab.Optimization;
    2322using HeuristicLab.Problems.DataAnalysis;
    24 using HeuristicLab.Core;
    25 using System.Collections.Generic;
     23using LibSVM;
    2624
    2725namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3028  /// </summary>
    3129  public interface ISupportVectorMachineModel : IDataAnalysisModel, IRegressionModel, IClassificationModel {
    32     SVM.Model Model { get; }
    33     SVM.RangeTransform RangeTransform { get; }
     30    svm_model Model { get; }
     31    RangeTransform RangeTransform { get; }
    3432    Dataset SupportVectors { get; }
    3533  }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs

    r7259 r9363  
    4545      return matrix;
    4646    }
     47    public static double[,] PrepareAndScaleInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows, Scaling scaling) {
     48      List<string> variablesList = variables.ToList();
     49      List<int> rowsList = rows.ToList();
     50
     51      double[,] matrix = new double[rowsList.Count, variablesList.Count];
     52
     53      int col = 0;
     54      foreach (string column in variables) {
     55        var values = scaling.GetScaledValues(dataset, column, rows);
     56        int row = 0;
     57        foreach (var value in values) {
     58          matrix[row, col] = value;
     59          row++;
     60        }
     61        col++;
     62      }
     63
     64      return matrix;
     65    }
    4766  }
    4867}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs

    r8139 r9363  
    111111      IClassificationProblemData problemData,
    112112      IEnumerable<int> rows) {
    113       return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
     113      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, new AccuracyMaximizationThresholdCalculator());
     114      model.RecalculateModelParameters(problemData, rows);
     115      return model;
    114116    }
    115117  }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitClassificationSolution.cs

    r7259 r9363  
    4545    public MultinomialLogitClassificationSolution(IClassificationProblemData problemData, MultinomialLogitModel logitModel)
    4646      : base(logitModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new MultinomialLogitClassificationSolution(this, cloner);
    5251    }
    53 
    54     protected override void RecalculateResults() {
    55       CalculateResults();
    56     }
    5752  }
    5853}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/MultinomialLogitModel.cs

    r7259 r9363  
    109109
    110110    public MultinomialLogitClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    111       return new MultinomialLogitClassificationSolution(problemData, this);
     111      return new MultinomialLogitClassificationSolution(new ClassificationProblemData(problemData), this);
    112112    }
    113113    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs

    r8139 r9363  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
    2726using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2927using HeuristicLab.Optimization;
     28using HeuristicLab.Parameters;
    3029using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3130using HeuristicLab.Problems.DataAnalysis;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    34 using HeuristicLab.Parameters;
    3531
    3632namespace HeuristicLab.Algorithms.DataAnalysis {
     
    8480
    8581    public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k) {
    86       Dataset dataset = problemData.Dataset;
    87       string targetVariable = problemData.TargetVariable;
    88       IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
    89       IEnumerable<int> rows = problemData.TrainingIndices;
    90       double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
    91       if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
    92         throw new NotSupportedException("Nearest neighbour classification does not support NaN or infinity values in the input dataset.");
     82      var problemDataClone = (IClassificationProblemData)problemData.Clone();
     83      return new NearestNeighbourClassificationSolution(problemDataClone, Train(problemDataClone, k));
     84    }
    9385
    94       alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree();
    95 
    96       int nRows = inputMatrix.GetLength(0);
    97       int nFeatures = inputMatrix.GetLength(1) - 1;
    98       double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray();
    99       int nClasses = classValues.Count();
    100       // map original class values to values [0..nClasses-1]
    101       Dictionary<double, double> classIndices = new Dictionary<double, double>();
    102       for (int i = 0; i < nClasses; i++) {
    103         classIndices[classValues[i]] = i;
    104       }
    105       for (int row = 0; row < nRows; row++) {
    106         inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]];
    107       }
    108       alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree);
    109       var problemDataClone = (IClassificationProblemData) problemData.Clone();
    110       return new NearestNeighbourClassificationSolution(problemDataClone, new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables, problemDataClone.ClassValues.ToArray()));
     86    public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k) {
     87      return new NearestNeighbourModel(problemData.Dataset,
     88        problemData.TrainingIndices,
     89        k,
     90        problemData.TargetVariable,
     91        problemData.AllowedInputVariables,
     92        problemData.ClassValues.ToArray());
    11193    }
    11294    #endregion
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassificationSolution.cs

    r7259 r9363  
    4545    public NearestNeighbourClassificationSolution(IClassificationProblemData problemData, INearestNeighbourModel nnModel)
    4646      : base(nnModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new NearestNeighbourClassificationSolution(this, cloner);
    5251    }
    53 
    54     protected override void RecalculateResults() {
    55       CalculateResults();
    56     }
    5752  }
    5853}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs

    r7294 r9363  
    3333  /// </summary>
    3434  [StorableClass]
    35   [Item("NearestNeighbourModel", "Represents a neural network for regression and classification.")]
     35  [Item("NearestNeighbourModel", "Represents a nearest neighbour model for regression and classification.")]
    3636  public sealed class NearestNeighbourModel : NamedItem, INearestNeighbourModel {
    3737
     
    5656    [Storable]
    5757    private int k;
     58
    5859    [StorableConstructor]
    5960    private NearestNeighbourModel(bool deserializing)
     
    9596        this.classValues = (double[])original.classValues.Clone();
    9697    }
    97     public NearestNeighbourModel(alglib.nearestneighbor.kdtree kdTree, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
    98       : base() {
    99       this.name = ItemName;
    100       this.description = ItemDescription;
    101       this.kdTree = kdTree;
     98    public NearestNeighbourModel(Dataset dataset, IEnumerable<int> rows, int k, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null) {
     99      Name = ItemName;
     100      Description = ItemDescription;
    102101      this.k = k;
    103102      this.targetVariable = targetVariable;
    104103      this.allowedInputVariables = allowedInputVariables.ToArray();
    105       if (classValues != null)
     104
     105      var inputMatrix = AlglibUtil.PrepareInputMatrix(dataset,
     106                                   allowedInputVariables.Concat(new string[] { targetVariable }),
     107                                   rows);
     108
     109      if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
     110        throw new NotSupportedException(
     111          "Nearest neighbour classification does not support NaN or infinity values in the input dataset.");
     112
     113      this.kdTree = new alglib.nearestneighbor.kdtree();
     114
     115      var nRows = inputMatrix.GetLength(0);
     116      var nFeatures = inputMatrix.GetLength(1) - 1;
     117
     118      if (classValues != null) {
    106119        this.classValues = (double[])classValues.Clone();
     120        int nClasses = classValues.Length;
     121        // map original class values to values [0..nClasses-1]
     122        var classIndices = new Dictionary<double, double>();
     123        for (int i = 0; i < nClasses; i++)
     124          classIndices[classValues[i]] = i;
     125
     126        for (int row = 0; row < nRows; row++) {
     127          inputMatrix[row, nFeatures] = classIndices[inputMatrix[row, nFeatures]];
     128        }
     129      }
     130      alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdTree);
    107131    }
    108132
     
    140164
    141165    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
     166      if (classValues == null) throw new InvalidOperationException("No class values are defined.");
    142167      double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
    143168
     
    177202
    178203    public INearestNeighbourRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    179       return new NearestNeighbourRegressionSolution(problemData, this);
     204      return new NearestNeighbourRegressionSolution(new RegressionProblemData(problemData), this);
    180205    }
    181206    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    183208    }
    184209    public INearestNeighbourClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    185       return new NearestNeighbourClassificationSolution(problemData, this);
     210      return new NearestNeighbourClassificationSolution(new ClassificationProblemData(problemData), this);
    186211    }
    187212    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs

    r8139 r9363  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Core;
    2725using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2926using HeuristicLab.Optimization;
     27using HeuristicLab.Parameters;
    3028using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3129using HeuristicLab.Problems.DataAnalysis;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    34 using HeuristicLab.Parameters;
    3530
    3631namespace HeuristicLab.Algorithms.DataAnalysis {
     
    8479
    8580    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) {
    86       Dataset dataset = problemData.Dataset;
    87       string targetVariable = problemData.TargetVariable;
    88       IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
    89       IEnumerable<int> rows = problemData.TrainingIndices;
    90       double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
    91       if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
    92         throw new NotSupportedException("Nearest neighbour regression does not support NaN or infinity values in the input dataset.");
     81      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
     82      return new NearestNeighbourRegressionSolution(clonedProblemData, Train(problemData, k));
     83    }
    9384
    94       alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree();
    95 
    96       int nRows = inputMatrix.GetLength(0);
    97 
    98       alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree);
    99 
    100       return new NearestNeighbourRegressionSolution((IRegressionProblemData)problemData.Clone(), new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables));
     85    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k) {
     86      return new NearestNeighbourModel(problemData.Dataset,
     87        problemData.TrainingIndices,
     88        k,
     89        problemData.TargetVariable,
     90        problemData.AllowedInputVariables);
    10191    }
    10292    #endregion
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegressionSolution.cs

    r7259 r9363  
    4545    public NearestNeighbourRegressionSolution(IRegressionProblemData problemData, INearestNeighbourModel nnModel)
    4646      : base(nnModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassificationSolution.cs

    r7259 r9363  
    4545    public NeuralNetworkClassificationSolution(IClassificationProblemData problemData, INeuralNetworkModel nnModel)
    4646      : base(nnModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new NeuralNetworkClassificationSolution(this, cloner);
    5251    }
    53     protected override void RecalculateResults() {
    54       CalculateResults();
    55     }
     52
    5653  }
    5754}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleClassificationSolution.cs

    r7259 r9363  
    4545    public NeuralNetworkEnsembleClassificationSolution(IClassificationProblemData problemData, INeuralNetworkEnsembleModel nnModel)
    4646      : base(nnModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new NeuralNetworkEnsembleClassificationSolution(this, cloner);
    5251    }
    53 
    54     protected override void RecalculateResults() {
    55       CalculateResults();
    56     }
    5752  }
    5853}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs

    r7694 r9363  
    130130
    131131    public INeuralNetworkEnsembleRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    132       return new NeuralNetworkEnsembleRegressionSolution(problemData, this);
     132      return new NeuralNetworkEnsembleRegressionSolution(new RegressionEnsembleProblemData(problemData), this);
    133133    }
    134134    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    136136    }
    137137    public INeuralNetworkEnsembleClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    138       return new NeuralNetworkEnsembleClassificationSolution(problemData, this);
     138      return new NeuralNetworkEnsembleClassificationSolution(new ClassificationEnsembleProblemData(problemData), this);
    139139    }
    140140    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs

    r7259 r9363  
    138138
    139139    public INeuralNetworkRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    140       return new NeuralNetworkRegressionSolution(problemData, this);
     140      return new NeuralNetworkRegressionSolution(new RegressionProblemData(problemData), this);
    141141    }
    142142    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    144144    }
    145145    public INeuralNetworkClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    146       return new NeuralNetworkClassificationSolution(problemData, this);
     146      return new NeuralNetworkClassificationSolution(new ClassificationProblemData(problemData), this);
    147147    }
    148148    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegression.cs

    r8139 r9363  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2928using HeuristicLab.Optimization;
     29using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    34 using HeuristicLab.Parameters;
    3532
    3633namespace HeuristicLab.Algorithms.DataAnalysis {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegressionSolution.cs

    r7259 r9363  
    4545    public NeuralNetworkRegressionSolution(IRegressionProblemData problemData, INeuralNetworkModel nnModel)
    4646      : base(nnModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Plugin.cs.frame

    r7943 r9363  
    2626  /// Plugin class for HeuristicLab.Algorithms.DataAnalysis plugin.
    2727  /// </summary>
    28   [Plugin("HeuristicLab.Algorithms.DataAnalysis", "Provides wrappers for data analysis algorithms implemented in external libraries (linear regression, linear discriminant analysis, k-means clustering, support vector classification and regression)", "3.4.2.$WCREV$")]
     28  [Plugin("HeuristicLab.Algorithms.DataAnalysis", "Provides wrappers for data analysis algorithms implemented in external libraries (linear regression, linear discriminant analysis, k-means clustering, support vector classification and regression)", "3.4.3.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Algorithms.DataAnalysis-3.4.dll", PluginFileType.Assembly)]
    30   [PluginDependency("HeuristicLab.ALGLIB", "3.5.0")]
    31   [PluginDependency("HeuristicLab.LibSVM", "1.6.3")]
     30  [PluginDependency("HeuristicLab.ALGLIB", "3.7.0")]
     31  [PluginDependency("HeuristicLab.AutoDiff", "1.0.0")]
     32  [PluginDependency("HeuristicLab.Algorithms.GradientDescent", "3.3")]
     33  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    3234  [PluginDependency("HeuristicLab.Collections", "3.3")]
    3335  [PluginDependency("HeuristicLab.Common", "3.3")]
     
    3537  [PluginDependency("HeuristicLab.Core", "3.3")]
    3638  [PluginDependency("HeuristicLab.Data", "3.3")]
     39  [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]
    3740  [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")]
     41  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3842  [PluginDependency("HeuristicLab.Optimization", "3.3")]
    3943  [PluginDependency("HeuristicLab.Parameters", "3.3")]
     
    4347  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification", "3.4")]
    4448  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")]
     49  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis", "3.4")]
     50  [PluginDependency("HeuristicLab.LibSVM", "3.12")]
     51  [PluginDependency("HeuristicLab.Random", "3.3")]
    4552  public class HeuristicLabAlgorithmsDataAnalysisPlugin : PluginBase {
    4653  }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/Properties/AssemblyInfo.cs.frame

    r7259 r9363  
    5353// by using the '*' as shown below:
    5454[assembly: AssemblyVersion("3.4.0.0")]
    55 [assembly: AssemblyFileVersion("3.4.2.$WCREV$")]
     55[assembly: AssemblyFileVersion("3.4.3.$WCREV$")]
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs

    r8139 r9363  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2928using HeuristicLab.Optimization;
     29using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    34 using HeuristicLab.Parameters;
    3532
    3633namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4542    private const string NumberOfTreesParameterName = "Number of trees";
    4643    private const string RParameterName = "R";
     44    private const string MParameterName = "M";
     45    private const string SeedParameterName = "Seed";
     46    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
     47
    4748    #region parameter properties
    48     public IValueParameter<IntValue> NumberOfTreesParameter {
    49       get { return (IValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
     49    public IFixedValueParameter<IntValue> NumberOfTreesParameter {
     50      get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
    5051    }
    51     public IValueParameter<DoubleValue> RParameter {
    52       get { return (IValueParameter<DoubleValue>)Parameters[RParameterName]; }
     52    public IFixedValueParameter<DoubleValue> RParameter {
     53      get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
     54    }
     55    public IFixedValueParameter<DoubleValue> MParameter {
     56      get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
     57    }
     58    public IFixedValueParameter<IntValue> SeedParameter {
     59      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
     60    }
     61    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
     62      get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
    5363    }
    5464    #endregion
     
    6272      set { RParameter.Value.Value = value; }
    6373    }
     74    public double M {
     75      get { return MParameter.Value.Value; }
     76      set { MParameter.Value.Value = value; }
     77    }
     78    public int Seed {
     79      get { return SeedParameter.Value.Value; }
     80      set { SeedParameter.Value.Value = value; }
     81    }
     82    public bool SetSeedRandomly {
     83      get { return SetSeedRandomlyParameter.Value.Value; }
     84      set { SetSeedRandomlyParameter.Value.Value = value; }
     85    }
    6486    #endregion
     87
    6588    [StorableConstructor]
    6689    private RandomForestClassification(bool deserializing) : base(deserializing) { }
     
    6891      : base(original, cloner) {
    6992    }
     93
    7094    public RandomForestClassification()
    7195      : base() {
    7296      Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50)));
    7397      Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "The ratio of the training set that will be used in the construction of individual trees (0<r<=1). Should be adjusted depending on the noise level in the dataset in the range from 0.66 (low noise) to 0.05 (high noise). This parameter should be adjusted to achieve good generalization error.", new DoubleValue(0.3)));
     98      Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
     99      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     100      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    74101      Problem = new ClassificationProblem();
    75102    }
     103
    76104    [StorableHook(HookType.AfterDeserialization)]
    77     private void AfterDeserialization() { }
     105    private void AfterDeserialization() {
     106      if (!Parameters.ContainsKey(MParameterName))
     107        Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
     108      if (!Parameters.ContainsKey(SeedParameterName))
     109        Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     110      if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
     111        Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
     112    }
    78113
    79114    public override IDeepCloneable Clone(Cloner cloner) {
     
    84119    protected override void Run() {
    85120      double rmsError, relClassificationError, outOfBagRmsError, outOfBagRelClassificationError;
    86       var solution = CreateRandomForestClassificationSolution(Problem.ProblemData, NumberOfTrees, R, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError);
     121      if (SetSeedRandomly) Seed = new System.Random().Next();
     122
     123      var solution = CreateRandomForestClassificationSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError);
    87124      Results.Add(new Result(RandomForestClassificationModelResultName, "The random forest classification solution.", solution));
    88125      Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError)));
     
    92129    }
    93130
    94     public static IClassificationSolution CreateRandomForestClassificationSolution(IClassificationProblemData problemData, int nTrees, double r,
     131    public static IClassificationSolution CreateRandomForestClassificationSolution(IClassificationProblemData problemData, int nTrees, double r, double m, int seed,
    95132      out double rmsError, out double relClassificationError, out double outOfBagRmsError, out double outOfBagRelClassificationError) {
     133      if (r <= 0 || r > 1) throw new ArgumentException("The R parameter in the random forest regression must be between 0 and 1.");
     134      if (m <= 0 || m > 1) throw new ArgumentException("The M parameter in the random forest regression must be between 0 and 1.");
     135
     136      alglib.math.rndobject = new System.Random(seed);
     137
    96138      Dataset dataset = problemData.Dataset;
    97139      string targetVariable = problemData.TargetVariable;
     
    102144        throw new NotSupportedException("Random forest classification does not support NaN or infinity values in the input dataset.");
    103145
     146      int info = 0;
     147      alglib.decisionforest dForest = new alglib.decisionforest();
     148      alglib.dfreport rep = new alglib.dfreport(); ;
     149      int nRows = inputMatrix.GetLength(0);
     150      int nColumns = inputMatrix.GetLength(1);
     151      int sampleSize = Math.Max((int)Math.Round(r * nRows), 1);
     152      int nFeatures = Math.Max((int)Math.Round(m * (nColumns - 1)), 1);
    104153
    105       alglib.decisionforest dforest;
    106       alglib.dfreport rep;
    107       int nRows = inputMatrix.GetLength(0);
    108       int nCols = inputMatrix.GetLength(1);
    109       int info;
    110       double[] classValues = dataset.GetDoubleValues(targetVariable).Distinct().OrderBy(x => x).ToArray();
    111       int nClasses = classValues.Count();
     154
     155      double[] classValues = problemData.ClassValues.ToArray();
     156      int nClasses = problemData.Classes;
    112157      // map original class values to values [0..nClasses-1]
    113158      Dictionary<double, double> classIndices = new Dictionary<double, double>();
     
    116161      }
    117162      for (int row = 0; row < nRows; row++) {
    118         inputMatrix[row, nCols - 1] = classIndices[inputMatrix[row, nCols - 1]];
     163        inputMatrix[row, nColumns - 1] = classIndices[inputMatrix[row, nColumns - 1]];
    119164      }
    120       // execute random forest algorithm
    121       alglib.dfbuildrandomdecisionforest(inputMatrix, nRows, nCols - 1, nClasses, nTrees, r, out info, out dforest, out rep);
     165      // execute random forest algorithm     
     166      alglib.dforest.dfbuildinternal(inputMatrix, nRows, nColumns - 1, nClasses, nTrees, sampleSize, nFeatures, alglib.dforest.dfusestrongsplits + alglib.dforest.dfuseevs, ref info, dForest.innerobj, rep.innerobj);
    122167      if (info != 1) throw new ArgumentException("Error in calculation of random forest classification solution");
    123168
     
    126171      relClassificationError = rep.relclserror;
    127172      outOfBagRelClassificationError = rep.oobrelclserror;
    128       return new RandomForestClassificationSolution((IClassificationProblemData)problemData.Clone(), new RandomForestModel(dforest, targetVariable, allowedInputVariables, classValues));
     173      return new RandomForestClassificationSolution((IClassificationProblemData)problemData.Clone(), new RandomForestModel(dForest, targetVariable, allowedInputVariables, classValues));
    129174    }
    130175    #endregion
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs

    r7259 r9363  
    4545    public RandomForestClassificationSolution(IClassificationProblemData problemData, IRandomForestModel randomForestModel)
    4646      : base(randomForestModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new RandomForestClassificationSolution(this, cloner);
    5251    }
    53 
    54     protected override void RecalculateResults() {
    55       CalculateResults();
    56     }
    5752  }
    5853}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs

    r7259 r9363  
    132132
    133133    public IRandomForestRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    134       return new RandomForestRegressionSolution(problemData, this);
     134      return new RandomForestRegressionSolution(new RegressionProblemData(problemData), this);
    135135    }
    136136    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    138138    }
    139139    public IRandomForestClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    140       return new RandomForestClassificationSolution(problemData, this);
     140      return new RandomForestClassificationSolution(new ClassificationProblemData(problemData), this);
    141141    }
    142142    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs

    r8139 r9363  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2928using HeuristicLab.Optimization;
     29using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
    32 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    33 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    34 using HeuristicLab.Parameters;
    3532
    3633namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4542    private const string NumberOfTreesParameterName = "Number of trees";
    4643    private const string RParameterName = "R";
     44    private const string MParameterName = "M";
     45    private const string SeedParameterName = "Seed";
     46    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
     47
    4748    #region parameter properties
    48     public IValueParameter<IntValue> NumberOfTreesParameter {
    49       get { return (IValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
     49    public IFixedValueParameter<IntValue> NumberOfTreesParameter {
     50      get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
    5051    }
    51     public IValueParameter<DoubleValue> RParameter {
    52       get { return (IValueParameter<DoubleValue>)Parameters[RParameterName]; }
     52    public IFixedValueParameter<DoubleValue> RParameter {
     53      get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
     54    }
     55    public IFixedValueParameter<DoubleValue> MParameter {
     56      get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
     57    }
     58    public IFixedValueParameter<IntValue> SeedParameter {
     59      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
     60    }
     61    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
     62      get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
    5363    }
    5464    #endregion
     
    6272      set { RParameter.Value.Value = value; }
    6373    }
     74    public double M {
     75      get { return MParameter.Value.Value; }
     76      set { MParameter.Value.Value = value; }
     77    }
     78    public int Seed {
     79      get { return SeedParameter.Value.Value; }
     80      set { SeedParameter.Value.Value = value; }
     81    }
     82    public bool SetSeedRandomly {
     83      get { return SetSeedRandomlyParameter.Value.Value; }
     84      set { SetSeedRandomlyParameter.Value.Value = value; }
     85    }
    6486    #endregion
    6587    [StorableConstructor]
     
    6890      : base(original, cloner) {
    6991    }
     92
    7093    public RandomForestRegression()
    7194      : base() {
    7295      Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50)));
    7396      Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "The ratio of the training set that will be used in the construction of individual trees (0<r<=1). Should be adjusted depending on the noise level in the dataset in the range from 0.66 (low noise) to 0.05 (high noise). This parameter should be adjusted to achieve good generalization error.", new DoubleValue(0.3)));
     97      Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
     98      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     99      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    74100      Problem = new RegressionProblem();
    75101    }
     102
    76103    [StorableHook(HookType.AfterDeserialization)]
    77     private void AfterDeserialization() { }
     104    private void AfterDeserialization() {
     105      if (!Parameters.ContainsKey(MParameterName))
     106        Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
     107      if (!Parameters.ContainsKey(SeedParameterName))
     108        Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
     109      if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
     110        Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
     111    }
    78112
    79113    public override IDeepCloneable Clone(Cloner cloner) {
     
    84118    protected override void Run() {
    85119      double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError;
    86       var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
     120      if (SetSeedRandomly) Seed = new System.Random().Next();
     121
     122      var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
    87123      Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution));
    88124      Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError)));
     
    92128    }
    93129
    94     public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r,
     130    public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, double m, int seed,
    95131      out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
     132      if (r <= 0 || r > 1) throw new ArgumentException("The R parameter in the random forest regression must be between 0 and 1.");
     133      if (m <= 0 || m > 1) throw new ArgumentException("The M parameter in the random forest regression must be between 0 and 1.");
     134
     135      alglib.math.rndobject = new System.Random(seed);
     136
    96137      Dataset dataset = problemData.Dataset;
    97138      string targetVariable = problemData.TargetVariable;
     
    102143        throw new NotSupportedException("Random forest regression does not support NaN or infinity values in the input dataset.");
    103144
     145      int info = 0;
     146      alglib.decisionforest dForest = new alglib.decisionforest();
     147      alglib.dfreport rep = new alglib.dfreport(); ;
     148      int nRows = inputMatrix.GetLength(0);
     149      int nColumns = inputMatrix.GetLength(1);
     150      int sampleSize = Math.Max((int)Math.Round(r * nRows), 1);
     151      int nFeatures = Math.Max((int)Math.Round(m * (nColumns - 1)), 1);
    104152
    105       alglib.decisionforest dforest;
    106       alglib.dfreport rep;
    107       int nRows = inputMatrix.GetLength(0);
    108 
    109       int info;
    110       alglib.dfbuildrandomdecisionforest(inputMatrix, nRows, allowedInputVariables.Count(), 1, nTrees, r, out info, out dforest, out rep);
     153      alglib.dforest.dfbuildinternal(inputMatrix, nRows, nColumns - 1, 1, nTrees, sampleSize, nFeatures, alglib.dforest.dfusestrongsplits + alglib.dforest.dfuseevs, ref info, dForest.innerobj, rep.innerobj);
    111154      if (info != 1) throw new ArgumentException("Error in calculation of random forest regression solution");
    112155
     
    116159      outOfBagRmsError = rep.oobrmserror;
    117160
    118       return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), new RandomForestModel(dforest, targetVariable, allowedInputVariables));
     161      return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), new RandomForestModel(dForest, targetVariable, allowedInputVariables));
    119162    }
    120163    #endregion
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs

    r7259 r9363  
    4545    public RandomForestRegressionSolution(IRegressionProblemData problemData, IRandomForestModel randomForestModel)
    4646      : base(randomForestModel, problemData) {
    47       RecalculateResults();
    4847    }
    4948
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs

    r8139 r9363  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using LibSVM;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4445    private const string NuParameterName = "Nu";
    4546    private const string GammaParameterName = "Gamma";
     47    private const string DegreeParameterName = "Degree";
    4648
    4749    #region parameter properties
     
    6062    public IValueParameter<DoubleValue> GammaParameter {
    6163      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
     64    }
     65    public IValueParameter<IntValue> DegreeParameter {
     66      get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
    6267    }
    6368    #endregion
     
    7984    public DoubleValue Gamma {
    8085      get { return GammaParameter.Value; }
     86    }
     87    public IntValue Degree {
     88      get { return DegreeParameter.Value; }
    8189    }
    8290    #endregion
     
    103111      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0)));
    104112      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
     113      Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
    105114    }
    106115    [StorableHook(HookType.AfterDeserialization)]
    107     private void AfterDeserialization() { }
     116    private void AfterDeserialization() {
     117      #region backwards compatibility (change with 3.4)
     118      if (!Parameters.ContainsKey(DegreeParameterName))
     119        Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
     120      #endregion
     121    }
    108122
    109123    public override IDeepCloneable Clone(Cloner cloner) {
     
    118132      int nSv;
    119133      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables,
    120         SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value,
     134        SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Degree.Value,
    121135        out trainingAccuracy, out testAccuracy, out nSv);
    122136
    123137      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
    124138      Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy)));
    125       Results.Add(new Result("Test ", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
     139      Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
    126140      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
    127141    }
    128142
    129143    public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
    130       string svmType, string kernelType, double cost, double nu, double gamma,
     144      string svmType, string kernelType, double cost, double nu, double gamma, int degree,
    131145      out double trainingAccuracy, out double testAccuracy, out int nSv) {
    132146      Dataset dataset = problemData.Dataset;
     
    135149
    136150      //extract SVM parameters from scope and set them
    137       SVM.Parameter parameter = new SVM.Parameter();
    138       parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
    139       parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
     151      svm_parameter parameter = new svm_parameter();
     152      parameter.svm_type = GetSvmType(svmType);
     153      parameter.kernel_type = GetKernelType(kernelType);
    140154      parameter.C = cost;
    141       parameter.Nu = nu;
    142       parameter.Gamma = gamma;
    143       parameter.CacheSize = 500;
    144       parameter.Probability = false;
    145 
     155      parameter.nu = nu;
     156      parameter.gamma = gamma;
     157      parameter.cache_size = 500;
     158      parameter.probability = 0;
     159      parameter.eps = 0.001;
     160      parameter.degree = degree;
     161      parameter.shrinking = 1;
     162      parameter.coef0 = 0;
     163
     164
     165      var weightLabels = new List<int>();
     166      var weights = new List<double>();
    146167      foreach (double c in problemData.ClassValues) {
    147168        double wSum = 0.0;
     
    151172          }
    152173        }
    153         parameter.Weights.Add((int)c, wSum);
     174        weightLabels.Add((int)c);
     175        weights.Add(wSum);
    154176      }
    155 
    156 
    157       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    158       SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    159       SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    160       var svmModel = SVM.Training.Train(scaledProblem, parameter);
     177      parameter.weight_label = weightLabels.ToArray();
     178      parameter.weight = weights.ToArray();
     179
     180
     181      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     182      RangeTransform rangeTransform = RangeTransform.Compute(problem);
     183      svm_problem scaledProblem = rangeTransform.Scale(problem);
     184      var svmModel = svm.svm_train(scaledProblem, parameter);
    161185      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
    162186      var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
    163187
    164       nSv = svmModel.SupportVectorCount;
     188      nSv = svmModel.SV.Length;
    165189      trainingAccuracy = solution.TrainingAccuracy;
    166190      testAccuracy = solution.TestAccuracy;
    167191
    168192      return solution;
     193    }
     194
     195    private static int GetSvmType(string svmType) {
     196      if (svmType == "NU_SVC") return svm_parameter.NU_SVC;
     197      if (svmType == "C_SVC") return svm_parameter.C_SVC;
     198      throw new ArgumentException("Unknown SVM type");
     199    }
     200
     201    private static int GetKernelType(string kernelType) {
     202      if (kernelType == "LINEAR") return svm_parameter.LINEAR;
     203      if (kernelType == "POLY") return svm_parameter.POLY;
     204      if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
     205      if (kernelType == "RBF") return svm_parameter.RBF;
     206      throw new ArgumentException("Unknown kernel type");
    169207    }
    170208    #endregion
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs

    r7259 r9363  
    4545    public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData)
    4646      : base(model, problemData) {
    47       RecalculateResults();
    4847    }
    4948
     
    5150      return new SupportVectorClassificationSolution(this, cloner);
    5251    }
    53 
    54     protected override void RecalculateResults() {
    55       CalculateResults();
    56     }
    5752  }
    5853}
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r7259 r9363  
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3030using HeuristicLab.Problems.DataAnalysis;
    31 using SVM;
     31using LibSVM;
    3232
    3333namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3939  public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel {
    4040
    41     private SVM.Model model;
     41    private svm_model model;
    4242    /// <summary>
    4343    /// Gets or sets the SVM model.
    4444    /// </summary>
    45     public SVM.Model Model {
     45    public svm_model Model {
    4646      get { return model; }
    4747      set {
     
    5757    /// Gets or sets the range transformation for the model.
    5858    /// </summary>
    59     private SVM.RangeTransform rangeTransform;
    60     public SVM.RangeTransform RangeTransform {
     59    private RangeTransform rangeTransform;
     60    public RangeTransform RangeTransform {
    6161      get { return rangeTransform; }
    6262      set {
     
    7171    public Dataset SupportVectors {
    7272      get {
    73         var data = new double[Model.SupportVectorCount, allowedInputVariables.Count()];
    74         for (int i = 0; i < Model.SupportVectorCount; i++) {
    75           var sv = Model.SupportVectors[i];
     73        var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()];
     74        for (int i = 0; i < Model.sv_coef.Length; i++) {
     75          var sv = Model.SV[i];
    7676          for (int j = 0; j < sv.Length; j++) {
    77             data[i, j] = sv[j].Value;
     77            data[i, j] = sv[j].value;
    7878          }
    7979        }
     
    101101        this.classValues = (double[])original.classValues.Clone();
    102102    }
    103     public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
     103    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
    104104      : this(model, rangeTransform, targetVariable, allowedInputVariables) {
    105105      this.classValues = classValues.ToArray();
    106106    }
    107     public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
     107    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
    108108      : base() {
    109109      this.name = ItemName;
     
    124124    }
    125125    public SupportVectorRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    126       return new SupportVectorRegressionSolution(this, problemData);
     126      return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData));
    127127    }
    128128    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     
    153153
    154154    public SupportVectorClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    155       return new SupportVectorClassificationSolution(this, problemData);
     155      return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData));
    156156    }
    157157    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
     
    161161    private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) {
    162162      // calculate predictions for the currently requested rows
    163       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    164       SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
    165 
    166       for (int i = 0; i < scaledProblem.Count; i++) {
    167         yield return SVM.Prediction.Predict(Model, scaledProblem.X[i]);
     163      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     164      svm_problem scaledProblem = rangeTransform.Scale(problem);
     165
     166      for (int i = 0; i < problem.l; i++) {
     167        yield return svm.svm_predict(Model, scaledProblem.x[i]);
    168168      }
    169169    }
     
    183183      get {
    184184        using (MemoryStream stream = new MemoryStream()) {
    185           SVM.Model.Write(stream, Model);
     185          svm.svm_save_model(new StreamWriter(stream), Model);
    186186          stream.Seek(0, System.IO.SeekOrigin.Begin);
    187187          StreamReader reader = new StreamReader(stream);
     
    191191      set {
    192192        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
    193           model = SVM.Model.Read(stream);
     193          model = svm.svm_load_model(new StreamReader(stream));
    194194        }
    195195      }
     
    199199      get {
    200200        using (MemoryStream stream = new MemoryStream()) {
    201           SVM.RangeTransform.Write(stream, RangeTransform);
     201          RangeTransform.Write(stream, RangeTransform);
    202202          stream.Seek(0, System.IO.SeekOrigin.Begin);
    203203          StreamReader reader = new StreamReader(stream);
     
    207207      set {
    208208        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
    209           RangeTransform = SVM.RangeTransform.Read(stream);
     209          RangeTransform = RangeTransform.Read(stream);
    210210        }
    211211      }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs

    r7259 r9363  
    2323using System.Linq;
    2424using HeuristicLab.Problems.DataAnalysis;
     25using LibSVM;
    2526
    2627namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3233    /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param>
    3334    /// <returns>A problem data type that can be used to train a support vector machine.</returns>
    34     public static SVM.Problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
     35    public static svm_problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
    3536      double[] targetVector =
    3637        dataset.GetDoubleValues(targetVariable, rowIndices).ToArray();
    3738
    38       SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
    39       List<SVM.Node> tempRow;
     39      svm_node[][] nodes = new svm_node[targetVector.Length][];
     40      List<svm_node> tempRow;
    4041      int maxNodeIndex = 0;
    4142      int svmProblemRowIndex = 0;
    4243      List<string> inputVariablesList = inputVariables.ToList();
    4344      foreach (int row in rowIndices) {
    44         tempRow = new List<SVM.Node>();
     45        tempRow = new List<svm_node>();
    4546        int colIndex = 1; // make sure the smallest node index for SVM = 1
    4647        foreach (var inputVariable in inputVariablesList) {
     
    4950          // => don't add NaN values in the dataset to the sparse SVM matrix representation
    5051          if (!double.IsNaN(value)) {
    51             tempRow.Add(new SVM.Node(colIndex, value)); // nodes must be sorted in ascending ordered by column index
     52            tempRow.Add(new svm_node() { index = colIndex, value = value }); // nodes must be sorted in ascending ordered by column index
    5253            if (colIndex > maxNodeIndex) maxNodeIndex = colIndex;
    5354          }
     
    5758      }
    5859
    59       return new SVM.Problem(targetVector.Length, targetVector, nodes, maxNodeIndex);
     60      return new svm_problem() { l = targetVector.Length, y = targetVector, x = nodes };
    6061    }
    6162  }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs

    r8139 r9363  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using LibSVM;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4546    private const string GammaParameterName = "Gamma";
    4647    private const string EpsilonParameterName = "Epsilon";
     48    private const string DegreeParameterName = "Degree";
    4749
    4850    #region parameter properties
     
    6466    public IValueParameter<DoubleValue> EpsilonParameter {
    6567      get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
     68    }
     69    public IValueParameter<IntValue> DegreeParameter {
     70      get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
    6671    }
    6772    #endregion
     
    8691    public DoubleValue Epsilon {
    8792      get { return EpsilonParameter.Value; }
     93    }
     94    public IntValue Degree {
     95      get { return DegreeParameter.Value; }
    8896    }
    8997    #endregion
     
    111119      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
    112120      Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
     121      Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
    113122    }
    114123    [StorableHook(HookType.AfterDeserialization)]
    115     private void AfterDeserialization() { }
     124    private void AfterDeserialization() {
     125      #region backwards compatibility (change with 3.4)
     126      if (!Parameters.ContainsKey(DegreeParameterName))
     127        Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
     128      #endregion
     129    }
    116130
    117131    public override IDeepCloneable Clone(Cloner cloner) {
     
    126140      int nSv;
    127141      var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value,
    128         KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value,
     142        KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value, Degree.Value,
    129143        out trainR2, out testR2, out nSv);
    130144
     
    136150
    137151    public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
    138       string svmType, string kernelType, double cost, double nu, double gamma, double epsilon,
     152      string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree,
    139153      out double trainingR2, out double testR2, out int nSv) {
    140154      Dataset dataset = problemData.Dataset;
     
    143157
    144158      //extract SVM parameters from scope and set them
    145       SVM.Parameter parameter = new SVM.Parameter();
    146       parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
    147       parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
     159      svm_parameter parameter = new svm_parameter();
     160      parameter.svm_type = GetSvmType(svmType);
     161      parameter.kernel_type = GetKernelType(kernelType);
    148162      parameter.C = cost;
    149       parameter.Nu = nu;
    150       parameter.Gamma = gamma;
    151       parameter.P = epsilon;
    152       parameter.CacheSize = 500;
    153       parameter.Probability = false;
    154 
    155 
    156       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    157       SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    158       SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    159       var svmModel = SVM.Training.Train(scaledProblem, parameter);
    160       nSv = svmModel.SupportVectorCount;
     163      parameter.nu = nu;
     164      parameter.gamma = gamma;
     165      parameter.p = epsilon;
     166      parameter.cache_size = 500;
     167      parameter.probability = 0;
     168      parameter.eps = 0.001;
     169      parameter.degree = degree;
     170      parameter.shrinking = 1;
     171      parameter.coef0 = 0;
     172
     173
     174
     175      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     176      RangeTransform rangeTransform = RangeTransform.Compute(problem);
     177      svm_problem scaledProblem = rangeTransform.Scale(problem);
     178      var svmModel = svm.svm_train(scaledProblem, parameter);
     179      nSv = svmModel.SV.Length;
    161180      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
    162181      var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     
    165184      return solution;
    166185    }
     186
     187    private static int GetSvmType(string svmType) {
     188      if (svmType == "NU_SVR") return svm_parameter.NU_SVR;
     189      if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR;
     190      throw new ArgumentException("Unknown SVM type");
     191    }
     192
     193    private static int GetKernelType(string kernelType) {
     194      if (kernelType == "LINEAR") return svm_parameter.LINEAR;
     195      if (kernelType == "POLY") return svm_parameter.POLY;
     196      if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
     197      if (kernelType == "RBF") return svm_parameter.RBF;
     198      throw new ArgumentException("Unknown kernel type");
     199    }
    167200    #endregion
    168201  }
  • branches/OaaS/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs

    r7259 r9363  
    4545    public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData)
    4646      : base(model, problemData) {
    47       RecalculateResults();
    4847    }
    4948
Note: See TracChangeset for help on using the changeset viewer.