Skip to content

Commit

Permalink
readLP
Browse files Browse the repository at this point in the history
  • Loading branch information
hhijazi committed Sep 26, 2024
1 parent bbe3b67 commit 53124cc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 27 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ if(UNIX)
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -pthread")
endif()

set(CMAKE_XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "$ENV{DYLD_LIBRARY_PATH}")

add_subdirectory(src)
add_subdirectory(examples)
Expand Down
27 changes: 6 additions & 21 deletions cmake/FindGUROBI.cmake
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
set(GUROBI_ROOT_DIR "$ENV{GUROBI_ROOT_DIR}" CACHE PATH "Gurobi root directory.")
set(GUROBI_DIR "$ENV{GUROBI_ROOT_DIR}" CACHE PATH "Gurobi root directory.")
set(GUROBI_VERSION "120")
message("Gurobi root" ${GUROBI_ROOT_DIR})
if(APPLE)
file(GLOB dirs /Library/gurobi*)
foreach(d in ${dirs})
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
endforeach(d)
elseif(UNIX)
file(GLOB dirs ${GUROBI_ROOT_DIR}/gurobi*)
foreach(d in ${dirs})
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};/macos_universal2)
elseif(UNIX)
string(CONCAT GUROBI_DIR ${GUROBI_ROOT_DIR};/gurobi;${GUROBI_VERSION};/linux64)
endif()
message("Looking for Gurobi in ${GUROBI_DIR}")

string(SUBSTRING ${GUROBI_VERSION} 0 3 GUROBI_VERSION_SHORT)
Expand All @@ -26,17 +11,17 @@ message("Gurobi version short ${GUROBI_VERSION_SHORT}")

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)
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.dylib HINTS ${GUROBI_DIR}/lib/macos_universal2)
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib/macos_universal2")
elseif(UNIX)
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.so HINTS ${GUROBI_DIR}/lib)
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.so HINTS ${GUROBI_DIR}/lib/linux64)
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib/linux64")
endif()
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(GUROBI_INCLUDE_DIRS ${GUROBI_INCLUDE_DIR})
set(GUROBI_LIBRARIES ${GUROBI_CPP_LIBRARY} ${GUROBI_LIBRARY})
message("CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}")
Expand Down
6 changes: 2 additions & 4 deletions examples/Optimization/NonLinear/Power/ACOPF/ACOPF_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ int main (int argc, char * argv[])
}
#endif
Model<> M;
int status = M.readNL(fname);
solver<> GRB(M,gurobi);
GRB.run();
int status = GRB.readLP(fname);
M.print();
M.read_solution(fname);
M.write_solution(15);
// GRB.run();
// M.reset();
// M.is_feasible(1e-6);
return 0;
Expand Down
6 changes: 4 additions & 2 deletions include/gravity/GurobiProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ using namespace gravity;
class GurobiProgram: public Program<>{
private:

GRBModel* grb_mod;
GRBEnv* grb_env;


vector<GRBVar> _grb_vars; /** Mapping variables to Gurobi variables */
public:
GRBEnv* grb_env;
GRBModel* grb_mod;
Model<>* _model;
int _output;
GurobiProgram();
Expand Down
1 change: 1 addition & 0 deletions include/gravity/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -6738,6 +6738,7 @@ const bool var_compare(const pair<string,shared_ptr<param_>>& v1, const pair<str
template<typename T=type>
shared_ptr<Model<type>> buildOA();


/** Returns a model such that when optimized will return an iterior point to the current model**/
template<typename T=type>
Model<type> build_model_interior() const;
Expand Down
42 changes: 42 additions & 0 deletions include/gravity/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class solver {
_bool_options[option] = val;
}


unsigned get_nb_iterations(){
return _nb_iterations;
}
Expand Down Expand Up @@ -203,6 +204,47 @@ class solver {
}
//@}
void set_model(gravity::Model<type>& m);


int readLP(const string& fname) {
if(_stype==gurobi)
{
#ifdef USE_GUROBI
indices C("C"), I("I"), LinConstr("LinConstr"), QuadConstr("QuadConstr"), NonLinConstr("NonLinConstr");
int nb_cont = 0;
int nb_int = 0;
int nb_other = 0;
vector<int> C_ids,I_ids;
auto grb_prog = (GurobiProgram*)(_prog.get());
try{
auto mod = GRBModel(grb_prog->grb_env, fname);
int n = mod.get(GRB_IntAttr_NumVars);
auto vars = mod.getVars();
int nb_c = 0, nb_i = 0;
for (int i = 0; i < n; i++) {
if(vars[i].get(GRB_CharAttr_VType) == GRB_CONTINUOUS){
nb_c++;
C.insert(to_string(v.index()));
C_ids.push_back(v.index());
}
else if (vars[i].get(GRB_CharAttr_VType) == GRB_INTEGER || vars[i].get(GRB_CharAttr_VType) == GRB_BINARY) {
I.insert(to_string(v.index()));
I_ids.push_back(v.index());
nb_i++;
}
}
DebugOn("Model has " << nb_c << " continuous vars and " << nb_i << " integers" << endl);
}catch(GRBException e) {
cerr << "\nError code = " << e.getErrorCode() << endl;
cerr << e.getMessage() << endl;
}
#else
gurobiNotAvailable();
#endif
}
return 0;
}

int run(bool relax){
return run(5, 1e-6, 10000, 1e-6, relax, {false,""}, 1e+6);
}
Expand Down
1 change: 1 addition & 0 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6874,6 +6874,7 @@ void Model<type>::update_upper_bound(shared_ptr<Model<type>>& obbt_model, vector
}
}
}


template <typename type>
template<typename T,typename std::enable_if<is_arithmetic<T>::value>::type*>
Expand Down
3 changes: 3 additions & 0 deletions src/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ void ClpNotAvailable()
}

namespace gravity {



/** Returns copy of current Model which has all variables in current model, same objective as current model and only linear constraints in current Model. Throws exception if model has nonlinear equality constraints
**/
template<typename type>
Expand Down

0 comments on commit 53124cc

Please sign in to comment.