Skip to content

Instantly share code, notes, and snippets.

View jgillis's full-sized avatar

Joris Gillis jgillis

View GitHub Profile
import casadi
from casadi import *
from casadi.casadi import OPTI_INEQUALITY, Opti_bounded
import matplotlib.pyplot as plt
# https://epubs.siam.org/doi/pdf/10.1137/16M1062569
m1 = 1
m2 = 0.3
class Node:
def __init__(self,val):
self.val = val
self.nodes = []
class AutoBrancher:
OPEN = 0
DONE = 1
def __init__(self):
@jgillis
jgillis / gist:65fb76ccf1a8862f342d9f3400a88747
Created March 2, 2021 10:16
Hack around hardcoded paths in casadi dump files
Find and replace in casadi file:
casadi.Function('f',{x},{x},struct('dump_dir','matching_string')).save('test.casadi')
casadi.Function('f',{x},{x},struct('dump_dir','replace__string')).save('test2.casadi')
Check the difference in test and test2 to see the replacement.
Preservation of length only.
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));