1 | using System;
|
---|
2 | using System.Runtime.InteropServices;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.NativeInterpreter {
|
---|
5 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
---|
6 | public struct NativeInstruction {
|
---|
7 | public byte OpCode;
|
---|
8 | public ushort Arity;
|
---|
9 | public ushort Length;
|
---|
10 | public double Value; // weights for variables, values for parameters
|
---|
11 | public IntPtr Data; // used for variables only
|
---|
12 | public bool Optimize; // used for parameters only
|
---|
13 | }
|
---|
14 |
|
---|
15 | public enum Algorithm : int {
|
---|
16 | Krogh = 0,
|
---|
17 | RuheWedin1 = 1,
|
---|
18 | RuheWedin2 = 2,
|
---|
19 | RuheWedin3 = 3
|
---|
20 | }
|
---|
21 |
|
---|
22 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
---|
23 | public class SolverOptions {
|
---|
24 | public int Iterations = 10;
|
---|
25 | public int UseNonmonotonicSteps = 0; // = false
|
---|
26 | public CeresTypes.Minimizer Minimizer = CeresTypes.Minimizer.TRUST_REGION;
|
---|
27 | public CeresTypes.LinearSolver LinearSolver = CeresTypes.LinearSolver.DENSE_QR;
|
---|
28 | public CeresTypes.TrustRegionStrategy TrustRegionStrategy = CeresTypes.TrustRegionStrategy.LEVENBERG_MARQUARDT;
|
---|
29 | public CeresTypes.DogLeg DogLeg = CeresTypes.DogLeg.TRADITIONAL_DOGLEG;
|
---|
30 | public CeresTypes.LineSearchDirection LineSearchDirection = CeresTypes.LineSearchDirection.LBFGS;
|
---|
31 | public Algorithm Algorithm = Algorithm.Krogh;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // proxy structure to pass information from ceres back to the caller
|
---|
35 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
---|
36 | public class OptimizationSummary {
|
---|
37 | public double InitialCost; // value of the objective function before the optimization
|
---|
38 | public double FinalCost; // value of the objective function after the optimization
|
---|
39 | public int SuccessfulSteps; // number of minimizer iterations in which the step was accepted
|
---|
40 | public int UnsuccessfulSteps; // number of minimizer iterations in which the step was rejected
|
---|
41 | public int InnerIterationSteps; // number of times inner iterations were performed
|
---|
42 | public int ResidualEvaluations; // number of residual only evaluations
|
---|
43 | public int JacobianEvaluations; // number of Jacobian (and residual) evaluations
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static class NativeWrapper {
|
---|
47 | private const string x64dll = "hl-native-interpreter-x64.dll";
|
---|
48 | private readonly static bool is64;
|
---|
49 |
|
---|
50 | static NativeWrapper() {
|
---|
51 | is64 = Environment.Is64BitProcess;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static void GetValues(NativeInstruction[] code, int[] rows, SolverOptions options, double[] result, double[] target, out OptimizationSummary optSummary) {
|
---|
55 | optSummary = new OptimizationSummary();
|
---|
56 | if (is64)
|
---|
57 | GetValues64(code, code.Length, rows, rows.Length, options, result, target, optSummary);
|
---|
58 | else
|
---|
59 | throw new NotSupportedException("Native interpreter is only available on x64 builds");
|
---|
60 | }
|
---|
61 | public static void GetValuesVarPro(NativeInstruction[] code, int[] termIndices, int[] rows, double[] coefficients, SolverOptions options, double[] result, double[] target, out OptimizationSummary optSummary) {
|
---|
62 | optSummary = new OptimizationSummary();
|
---|
63 | if (is64)
|
---|
64 | GetValuesVarPro64(code, code.Length, termIndices, termIndices.Length, rows, rows.Length, coefficients, options, result, target, optSummary);
|
---|
65 | else
|
---|
66 | throw new NotSupportedException("Native interpreter is only available on x64 builds");
|
---|
67 | }
|
---|
68 |
|
---|
69 | [DllImport(x64dll, EntryPoint = "GetValues", CallingConvention = CallingConvention.Cdecl)]
|
---|
70 | internal static extern void GetValues64(
|
---|
71 | [In,Out] NativeInstruction[] code, // parameters are optimized by callee
|
---|
72 | [In] int len,
|
---|
73 | [In] int[] rows,
|
---|
74 | [In] int nRows,
|
---|
75 | [In] SolverOptions options,
|
---|
76 | [Out] double[] result,
|
---|
77 | [In] double[] target,
|
---|
78 | [Out] OptimizationSummary optSummary);
|
---|
79 |
|
---|
80 | [DllImport(x64dll, EntryPoint = "GetValuesVarPro", CallingConvention = CallingConvention.Cdecl)]
|
---|
81 | internal static extern void GetValuesVarPro64(
|
---|
82 | [In,Out] NativeInstruction[] code, // the values fields for non-linear parameters are changed by the callee
|
---|
83 | int len,
|
---|
84 | int[] termIndices,
|
---|
85 | int nTerms,
|
---|
86 | int[] rows,
|
---|
87 | int nRows,
|
---|
88 | [In,Out] double[] coefficients,
|
---|
89 | [In] SolverOptions options,
|
---|
90 | [In, Out] double[] result,
|
---|
91 | [In] double[] target,
|
---|
92 | [In,Out] OptimizationSummary optSummary);
|
---|
93 | }
|
---|
94 | }
|
---|