Skip to content

Instantly share code, notes, and snippets.

View alphaville's full-sized avatar
:octocat:
(De)coding

Pantelis Sopasakis alphaville

:octocat:
(De)coding
View GitHub Profile
@alphaville
alphaville / optimizer.c
Created June 4, 2019 19:41
Compilation of auto-generated C file (C bindings to Rust code)
/* Compile with:
* gcc optimizer.c -l:libthe_optimizer.a -L./target/debug -pthread -lm -ldl -o optimizer -Wall
*/
#include <stdio.h>
#include "open_bindings.h"
int main() {
HomotopyCache * y = solver_new();
const double p[2] = {1.0, 10.0};
@alphaville
alphaville / open-tcp-server.rs
Last active May 31, 2019 21:13
OpEn TCP server
use optimization_engine::continuation::HomotopySolverStatus;
use serde::{Deserialize, Serialize};
use std::{
io::{prelude::Read, Write},
net::TcpListener,
};
use test_optimizer::*;
extern crate pretty_env_logger;
#[macro_use]
@alphaville
alphaville / opengen-example.py
Created May 29, 2019 17:17
Example of using opengen
import casadi.casadi as cs
import opengen as og
u = cs.SX.sym("u", 5)
p = cs.SX.sym("p", 2)
# COST FUNCTION
phi = og.functions.rosenbrock(u, p)
# PENALTY-TYPE CONSTRAINTS
function [r,a,rho,beta,q] = lbfgs(H0, v, Y, S)
%LBFGS performs a simple L-BFGS update
%
% Input arguments:
% - H0: Initial Hessian estimate (matrix); typical choice: H0 = (s'y)/(y'y)
% where (s,y) is the most recent pair of s and y
% - v: the vector on which the L-BFGS Hessian estimate should be applied;
% the function will return r = Hk*v, for given v
% - Y and S: buffers: Y = [y(k-1), ..., y(0)], where y(k-1) = g(k) - g(k-1)
% and Sk = [s(k-1), ..., s(0)], where s(k-1) = x(k) - x(k-1)
@alphaville
alphaville / closures-examples.rs
Last active December 1, 2018 00:21
Using closures in structures (Rust)
// ----------------------------------------------------------------------------
// Boxed simple function
// ----------------------------------------------------------------------------
struct Chung<'a> {
close: Box<Fn(f64) -> f64 + 'a>,
}
impl<'a> Chung<'a> {
fn new<C>(f: C) -> Chung<'a>
where
@alphaville
alphaville / cvx_scs.m
Created July 4, 2017 15:36
Interface between CVX and SuperSCS (see https://github.com/kul-forbes/scs)
function shim = cvx_scs( shim )
% CVX_SOLVER_SHIM SeDuMi interface for CVX.
% This procedure returns a 'shim': a structure containing the necessary
% information CVX needs to use this solver in its modeling framework.
if ~isempty( shim.solve ),
return
end
if isempty( shim.name ),
@alphaville
alphaville / epipr_sqnorm.m
Created May 2, 2017 10:51
Projection onto the epigraph of the squared norm
function [x_, z_, details] = epipr_sqnorm(x,z)
if (x'*x <= z)
x_ = x; z_ = z;
return
end
theta = 1 - 2 * z;
[r, status] = cubic_roots(theta, x);
details.status = status;
% Pick the right root
for i=1:length(r),
function [controller, Tree, yalmip_struct] = ...
FormulateMJLSMPCLV(probStruct,sysStruct,LV)
% do_write_file = false;
N = probStruct.N;
Prob = probStruct.Prob;
A = sysStruct.A;
B = sysStruct.B;
xmin = sysStruct.xmin;
xmax = sysStruct.xmax;
umin = sysStruct.umin;
function fun_linear_system(t, x, A)
return A*x;
end
nx = 4;
eigens = diagm(-1.+(1:nx)/nx/4);
for i=1:2*floor(nx/2)
eigens[1,i]=0.4^(i/2);
eigens[i,1]=-0.4^(i/2);
end
function [x_star, details] = lasso_newton(A, y, lambda, x0, ops)
%LASSO_NEWTON solves the LASSO problem
%
% min_x ||Ax - y||^2 + lambda*||x||_1
%
%
% Debug mode
dbg = true;