On this page:
2.1 Quadratic and linear programs
2.2 Second-order cone
2.3 Semidefinite program
2.4 Exponential cone
2.5 Lasso with warm starting
2.6 Maximum entropy
2.7 Model predictive control
9.2

2 Examples🔗ℹ

Each example below has a runnable counterpart in the package’s "examples/" directory (e.g. "examples/00-quadratic-program.rkt") with a module+ test. They mirror the worked examples in the SCS documentation, rewritten in Racket.

2.1 Quadratic and linear programs🔗ℹ

A quadratic program with one equality and two inequalities ("examples/00-quadratic-program.rkt"); optimum x = (0.3, -0.7):

(solve #:A (scs:matrix 3 2  -1 1   1 0   0 1)
       #:b #(-1.0 0.3 -0.5)
       #:c #(-1.0 -1.0)
       #:P (scs:sparse-matrix 2 2 '(0 0 3) '(0 1 -1) '(1 1 2))
       #:cone (make-cone #:zero 1 #:positive 2))

A pure linear program over the positive orthant ("examples/01-linear-program.rkt") drops #:P and uses only #:positive rows.

2.2 Second-order cone🔗ℹ

Minimize t subject to ‖(u, v)‖₂ ≤ t with u, v pinned by equalities ("examples/02-second-order-cone.rkt"); optimum (t, u, v) = (5, 3, 4). The SOC block is encoded with A = -I so the slack s = (t, u, v) lands in the cone:

(solve #:A (scs:matrix 5 3
                       0 1 0     ; u = 3
                       0 0 1     ; v = 4
                       -1 0 0    ; SOC block
                       0 -1 0
                       0 0 -1)
       #:b #(3.0 4.0 0.0 0.0 0.0)
       #:c #(1.0 0.0 0.0)
       #:cone (make-cone #:zero 2 #:soc '(3)))

2.3 Semidefinite program🔗ℹ

Minimize the trace of a 2×2 PSD matrix X = [[a b] [b c]] subject to b = 1 ("examples/03-semidefinite.rkt"); optimum (a, b, c) = (1, 1, 1). The PSD block uses the √2-scaled vectorization (a, √2·b, c) (see Semidefinite cones):

(define root2 (sqrt 2.0))
(solve #:A (scs:matrix 4 3
                       0 1 0           ; b = 1
                       -1 0 0          ; svec = (a, root2 b, c)
                       0 (- root2) 0
                       0 0 -1)
       #:b #(1.0 0.0 0.0 0.0)
       #:c #(1.0 0.0 1.0)
       #:cone (make-cone #:zero 1 #:psd '(2)))

2.4 Exponential cone🔗ℹ

Minimize z subject to (x, y, z) in the exponential cone with x = y = 1 ("examples/04-exponential-cone.rkt"); the cone forces z ≥ e, so the optimum is z = e.

2.5 Lasso with warm starting🔗ℹ

minimize (1/2)‖Ax − b‖² + λ‖x‖₁ over a sequence of λ ("examples/07-lasso.rkt"), mirroring the SCS lasso example. Writing |x_i| ≤ t_i as two linear inequalities gives a quadratic cone program in (x, t) where P carries AᵀA; only c changes with λ, so a single workspace is reused and scs-updated across the regularization path:

(require scs scs/foreign)
(define work (scs-init data cone stgs))
(scs-solve work sol info 0)               ; first lambda
(scs-update work b (cost next-lambda))    ; only c changes
(scs-solve work sol info 1)               ; warm-started re-solve

2.6 Maximum entropy🔗ℹ

maximize −Σ x_i log x_i over the probability simplex ("examples/08-max-entropy.rkt"), mirroring the SCS entropy example. The epigraph t_i ≥ x_i log x_i is the exponential-cone membership (−t_i, x_i, 1) ∈ K_exp. With only the simplex constraint the maximizer is the uniform distribution.

2.7 Model predictive control🔗ℹ

A finite-horizon control problem with input bounds via the box cone and receding-horizon warm starting ("examples/09-mpc.rkt"), mirroring the SCS MPC example. The bounds −1 ≤ u_t ≤ 1 use #:box-lower and #:box-upper, and the workspace is scs-updated as the initial state evolves.