-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
209 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
.. _c_example: | ||
|
||
C/C++ | ||
====== | ||
TODO | ||
===== | ||
|
||
.. include:: qp.prob | ||
|
||
C code to solve this is below. | ||
|
||
.. literalinclude:: qp.c | ||
:language: c | ||
|
||
After following the CMake :ref:`install instructions <c_install>`, we can | ||
compile the code using: | ||
|
||
.. code:: | ||
gcc -I/usr/local/include/scs -L/usr/local/lib/ -lscsdir qp.c -o qp.out | ||
.. ./qp.out > qp.c.out | ||
Then run the code yielding output | ||
|
||
.. literalinclude:: qp.c.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "scs.h" /* SCS API */ | ||
|
||
/* Set up and solve basic qp */ | ||
int main(int argc, char **argv) { | ||
|
||
/* Set up the problem data */ | ||
/* A and P must be in compressed sparse column format */ | ||
double P_x[3] = {3., -1., 2.}; /* Upper triangular of P only */ | ||
int P_i[3] = {0, 0, 1}; | ||
int P_p[3] = {0, 1, 3}; | ||
double A_x[4] = {-1., 1., 1., 1.}; | ||
int A_i[4] = {0, 1, 0, 2}; | ||
int A_p[3] = {0, 2, 4}; | ||
double b[3] = {-1., 0.3, -0.5,}; | ||
double c[2] = {-1., -1.}; | ||
/* data shape */ | ||
int n = 2; | ||
int m = 3; | ||
|
||
/* Allocate SCS structs */ | ||
ScsCone *k = (ScsCone *)calloc(1, sizeof(ScsCone)); | ||
ScsData *d = (ScsData *)calloc(1, sizeof(ScsData)); | ||
ScsSettings *stgs = (ScsSettings *)calloc(1, sizeof(ScsSettings)); | ||
ScsSolution *sol = (ScsSolution *)calloc(1, sizeof(ScsSolution)); | ||
ScsInfo *info = (ScsInfo *)calloc(1, sizeof(ScsInfo)); | ||
|
||
/* Fill in structs */ | ||
d->m = m; | ||
d->n = n; | ||
d->b = b; | ||
d->c = c; | ||
d->A = &(ScsMatrix) {A_x, A_i, A_p, m, n}; | ||
d->P = &(ScsMatrix) {P_x, P_i, P_p, n, n}; | ||
|
||
/* Cone */ | ||
k->l = m; | ||
|
||
/* Utility to set some default settings */ | ||
scs_set_default_settings(stgs); | ||
|
||
/* Modify tolerances */ | ||
stgs->eps_abs=1e-9; | ||
stgs->eps_rel=1e-9; | ||
|
||
/* Solve! */ | ||
int exitflag = scs(d, k, stgs, sol, info); | ||
|
||
/* Verify that SCS solved the problem */ | ||
printf("SCS solved successfully: %i\n", exitflag == SCS_SOLVED); | ||
|
||
/* Print iterations taken */ | ||
printf("SCS took %i iters\n", info->iter); | ||
|
||
/* Print solution x */ | ||
printf("Optimal solution vector x*:\n"); | ||
for (int i = 0; i < n; ++i) { | ||
printf("x[%i] = %4f\n", i, sol->x[i]); | ||
} | ||
|
||
/* Print dual solution y */ | ||
printf("Optimal dual vector y*:\n"); | ||
for (int i = 0; i < m; ++i) { | ||
printf("y[%i] = %4f\n", i, sol->y[i]); | ||
} | ||
|
||
/* Free allocated memory */ | ||
free(k); | ||
free(d); | ||
free(stgs); | ||
free(info); | ||
/* SCS allocates sol->x,y,s if NULL on entry, need to be freed */ | ||
free(sol->x); | ||
free(sol->y); | ||
free(sol->s); | ||
free(sol); | ||
|
||
return exitflag; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
% First, make sure SCS is in the path so MATLAB can call it | ||
addpath("/Users/bodonoghue/git/scs-matlab") | ||
|
||
% Set up data | ||
data.P = sparse([3., -1.; -1., 2.]); | ||
data.A = sparse([-1., 1.; 1., 0.; 0., 1.]); | ||
data.b = [-1; 0.3; -0.5]; | ||
data.c = [-1.; -1.]; | ||
cone.l = size(data.b, 1); | ||
|
||
% Optional solver settings | ||
settings = struct('eps_abs', 1e-9, 'eps_rel', 1e-9); | ||
|
||
% Solve! | ||
[x, y, s, info] = scs(data, cone, settings); | ||
|
||
disp(sprintf("SCS took %i iters", info.iter)) | ||
disp("Optimal solution vector x*:"); | ||
disp(x) | ||
disp("Optimal dual vector y*:"); | ||
disp(y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
In this example we shall solve the following small quadratic program: | ||
|
||
.. math:: | ||
\begin{array}{ll} | ||
\mbox{minimize} & \frac{1}{2} x^T \begin{bmatrix}3 & -1\\ -1 & 2 \end{bmatrix} | ||
x + \begin{bmatrix}-1 \\ -1\end{bmatrix}^T x \\ | ||
\mbox{subject to} & \begin{bmatrix} -1 & 1\\ 1 & 0\\ 0 & 1\end{bmatrix} x \leq \begin{bmatrix}-1 \\ 0.3 \\ -0.5\end{bmatrix} | ||
\end{array} | ||
|
||
over variable :math:`x \in \mathbf{R}^2`. This problem corresponds to data: | ||
|
||
.. math:: | ||
\begin{array}{cccc} | ||
P = \begin{bmatrix}3 & -1\\ -1 & 2 \end{bmatrix}, & | ||
A = \begin{bmatrix}-1 & 1\\ 1 & 0\\ 0 & 1\end{bmatrix}, & | ||
b = \begin{bmatrix}-1 \\ 0.3 \\ -0.5\end{bmatrix}, & | ||
c = \begin{bmatrix}-1 \\ -1\end{bmatrix} | ||
\end{array} | ||
|
||
The cone :math:`\mathcal{K}` is simply the positive orthant :code:`l` of | ||
dimension 3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import scs | ||
import numpy as np | ||
from scipy import sparse | ||
|
||
# Set up the problem data | ||
P = sparse.csc_matrix([[3., -1.], [-1., 2.]]) | ||
A = sparse.csc_matrix([[-1., 1.], [1., 0.], [0., 1.]]) | ||
b = np.array([-1, 0.3, -0.5]) | ||
c = np.array([-1., -1.]) | ||
|
||
# Populate dicts with data to pass into SCS | ||
data = dict(P=P, A=A, b=b, c=c) | ||
cone = dict(l=len(b)); | ||
|
||
# Solve! | ||
sol = scs.solve(data, cone, eps_abs=1e-9, eps_rel=1e-9) | ||
|
||
print(f"SCS took {sol['info']['iter']} iters") | ||
print("Optimal solution vector x*:"); | ||
print(sol['x']) | ||
|
||
print("Optimal dual vector y*:"); | ||
print(sol['y']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters