To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 8af6f3dc authored by roangel's avatar roangel
Browse files

added C libraries gurobi. Tested with dummy example

parent c625122e
......@@ -207,7 +207,9 @@ catkin_package(
LIBRARIES
CATKIN_DEPENDS roscpp rospy std_msgs rosbag roslib
DEPENDS
)
)
set(GUROBI_HOME /home/ppsteacher/Downloads/gurobi800/linux64)
###########
## Build ##
......@@ -222,7 +224,27 @@ include_directories(
${catkin_INCLUDE_DIRS}
include
include/nodes
)
${GUROBI_HOME}/include
)
find_library( GUROBI_LIBRARY
NAMES gurobi
gurobi45
gurobi46
gurobi50
gurobi51
gurobi52
gurobi55
gurobi56
gurobi60
gurobi75
gurobi80
libgurobi_c++.a
libgurobi60.so
libgurobi80.so
PATHS "${GUROBI_HOME}/lib"
)
## Declare a C++ library
##add_library(${PROJECT_NAME}
......@@ -328,7 +350,7 @@ target_link_libraries(PPSClient ${catkin_LIBRARIES})
target_link_libraries(SafeControllerService ${catkin_LIBRARIES})
target_link_libraries(DemoControllerService ${catkin_LIBRARIES})
target_link_libraries(StudentControllerService ${catkin_LIBRARIES})
target_link_libraries(MpcControllerService ${catkin_LIBRARIES} Eigen3::Eigen)
target_link_libraries(MpcControllerService ${catkin_LIBRARIES} Eigen3::Eigen ${GUROBI_LIBRARY})
target_link_libraries(CentralManagerService ${catkin_LIBRARIES})
target_link_libraries(ParameterService ${catkin_LIBRARIES})
......
......@@ -2,6 +2,11 @@
#include <unsupported/Eigen/MatrixFunctions>
#include <unsupported/Eigen/KroneckerProduct>
extern "C"
{
#include <gurobi_c.h>
}
using namespace Eigen;
using namespace std;
......@@ -310,5 +315,154 @@ VectorUtype mympc_varying_another(std::vector<MatrixAtype> A_tray, std::vector<M
// std::cout << "F: " << std::endl;
// std::cout << F.format(CleanFmt) << std::endl;
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double sol[3];
int ind[3];
double val[3];
int qrow[5];
int qcol[5];
double qval[5];
char vtype[3];
int optimstatus;
double objval;
/* Create environment */
error = GRBloadenv(&env, "qp.log");
if (error) goto QUIT;
/* Create an empty model */
error = GRBnewmodel(env, &model, "qp", 0, NULL, NULL, NULL, NULL, NULL);
if (error) goto QUIT;
/* Add variables */
error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL);
if (error) goto QUIT;
/* Quadratic objective terms */
qrow[0] = 0; qrow[1] = 0; qrow[2] = 1; qrow[3] = 1; qrow[4] = 2;
qcol[0] = 0; qcol[1] = 1; qcol[2] = 1; qcol[3] = 2; qcol[4] = 2;
qval[0] = 1; qval[1] = 1; qval[2] = 1; qval[3] = 1; qval[4] = 1;
error = GRBaddqpterms(model, 5, qrow, qcol, qval);
if (error) goto QUIT;
/* Linear objective term */
error = GRBsetdblattrelement(model, GRB_DBL_ATTR_OBJ, 0, 2.0);
if (error) goto QUIT;
/* First constraint: x + 2 y + 3 z <= 4 */
ind[0] = 0; ind[1] = 1; ind[2] = 2;
val[0] = 1; val[1] = 2; val[2] = 3;
error = GRBaddconstr(model, 3, ind, val, GRB_GREATER_EQUAL, 4.0, "c0");
if (error) goto QUIT;
/* Second constraint: x + y >= 1 */
ind[0] = 0; ind[1] = 1;
val[0] = 1; val[1] = 1;
error = GRBaddconstr(model, 2, ind, val, GRB_GREATER_EQUAL, 1.0, "c1");
if (error) goto QUIT;
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
/* Write model to 'qp.lp' */
error = GRBwrite(model, "qp.lp");
if (error) goto QUIT;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
/* Modify variable types */
vtype[0] = GRB_INTEGER; vtype[1] = GRB_INTEGER; vtype[2] = GRB_INTEGER;
error = GRBsetcharattrarray(model, GRB_CHAR_ATTR_VTYPE, 0, 3, vtype);
if (error) goto QUIT;
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
/* Write model to 'qp2.lp' */
error = GRBwrite(model, "qp2.lp");
if (error) goto QUIT;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" x=%.4f, y=%.4f, z=%.4f\n", sol[0], sol[1], sol[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free model */
GRBfreemodel(model);
/* Free environment */
GRBfreeenv(env);
return u_optimal;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment