Comments (5)
For compilation time I find that MX is often faster. A good compromise is to write the inner functions using SX and then write the outer problem in MX using vectorized operations (or map/fold functions if that is not straightforward).
For this specific case this will be fast for large N
import sys
import casadi as ca
N = int(sys.argv[1])
x = ca.MX.sym("x",N)
x0 = [1.0 if i % 2 == 1 else -1.2 for i in range(N)]
f = ca.sum1(100*(x[:-1]**2-x[1:])**2+(x[:-1]-1)**2)
g = 3*x[1:-1]**3 + 2*x[2:] - 5 + ca.sin(x[1:-1]-x[2:])*ca.sin(x[1:-1]+x[2:]) + 4*x[1:-1] - x[:-2]*ca.exp(x[:-2]-x[1:-1]) - 3
nlp = {'x':x, 'f':f, 'g':g}
options = {
"jit": True,
"compiler": "shell",
"jit_options": {
"flags": ["-O3"],
"verbose": True
},
"ipopt.linear_solver": "ma27",
}
solver = ca.nlpsol("solver", "ipopt", nlp, options)
solver(x0 = x0, lbg=0, ubg=0)
from casadi.
I have the same issue unfortunately... I was also wondering if there was an option or an alternative method to speed-up the compiling process, so I'll also be grateful for any advice!
from casadi.
Vectorizing the operation solved my issue. Thanks @victorfors! Closing the issue.
from casadi.
If g_i
were a bit more involved, the winner combination would typically be an inner SX Function with a map on top ( https://web.casadi.org/docs/#for-loop-equivalents ):
import sys
import casadi as ca
N = int(sys.argv[1])
x = ca.MX.sym("x",N)
x0 = [1.0 if i % 2 == 1 else -1.2 for i in range(N)]
f = sum(100*(x[i-1]**2-x[i])**2+(x[i-1]-1)**2 for i in range(1,N))
xi = ca.SX.sym("x[i]")
xip = ca.SX.sym("x[i+1]")
xipp = ca.SX.sym("x[i+2]")
gf = Function('g',[xi,xip,xipp],[3*xip**3 + 2*xipp - 5 + ca.sin(xip-xipp)*ca.sin(xip+xipp) + 4*xip - xi*ca.exp(xi-xip) - 3])
g = gf.map(N-2)(x[:-2],x[1:-1],x[2:])
nlp = {'x':x, 'f':f, 'g':g}
options = {
"jit": True,
"compiler": "shell",
"jit_cleanup": False,
"jit_options": {
"flags": ["-O3"],
"verbose": True,
},
"ipopt.linear_solver": "ma27",
}
solver = ca.nlpsol("solver", "ipopt", nlp, options)
solver(x0 = x0, lbg=0, ubg=0)
(The example has a lot of potential for further speedup of vectorized instructions, which has not been merged yet with main CasADi branch.)
from casadi.
Thanks for the comment @jgillis. I think you meant better runtime performance? I checked the SX approach you suggested, and it seems that the compilation time tends to be longer than the pure MX approach.
This is purely MX
time python luksan.py 400
real 0m1.107s
user 0m1.045s
sys 0m0.061s
time python luksan.py 800
real 0m1.121s
user 0m1.046s
sys 0m0.073s
luksan-2.py
is SX approach you suggested
time python luksan-2.py 400
real 0m12.617s
user 0m12.485s
sys 0m0.122s
time python luksan-2.py 800
real 0m38.007s
user 0m37.743s
sys 0m0.236s
SX approach seems to be similar to what we are doing at ExaModels.jl---our experimental algebraic modeling/AD implementation in Julia running on GPUs. Would you be able to explain why the SX approach is expected to have better performance than MX? And how is the new vectorized instruction under development different from MX? Sorry for lots of questions :)
from casadi.
Related Issues (20)
- libcasadi_interpolant_linear.dll not found HOT 1
- Difference between work vector size in generated header and c files HOT 1
- Set `MACOSX_DEPLOYMENT_TARGET` to 11.0 for CasADi v3.6.5 on arm64 macOS – `libcasadi.3.7.dylib` requires 11.1 and breaks linkage HOT 1
- Using casadi+integrator in a different thread
- compact dense sparsity patterns HOT 3
- Symmetric jacobian with MX
- pseudo MX optimizations HOT 1
- Add inlining MX call to SXFunction HOT 1
- undefined symbol: _ZN6casadi10differenceERKSt6vectorINS_2MXESaIS1_EES5_ HOT 11
- Performance difference between CasADi 3.5.5 and 3.6.4 HOT 4
- jtimes corner case
- Build Model error C2065: 'mxIsChar_is_not_supported_in_Simulink_Coder'
- Performance Degradation with Multithreading in Parametric Trajectory Optimization HOT 2
- MATLAB Coder meets CasADi codegen - macos - MEX, dylib, path error
- Fix fatrop printing
- undefined reference to `casadi::difference` when building in windows/mingw HOT 1
- Graceful failure when trying to load a co-simulation only FMU
- Using rootfinder object inside optimization fails HOT 2
- (Installation help) Python casadi, module not found _casadi on Ubuntu
- timer reset missing?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from casadi.