Skip to content

Commit 53124cc

Browse files
committed
readLP
1 parent bbe3b67 commit 53124cc

File tree

8 files changed

+60
-27
lines changed

8 files changed

+60
-27
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ if(UNIX)
362362
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -pthread")
363363
endif()
364364

365+
set(CMAKE_XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "$ENV{DYLD_LIBRARY_PATH}")
365366

366367
add_subdirectory(src)
367368
add_subdirectory(examples)

cmake/FindGUROBI.cmake

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
set(GUROBI_ROOT_DIR "$ENV{GUROBI_ROOT_DIR}" CACHE PATH "Gurobi root directory.")
2+
set(GUROBI_DIR "$ENV{GUROBI_ROOT_DIR}" CACHE PATH "Gurobi root directory.")
3+
set(GUROBI_VERSION "120")
24
message("Gurobi root" ${GUROBI_ROOT_DIR})
3-
if(APPLE)
4-
file(GLOB dirs /Library/gurobi*)
5-
foreach(d in ${dirs})
6-
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
7-
endforeach(d)
8-
elseif(UNIX)
9-
file(GLOB dirs ${GUROBI_ROOT_DIR}/gurobi*)
10-
foreach(d in ${dirs})
11-
string(REGEX MATCH "[0-9]+" GUROBI_VERSION "${d}")
12-
endforeach(d)
13-
endif()
14-
155
message("Gurobi version ${GUROBI_VERSION}")
16-
if(APPLE)
17-
string(CONCAT GUROBI_DIR /Library/gurobi;${GUROBI_VERSION};/macos_universal2)
18-
elseif(UNIX)
19-
string(CONCAT GUROBI_DIR ${GUROBI_ROOT_DIR};/gurobi;${GUROBI_VERSION};/linux64)
20-
endif()
216
message("Looking for Gurobi in ${GUROBI_DIR}")
227

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

2712
find_path(GUROBI_INCLUDE_DIR gurobi_c++.h HINTS "${GUROBI_DIR}/include")
2813
if(APPLE)
29-
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.dylib HINTS ${GUROBI_DIR}/lib)
14+
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.dylib HINTS ${GUROBI_DIR}/lib/macos_universal2)
15+
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib/macos_universal2")
3016
elseif(UNIX)
31-
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.so HINTS ${GUROBI_DIR}/lib)
17+
find_library(GUROBI_LIBRARY libgurobi${GUROBI_VERSION_SHORT}.so HINTS ${GUROBI_DIR}/lib/linux64)
18+
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib/linux64")
3219
endif()
33-
find_library(GUROBI_CPP_LIBRARY libgurobi_c++.a HINTS "${GUROBI_DIR}/lib")
3420
message("GUROBI_CPP_LIBRARY ${GUROBI_CPP_LIBRARY}")
3521
include(FindPackageHandleStandardArgs)
3622
find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_LIBRARY GUROBI_CPP_LIBRARY GUROBI_INCLUDE_DIR)
3723

3824
if(GUROBI_FOUND)
39-
set(GRB_LICENSE_FILE "~/gurobi.research.lic")
4025
set(GUROBI_INCLUDE_DIRS ${GUROBI_INCLUDE_DIR})
4126
set(GUROBI_LIBRARIES ${GUROBI_CPP_LIBRARY} ${GUROBI_LIBRARY})
4227
message("CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}")

examples/Optimization/NonLinear/Power/ACOPF/ACOPF_main.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ int main (int argc, char * argv[])
6161
}
6262
#endif
6363
Model<> M;
64-
int status = M.readNL(fname);
6564
solver<> GRB(M,gurobi);
66-
GRB.run();
65+
int status = GRB.readLP(fname);
6766
M.print();
68-
M.read_solution(fname);
69-
M.write_solution(15);
67+
// GRB.run();
7068
// M.reset();
7169
// M.is_feasible(1e-6);
7270
return 0;

include/gravity/GurobiProgram.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ using namespace gravity;
1111
class GurobiProgram: public Program<>{
1212
private:
1313

14-
GRBModel* grb_mod;
15-
GRBEnv* grb_env;
14+
15+
1616
vector<GRBVar> _grb_vars; /** Mapping variables to Gurobi variables */
1717
public:
18+
GRBEnv* grb_env;
19+
GRBModel* grb_mod;
1820
Model<>* _model;
1921
int _output;
2022
GurobiProgram();

include/gravity/model.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6738,6 +6738,7 @@ const bool var_compare(const pair<string,shared_ptr<param_>>& v1, const pair<str
67386738
template<typename T=type>
67396739
shared_ptr<Model<type>> buildOA();
67406740

6741+
67416742
/** Returns a model such that when optimized will return an iterior point to the current model**/
67426743
template<typename T=type>
67436744
Model<type> build_model_interior() const;

include/gravity/solver.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class solver {
116116
_bool_options[option] = val;
117117
}
118118

119+
119120
unsigned get_nb_iterations(){
120121
return _nb_iterations;
121122
}
@@ -203,6 +204,47 @@ class solver {
203204
}
204205
//@}
205206
void set_model(gravity::Model<type>& m);
207+
208+
209+
int readLP(const string& fname) {
210+
if(_stype==gurobi)
211+
{
212+
#ifdef USE_GUROBI
213+
indices C("C"), I("I"), LinConstr("LinConstr"), QuadConstr("QuadConstr"), NonLinConstr("NonLinConstr");
214+
int nb_cont = 0;
215+
int nb_int = 0;
216+
int nb_other = 0;
217+
vector<int> C_ids,I_ids;
218+
auto grb_prog = (GurobiProgram*)(_prog.get());
219+
try{
220+
auto mod = GRBModel(grb_prog->grb_env, fname);
221+
int n = mod.get(GRB_IntAttr_NumVars);
222+
auto vars = mod.getVars();
223+
int nb_c = 0, nb_i = 0;
224+
for (int i = 0; i < n; i++) {
225+
if(vars[i].get(GRB_CharAttr_VType) == GRB_CONTINUOUS){
226+
nb_c++;
227+
C.insert(to_string(v.index()));
228+
C_ids.push_back(v.index());
229+
}
230+
else if (vars[i].get(GRB_CharAttr_VType) == GRB_INTEGER || vars[i].get(GRB_CharAttr_VType) == GRB_BINARY) {
231+
I.insert(to_string(v.index()));
232+
I_ids.push_back(v.index());
233+
nb_i++;
234+
}
235+
}
236+
DebugOn("Model has " << nb_c << " continuous vars and " << nb_i << " integers" << endl);
237+
}catch(GRBException e) {
238+
cerr << "\nError code = " << e.getErrorCode() << endl;
239+
cerr << e.getMessage() << endl;
240+
}
241+
#else
242+
gurobiNotAvailable();
243+
#endif
244+
}
245+
return 0;
246+
}
247+
206248
int run(bool relax){
207249
return run(5, 1e-6, 10000, 1e-6, relax, {false,""}, 1e+6);
208250
}

src/model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6874,6 +6874,7 @@ void Model<type>::update_upper_bound(shared_ptr<Model<type>>& obbt_model, vector
68746874
}
68756875
}
68766876
}
6877+
68776878

68786879
template <typename type>
68796880
template<typename T,typename std::enable_if<is_arithmetic<T>::value>::type*>

src/solver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ void ClpNotAvailable()
5959
}
6060

6161
namespace gravity {
62+
63+
64+
6265
/** 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
6366
**/
6467
template<typename type>

0 commit comments

Comments
 (0)