Skip to content

Instantly share code, notes, and snippets.

View jgillis's full-sized avatar

Joris Gillis jgillis

View GitHub Profile
from casadi import *
x = MX.sym('x')
y = MX.sym('y')
p = MX.sym('p')
nlp = {"x":vertcat(x,y),"p":p,"f":(x-2)**4+(y-3)**2}
solver = nlpsol('solver','ipopt',nlp)
#
# This file is part of rockit.
#
# rockit -- Rapid Optimal Control Kit
# Copyright (C) 2019 MECO, KU Leuven. All rights reserved.
#
# Rockit is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
import casadi.*
x = MX.sym('x');
y = MX.sym('y');
xy = [x;y];
p = MX.sym('p');
nlp = struct;
nlp.x = xy;
@jgillis
jgillis / demo.py
Last active June 29, 2020 19:04
Couenne solver via AMPL mod export
from casadi import SX, vertcat, inf
# Declare variables
x = SX.sym("x")
y = SX.sym("y")
z = SX.sym("z")
# Formulate the NLP
f = x**2 + 100*z**2
g = z + (1-x)**2 - y
@jgillis
jgillis / codegen.py
Created June 7, 2020 17:34
Calling CasADi codegen from Python with Ctypes
import numpy as np
from ctypes import *
class CasadiSparsity:
"""
Compressed Columun storage sparsity pattern
"""
def __init__(self,sp):
self.n_row = sp[0]
self.n_col = sp[1]
@jgillis
jgillis / test.py
Created May 7, 2020 19:50
Custom stopping criterion
from casadi import *
x=SX.sym("x")
y=SX.sym("y")
f = (1-x)**2+100*(y-x**2)**2
nlp={'x':vertcat(x,y), 'f':f,'g':x+y}
fcn = Function('f', [x, y], [f])
callback_inputs = dict()
@jgillis
jgillis / demo.m
Last active September 24, 2020 21:29
retrieving nlpsol stats with to_function
import casadi.*
opti = Opti();
x = opti.variable(2);
fx = sin(x)-[2;0.3];
opti.minimize(sumsqr(fx));
@jgillis
jgillis / test.m
Created April 23, 2020 21:34
Quadrature integration
import casadi.*
% Per https://en.wikipedia.org/wiki/Numerical_integration
% F(x) = int_a^x f(u) du
% <-> dF(x)/dx = f(x), F(a)=0
f = @(x) x.^5;
interv = [1.1 2.3];
from casadi import *
opti = Opti()
N = 5
degree = 3
grid = np.linspace(0, 6, N)
# knots ~ grid but with multiplicity
@jgillis
jgillis / test.m
Last active February 28, 2020 11:21
Going from piecewise polynomials to CasADi BSplines
close all
% casadi.interpolant creates 'knots' and 'coefficients' out of data and passes it on to casadi.Function.bspline
% Here, we supply knots and coefficients directly to casadi.Function.bspline
%% Example 1: discontinuous example, just to make clear the interpretation
% of the pp = piecewise polynomial form clear
% 4 intervals: 0-1, 1-2, 2-4, 4-6