Skip to content

Commit

Permalink
perpective cost works
Browse files Browse the repository at this point in the history
  • Loading branch information
hhijazi committed Jan 17, 2024
1 parent b1554ab commit faf002a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 40 deletions.
41 changes: 24 additions & 17 deletions cmake/FindGUROBI.cmake
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
set(GUROBI_ROOT_DIR "$ENV{GUROBI_ROOT_DIR}" CACHE PATH "Gurobi root directory.")
message("Gurobi root" ${GUROBI_ROOT_DIR})
if(APPLE)
file(GLOB dirs /Library/gurobi*)
file(GLOB dirs /Library/gurobi1*)
foreach(d in ${dirs})
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
endforeach(d)
elseif(UNIX)
file(GLOB dirs /opt/applications/gurobi/gurobi*)
file(GLOB dirs ${GUROBI_ROOT_DIR}/gurobi1*)
foreach(d in ${dirs})
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
endforeach(d)
endif()

message("Gurobi version ${GUROBI_VERSION}")
if(APPLE)
string(CONCAT GUROBI_DIR /Library/gurobi;${GUROBI_VERSION};/mac64)
string(CONCAT GUROBI_DIR2 /Library/gurobi;${GUROBI_VERSION};/macos_universal2)
string(CONCAT GUROBI_DIR /Library/gurobi;${GUROBI_VERSION};/macos_universal2)
elseif(UNIX)
string(CONCAT GUROBI_DIR /opt/applications/gurobi/gurobi;${GUROBI_VERSION};/linux64)
string(CONCAT GUROBI_DIR ${GUROBI_ROOT_DIR};/gurobi;${GUROBI_VERSION};/linux64)
endif()
message("Looking for Gurobi in ${GUROBI_DIR}")

string(SUBSTRING ${GUROBI_VERSION} 0 2 GUROBI_VERSION_SHORT)
string(SUBSTRING ${GUROBI_VERSION} 0 3 GUROBI_VERSION_SHORT)

message("Gurobi version short ${GUROBI_VERSION_SHORT}")

find_path(GUROBI_INCLUDE_DIR gurobi_c++.h HINTS "${GUROBI_DIR}/include" "${GUROBI_DIR2}/include")
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.dylib HINTS ${GUROBI_DIR}/lib ${GUROBI_DIR2}/lib)
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS ${GUROBI_DIR}/lib ${GUROBI_DIR2}/lib)

find_path(GUROBI_INCLUDE_DIR gurobi_c++.h HINTS "${GUROBI_DIR}/include")
if(APPLE)
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.dylib HINTS ${GUROBI_DIR}/lib)
elseif(UNIX)
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.so HINTS ${GUROBI_DIR}/lib)
endif()
message("GUROBI_DIR ${GUROBI_DIR}")
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib")
message("GUROBI_CPP_LIBRARY ${GUROBI_CPP_LIBRARY}")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_LIBRARY GUROBI_CPP_LIBRARY GUROBI_INCLUDE_DIR)

if(GUROBI_FOUND)
set(GRB_LICENSE_FILE "~/gurobi.research.lic")
set(GRB_LICENSE_FILE "~/gurobi/gurobi.lic")
set(GUROBI_INCLUDE_DIRS ${GUROBI_INCLUDE_DIR})
set(GUROBI_LIBRARIES ${GUROBI_CPP_LIBRARY} ${GUROBI_LIBRARY})
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(GUROBI_LIBRARIES "${GUROBI_LIBRARIES};m;pthread")
endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
message("CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}")
#if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# set(GUROBI_LIBRARIES "${GUROBI_LIBRARIES};m;pthread")
# endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif(GUROBI_FOUND)

mark_as_advanced(GUROBI_LIBRARY GUROBI_CPP_LIBRARY GUROBI_INCLUDE_DIR)
mark_as_advanced(GUROBI_LIBRARY GUROBI_CPP_LIBRARY GUROBI_INCLUDE_DIR)
21 changes: 11 additions & 10 deletions examples/Optimization/Linear/Power/DCOPF/DCOPF_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using namespace gravity;
int main (int argc, char * argv[])
{
int output = 0;
bool projected = false, use_cplex = false, use_gurobi = false;
bool projected = false, use_cplex = false, use_gurobi = true;
double tol = 1e-6;
double solver_time_end, total_time_end, solve_time, total_time;
string mehrotra = "no", log_level="0";
Expand Down Expand Up @@ -207,15 +207,16 @@ int main (int argc, char * argv[])
solve_time = solver_time_end - solver_time_start;
total_time = total_time_end - total_time_start;
}
// else if (use_gurobi) {
// solver DCOPF_GRB(DCOPF, gurobi);
// auto solver_time_start = get_wall_time();
// DCOPF_GRB.run(output, relax = false, tol = 1e-6);
// solver_time_end = get_wall_time();
// total_time_end = get_wall_time();
// solve_time = solver_time_end - solver_time_start;
// total_time = total_time_end - total_time_start;
// }
else if (use_gurobi) {
solver<> DCOPF_GRB(DCOPF, gurobi);
auto solver_time_start = get_wall_time();
bool relax = false;
DCOPF_GRB.run(output, relax = false, tol = 1e-6);
solver_time_end = get_wall_time();
total_time_end = get_wall_time();
solve_time = solver_time_end - solver_time_start;
total_time = total_time_end - total_time_start;
}
else {
solver<> DCOPF_IPT(DCOPF,ipopt);
auto solver_time_start = get_wall_time();
Expand Down
34 changes: 21 additions & 13 deletions examples/Optimization/MILP/SpaceTraffic/Sensor_Assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, const char * argv[]) {
var<> x("x",-1e4, 1e4), y("y", -1e4, 1e4), z("z", 1e3, 1e4);/*< (x,y,z) object coordinates */
var<> vx("vx", -1e2, 1e2), vy("vy", -1e2, 1e2), vz("vz", -1e2, 1e2);/*< (vx,vy,vz) object velocity */
var<> ux("ux", -1e4, 1e4), uy("uy", -1e4, 1e4), uz("uz", -1e4, 1e4);/*< (ux,uy,uz) thrust applied at given coordinates */
var<int> b("b", 0, 1);/*< if b_t = 1, thrust is applied at time step t */
var<> b("b", 0, 1);/*< if b_t = 1, thrust is applied at time step t */
// var<> ux("ux", 0, 0), uy("uy", 0, 0), uz("uz", -1e2, 1e2);/*< (ux,uy,uz) thrust applied at given coordinates */
/* Runge-Kutta auxiliary variables */
var<> k1_x("k1_x"), k2_x("k2_x"), k3_x("k3_x"), k4_x("k4_x");
Expand All @@ -66,50 +66,58 @@ int main(int argc, const char * argv[]) {

Mopt.add(x.in(T), y.in(T), z.in(T), vx.in(T), vy.in(T), vz.in(T), ux.in(T), uy.in(T), uz.in(T), k1_x.in(T), k2_x.in(T), k3_x.in(T), k4_x.in(T), k1_y.in(T), k2_y.in(T), k3_y.in(T), k4_y.in(T), k1_z.in(T), k2_z.in(T), k3_z.in(T), k4_z.in(T), k1_vx.in(T), k2_vx.in(T), k3_vx.in(T), k4_vx.in(T), k1_vy.in(T), k2_vy.in(T), k3_vy.in(T), k4_vy.in(T), k1_vz.in(T), k2_vz.in(T), k3_vz.in(T), k4_vz.in(T), r1.in(T), r2.in(T), r3.in(T), r4.in(T), rc1.in(T), rc2.in(T), rc3.in(T), rc4.in(T));

bool add_impulse_max = false;
var<> cost("cost", pos_);
bool add_impulse_max = true;
if(add_impulse_max){
int max_nb = 2; /*< max number of thrust impulses */
int max_nb = 50; /*< max number of thrust impulses */
double min_th = 200; /*< minimum thrust applied if impulse binary is one (using L2 norm on (ux,uy,uz)*/
Mopt.add(b.in(T));

Constraint<> thrust_on_off_ux("thrust_on_off_ux");
thrust_on_off_ux = ux - 1e4*b;
thrust_on_off_ux = ux - b;
Mopt.add(thrust_on_off_ux.in(T) <= 0);

Constraint<> thrust_on_off_uy("thrust_on_off_uy");
thrust_on_off_uy = uy - 1e4*b;
thrust_on_off_uy = uy - b;
Mopt.add(thrust_on_off_uy.in(T) <= 0);

Constraint<> thrust_on_off_uz("thrust_on_off_uz");
thrust_on_off_uz = uz - 1e4*b;
thrust_on_off_uz = uz - b;
Mopt.add(thrust_on_off_uz.in(T) <= 0);

Constraint<> thrust_on_off_ux_neg("thrust_on_off_ux_neg");
thrust_on_off_ux_neg = ux + 1e4*b;
thrust_on_off_ux_neg = ux + b;
Mopt.add(thrust_on_off_ux_neg.in(T) >= 0);

Constraint<> thrust_on_off_uy_neg("thrust_on_off_uy_neg");
thrust_on_off_uy_neg = uy + 1e4*b;
thrust_on_off_uy_neg = uy + b;
Mopt.add(thrust_on_off_uy_neg.in(T) >= 0);

Constraint<> thrust_on_off_uz_neg("thrust_on_off_uz_neg");
thrust_on_off_uz_neg = uz + 1e4*b;
thrust_on_off_uz_neg = uz + b;
Mopt.add(thrust_on_off_uz_neg.in(T) >= 0);

Constraint<> max_impulses("max_impulses");
max_impulses = sum(b) - max_nb;
Mopt.add(max_impulses <= 0);
// var<> ux_b("ux_b", pos_), uy_b("uy_b", pos_), uz_b("uz_b", pos_);
// Mopt.add(ux_b.in(T), uy_b.in(T), ux_b.in(T))

Constraint<> min_thrust("min_thrust");
min_thrust = ux*ux + uy*uy + uz*uz - min_th*min_th;
Mopt.add(min_thrust.in(T) >= 0);
Mopt.add(cost.in(T));
Constraint<> cost_perspective("cost_perspective");
cost_perspective = b*cost - (ux*ux + uy*uy + uz*uz);
Mopt.add(cost_perspective.in(T) >= 0);

// Constraint<> min_thrust("min_thrust");
// min_thrust = ux*ux + uy*uy + uz*uz - min_th*min_th;
// Mopt.add(min_thrust.in(T) >= 0);


}


/* Objective */
Mopt.min(sum(ux*ux) + sum(uy*uy) + sum(uz*uz));
Mopt.min(sum(cost));

/* Constraints */

Expand Down

0 comments on commit faf002a

Please sign in to comment.