|
| 1 | +.. _javascript_interface: |
| 2 | + |
| 3 | +JavaScript / WebAssembly |
| 4 | +======================== |
| 5 | + |
| 6 | +After :ref:`installing the scs-solver package <javascript_install>`, or after |
| 7 | +loading ``scs.js`` :ref:`in the browser <javascript_install>`, you can use SCS |
| 8 | +using the following interface. |
| 9 | + |
| 10 | +Instantiating the Solver |
| 11 | +------------------------ |
| 12 | + |
| 13 | +In Node.js, you can use SCS as follows using CommonJS: |
| 14 | + |
| 15 | +.. code-block:: javascript |
| 16 | +
|
| 17 | + const createSCS = require('scs-solver'); |
| 18 | +
|
| 19 | + createSCS().then(SCS => { |
| 20 | + // define problem here |
| 21 | + SCS.solve(data, cone, settings); |
| 22 | + }); |
| 23 | +
|
| 24 | +Alternatively, you can use ES6 modules, as well as async/await: |
| 25 | + |
| 26 | +.. code-block:: javascript |
| 27 | +
|
| 28 | + import createSCS from 'scs-solver'; |
| 29 | +
|
| 30 | + async function main() { |
| 31 | + const SCS = await createSCS(); |
| 32 | + // define problem here |
| 33 | + SCS.solve(data, cone, settings); |
| 34 | + } |
| 35 | +
|
| 36 | + main(); |
| 37 | +
|
| 38 | +In browsers, you can load SCS using a script tag: |
| 39 | + |
| 40 | +.. code-block:: html |
| 41 | + |
| 42 | + <script src="https://unpkg.com/scs-solver/dist/scs.js"></script> |
| 43 | + <script> |
| 44 | + createSCS().then(SCS => { |
| 45 | + // define problem here |
| 46 | + SCS.solve(data, cone, settings); |
| 47 | + }); |
| 48 | + </script> |
| 49 | + |
| 50 | +Data Format |
| 51 | +----------- |
| 52 | + |
| 53 | +Problem data must be provided as sparse matrices in CSC format using the following structure: |
| 54 | + |
| 55 | +.. code-block:: javascript |
| 56 | +
|
| 57 | + const data = { |
| 58 | + m: number, // Number of rows of A |
| 59 | + n: number, // Number of cols of A and of P |
| 60 | + A_x: number[], // Non-zero elements of matrix A |
| 61 | + A_i: number[], // Row indices of A elements |
| 62 | + A_p: number[], // Column pointers for A |
| 63 | + P_x: number[], // Non-zero elements of matrix P (optional) |
| 64 | + P_i: number[], // Row indices of P elements (optional) |
| 65 | + P_p: number[], // Column pointers for P (optional) |
| 66 | + b: number[], // Length m array |
| 67 | + c: number[] // Length n array |
| 68 | + }; |
| 69 | +
|
| 70 | +One way to handle the CSC format in javascript is via the |
| 71 | +`Math.js library <https://mathjs.org/docs/reference/classes/sparsematrix.html>`_, |
| 72 | +for example |
| 73 | + |
| 74 | +.. code-block:: javascript |
| 75 | +
|
| 76 | + // npm install mathjs |
| 77 | + const { matrix } = require('mathjs'); |
| 78 | + // or import { matrix } from 'mathjs'; |
| 79 | + // or <script src="https://unpkg.com/[email protected]/lib/browser/math.js"></script> |
| 80 | +
|
| 81 | + const A = matrix([ |
| 82 | + [1, 0], |
| 83 | + [0, 1], |
| 84 | + [1, 1] |
| 85 | + ], 'sparse'); |
| 86 | +
|
| 87 | + const P = matrix([ |
| 88 | + [3, 0], |
| 89 | + [0, 2] |
| 90 | + ], 'sparse'); |
| 91 | +
|
| 92 | + const data = { |
| 93 | + m: 3, |
| 94 | + n: 2, |
| 95 | + A_x: A._values, |
| 96 | + A_i: A._index, |
| 97 | + A_p: A._ptr, |
| 98 | + P_x: P._values, |
| 99 | + P_i: P._index, |
| 100 | + P_p: P._ptr, |
| 101 | + b: [-1.0, 0.3, -0.5], |
| 102 | + c: [-1.0, -1.0] |
| 103 | + }; |
| 104 | +
|
| 105 | +Cone Specification |
| 106 | +------------------ |
| 107 | + |
| 108 | +Cones are specified using the following structure: |
| 109 | + |
| 110 | +.. code-block:: javascript |
| 111 | +
|
| 112 | + const cone = { |
| 113 | + z: number, // Number of zero cones |
| 114 | + l: number, // Number of positive (or linear) cones |
| 115 | + bu: number[], // Box cone upper values |
| 116 | + bl: number[], // Box cone lower values |
| 117 | + bsize: number, // Total length of box cone |
| 118 | + q: number[], // Array of second-order cone lengths |
| 119 | + qsize: number, // Number of second-order cones |
| 120 | + ep: number, // Number of primal exponential cone triples |
| 121 | + ed: number, // Number of dual exponential cone triples |
| 122 | + p: number[], // Array of power cone parameters |
| 123 | + psize: number // Number of power cone triples |
| 124 | + }; |
| 125 | +
|
| 126 | +Note that positive semidefinite cones are not supported in the JavaScript interface. |
| 127 | + |
| 128 | +Usually, not all cone types are used in a problem, in which case the unused |
| 129 | +cones can be omitted. For example, if only zero and positive cones are used: |
| 130 | + |
| 131 | +.. code-block:: javascript |
| 132 | +
|
| 133 | + const cone = { |
| 134 | + z: 1, |
| 135 | + l: 2 |
| 136 | + }; |
| 137 | +
|
| 138 | +Settings |
| 139 | +-------- |
| 140 | + |
| 141 | +Control solver behavior using settings: |
| 142 | + |
| 143 | +.. code-block:: javascript |
| 144 | +
|
| 145 | + const settings = new Module.ScsSettings(); |
| 146 | + Module.setDefaultSettings(settings); |
| 147 | +
|
| 148 | +Available settings: |
| 149 | + |
| 150 | +- ``normalize`` (boolean): Heuristically rescale problem data |
| 151 | +- ``scale`` (number): Initial dual scaling factor |
| 152 | +- ``adaptiveScale`` (boolean): Whether to adaptively update scale |
| 153 | +- ``rhoX`` (number): Primal constraint scaling factor |
| 154 | +- ``maxIters`` (number): Maximum iterations to take |
| 155 | +- ``epsAbs`` (number): Absolute convergence tolerance |
| 156 | +- ``epsRel`` (number): Relative convergence tolerance |
| 157 | +- ``epsInfeas`` (number): Infeasible convergence tolerance |
| 158 | +- ``alpha`` (number): Douglas-Rachford relaxation parameter |
| 159 | +- ``timeLimitSecs`` (number): Time limit in seconds |
| 160 | +- ``verbose`` (number): Output level (0-3) |
| 161 | +- ``warmStart`` (boolean): Use warm starting |
| 162 | + |
| 163 | +Solving Problems |
| 164 | +---------------- |
| 165 | + |
| 166 | +Use the ``solve`` function to solve optimization problems: |
| 167 | + |
| 168 | +.. code-block:: javascript |
| 169 | +
|
| 170 | + const solution = Module.solve(data, cone, settings, [warmStartSolution]); |
| 171 | +
|
| 172 | +The function takes an optional ``warmStartSolution`` object to warm-start the solver, |
| 173 | +provided ``settings.warmStart`` is set to ``true``. |
| 174 | + |
| 175 | +The returned ``solution`` object contains: |
| 176 | + |
| 177 | +- ``x``: Primal variables |
| 178 | +- ``y``: Dual variables |
| 179 | +- ``s``: Slack variables |
| 180 | +- ``info``: Solver information |
| 181 | + |
| 182 | + - ``iter``: Number of iterations |
| 183 | + - ``pobj``: Primal objective |
| 184 | + - ``dobj``: Dual objective |
| 185 | + - ``solveTime``: Solve time |
| 186 | + - and :ref:`other solver information <info>` |
| 187 | + |
| 188 | +- ``status``: Solution status (e.g. ``SOLVED``, ``INFEASIBLE``, ``UNBOUNDED``, see :ref:`exit flags <exit_flags>`) |
| 189 | +- ``statusVal``: Solution status value (see :ref:`exit flags <exit_flags>`) |
| 190 | + |
0 commit comments