/// <summary> /// Checks whether the parameters are within the feasible range of the problem. /// </summary> /// <param name="problem"><see cref="Problem"/>.</param> /// <param name="parameter"><see cref="Parameter"/>.</param> /// <returns>It returns null if the parameters are feasible, otherwise an error message is returned.</returns> public static string CheckParameter(Problem problem, Parameter parameter) { if (problem == null) { throw new ArgumentNullException(nameof(problem)); } if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } problem.ThrowIfDisposed(); unsafe { var param = new NativeMethods.svm_parameter(); try { // This method throw exception when there is some errors. // This error check is not official's. param = parameter.ToNative(); } catch (LibSvmException lle) { return(lle.Message); } try { var ret = NativeMethods.svm_check_parameter((NativeMethods.svm_problem *)problem.NativePtr, ¶m); return(Marshal.PtrToStringAnsi(ret)); } finally { Free(¶m); } } }
private static unsafe NativeMethods.svm_parameter ToNative(this Parameter parameter) { var ret = new NativeMethods.svm_parameter { svm_type = (int)parameter.SvmType, kernel_type = (int)parameter.KernelType, degree = parameter.Degree, gamma = parameter.Gamma, coef0 = parameter.Coef0, cache_size = parameter.CacheSize, eps = parameter.Epsilon, C = parameter.C, nr_weight = parameter.LengthOfWeight, nu = parameter.Nu, p = parameter.P, shrinking = parameter.Shrinking ? NativeMethods.True : NativeMethods.False, probability = parameter.Probability ? NativeMethods.True : NativeMethods.False }; if (ret.nr_weight > 0) { if (parameter.WeightLabel == null) { throw new LibSvmException("Parameter.WeightLabel is null although Parameter.LengthOfWeight is over 0."); } if (parameter.Weight == null) { throw new LibSvmException("Parameter.Weight is null although Parameter.LengthOfWeight is over 0."); } var len1 = parameter.WeightLabel.Length; var len2 = parameter.Weight.Length; if (len1 != ret.nr_weight || len2 != ret.nr_weight) { throw new LibSvmException("Parameter.WeightLabel.Length does not match Parameter.Weight.Length"); } } var failAlloc = false; try { if (ret.nr_weight > 0) { ret.weight_label = (int *)NativeMethods.malloc(sizeof(int), ret.nr_weight); fixed(int *p = ¶meter.WeightLabel[0]) NativeMethods.memcpy(ret.weight_label, p, ret.nr_weight * sizeof(int)); ret.weight = (double *)NativeMethods.malloc(sizeof(double), ret.nr_weight); fixed(double *p = ¶meter.Weight[0]) NativeMethods.memcpy(ret.weight, p, ret.nr_weight * sizeof(double)); } failAlloc = true; } finally { if (!failAlloc) { NativeMethods.free((IntPtr)ret.weight_label); NativeMethods.free((IntPtr)ret.weight); } } return(ret); }