-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build/ | ||
.ccls-cache | ||
compile_commands.json | ||
.*.sw[a-z] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(madnlp_test) | ||
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) | ||
set(CMAKE_VERBOSE_MAKEFILE 1) | ||
set(VERBOSE 1) | ||
|
||
set(CASADI_DIR ${PROJECT_SOURCE_DIR}/../../casadi/build_madnlp/test/) | ||
set(MADNLP_DIR ${PROJECT_SOURCE_DIR}/../build/madnlp_c-0.8.3-1.0.0/) | ||
set(JULIA_DIR ${HOME}/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu) | ||
|
||
set(JULIA_INCLUDE_DIR ${JULIA_DIR}/include/julia/) | ||
set(CASADI_INCLUDE_DIR ${CASADI_DIR}/include) | ||
set(MADNLP_INCLUDE_DIR ${MADNLP_DIR}/include) | ||
|
||
set(CASADI_LIB_DIR ${CASADI_DIR}/lib/) | ||
set(MADNLP_LIB_DIR ${MADNLP_DIR}/lib/) | ||
|
||
include(LocalInclude OPTIONAL) | ||
|
||
add_compile_options(-Wall -ggdb -std=c++11) | ||
|
||
#set(CASADI_IPOPT_SOURCE casadi_ipopt.cpp) | ||
#add_executable(casadi_ipopt ${CASADI_IPOPT_SOURCE}) | ||
#target_compile_definitions(casadi_ipopt PUBLIC INCLUDE_DIR=\"${CASADI_INCLUDE_DIR}\") | ||
#target_include_directories(casadi_ipopt PUBLIC {CASADI_INCLUDE_DIR}) | ||
#target_link_libraries(casadi_ipopt -L${LIB_DIR} -lcasadi -lstdc++ -ldl -lm) | ||
|
||
add_executable(test_madnlp_c main.cpp) | ||
target_compile_definitions(test_madnlp_c PUBLIC INCLUDE_DIR=\"${CASADI_DIR}/include/\") | ||
# target_include_directories(test_madnlp_c PUBLIC ${CASADI_INCLUDE_DIR}) | ||
target_include_directories(test_madnlp_c PUBLIC ${MADNLP_INCLUDE_DIR}) | ||
target_include_directories(test_madnlp_c PUBLIC ${JULIA_INCLUDE_DIR}) | ||
target_link_libraries(test_madnlp_c -L${CASADI_LIB_DIR} -L${MADNLP_LIB_DIR} -lmadnlp_c -ljulia -lstdc++ -ldl -lm) | ||
|
||
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/sources/_all/casadi/build_madnlp/test/lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include "madnlp_c.h" | ||
#include <stdio.h> | ||
#include <algorithm> | ||
#include <iostream> | ||
|
||
extern "C" { | ||
int init_julia(int, char**); | ||
void shutdown_julia(int); | ||
} | ||
|
||
double a = 1.0; | ||
double b = 100.0; | ||
|
||
int eval_f(const double* w,double* f, void* user_data) { | ||
f[0] = (a-w[0])*(a-w[0]) + b*(w[1]-w[0]*w[0])*(w[1]-w[0]*w[0]); | ||
return 0; | ||
} | ||
int eval_g(const double* w,double* c, void* user_data) { | ||
c[0] = w[0]*w[0]+w[1]*w[1]-1; | ||
return 0; | ||
} | ||
int eval_grad_f(const double* w,double* g, void* user_data) { | ||
g[0] = -4*b*w[0]*(w[1]-w[0]*w[0])-2*(a-w[0]); | ||
g[1] = b*2*(w[1]-w[0]*w[0]); | ||
return 0; | ||
} | ||
int eval_jac_g(const double *w,double *j, void* user_data) { | ||
j[0] = 2*w[0]; | ||
j[1] = 2*w[1]; | ||
return 0; | ||
} | ||
int eval_h(double obj_scale, const double* w, const double* l, double* h, void* user_data) { | ||
// dw0dw0 | ||
h[0] = +2 -4*b*w[1] +12*b*w[0]*w[0]; | ||
// dw0dw1|dw1dw0 | ||
h[1] = -4*b*w[0]; | ||
// dw1dw1 | ||
h[2] = 2*b; | ||
|
||
// constraints | ||
h[0] += l[0]*2; | ||
h[2] += l[0]*2; | ||
|
||
return 0; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
init_julia(argc, argv); | ||
|
||
madnlp_int nw = 2; | ||
madnlp_int nc = 1; | ||
|
||
MadnlpCInterface interf; | ||
interf.eval_obj = eval_f; | ||
interf.eval_constr = eval_g; | ||
interf.eval_constr_jac = eval_jac_g; | ||
interf.eval_obj_grad = eval_grad_f; | ||
interf.eval_lag_hess = eval_h; | ||
|
||
interf.nw = nw; | ||
interf.nc = nc; | ||
madnlp_int nzj_i[2] = {1,1}; | ||
madnlp_int nzj_j[2] = {1,2}; | ||
madnlp_int nzh_i[3] = {1,1,2}; | ||
madnlp_int nzh_j[3] = {1,2,2}; | ||
interf.nzj_i = nzj_i; | ||
interf.nzj_j = nzj_j; | ||
interf.nzh_i = nzh_i; | ||
interf.nzh_j = nzh_j; | ||
|
||
interf.nnzj = 2; | ||
interf.nnzh = 3; | ||
|
||
double x0[2] = {1,1}; | ||
double l0[1] = {1}; | ||
double lbx[2] = {-100,-100}; | ||
double ubx[2] = {100,100}; | ||
double lbg[1] = {0}; | ||
double ubg[1] = {0}; | ||
|
||
printf("lbx %p, ubx %p\n", lbx, ubx); | ||
|
||
printf("interf %p\n", &interf); | ||
|
||
struct MadnlpCSolver* solver = madnlp_c_create(&interf); | ||
|
||
//madnlp_c_set_option_int(solver, "max_iter", 5); | ||
|
||
const MadnlpCNumericIn* in = madnlp_c_input(solver); | ||
std::copy(x0,x0+2,in->x0); | ||
std::copy(l0,l0+1,in->l0); | ||
std::copy(lbx,lbx+2,in->lbx); | ||
std::copy(ubx,ubx+2,in->ubx); | ||
std::copy(lbg,lbg+1,in->lbg); | ||
std::copy(ubg,ubg+1,in->ubg); | ||
|
||
madnlp_c_solve(solver); | ||
const MadnlpCNumericOut* out = madnlp_c_output(solver); | ||
|
||
double sol[2]; | ||
double cons[1]; | ||
std::copy(out->sol,out->sol+2,sol); | ||
std::copy(out->sol,out->sol+1,cons); | ||
|
||
std::cout << sol[0] << std::endl; | ||
std::cout << sol[1] << std::endl; | ||
|
||
shutdown_julia(0); | ||
|
||
return 0; | ||
} |