Skip to content

Commit

Permalink
tests with gurobi ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 28, 2024
1 parent c1a3616 commit 318c92d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 15 deletions.
32 changes: 32 additions & 0 deletions dev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,43 @@
#include "idol/mixed-integer/modeling/objects/Env.h"
#include "idol/mixed-integer/modeling/expressions/operations/operators.h"
#include "idol/mixed-integer/modeling/constraints/TempCtr.h"
#include "idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h"
#include <gurobi_c++.h>

using namespace idol;

int main(int t_argc, const char** t_argv) {

Env env;

Var x(env, 0, Inf, Continuous, 0., "x");
Var y(env, 0, Inf, Continuous, 0., "y");

Ctr c1(env, 120 * x + 210 * y <= 15000);
Ctr c2(env, 110 * x + 30 * y <= 4000);
Ctr c3(env, x + y <= 75);

Model model(env);
model.add(x);
model.add(y);
model.add(c1);
model.add(c2);
model.add(c3);
model.set_obj_expr(-143 * x - 60 * y);

model.use(Gurobi().with_logs(true).with_infeasible_or_unbounded_info(true).with_presolve(false));

model.write("model.lp");

model.optimize();

std::cout << model.get_status() << std::endl;

const auto dual_solution = save_dual(model);


/*
Env env;
Model model(env, Model::Storage::RowOriented);
const auto x = model.add_vars(Dim<1>(10), 0., 1., Continuous, 1, "x");
Expand All @@ -37,6 +68,7 @@ int main(int t_argc, const char** t_argv) {
model.set_storage(Model::Storage::Both);
model.dump();
*/

return 0;
}
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ add_library(idol STATIC
src/mixed-integer/modeling/constraints/Ctr.cpp
include/idol/general/optimizers/Optimizer.h
include/idol/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.h
include/idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h
include/idol/general/optimizers/OptimizerWithLazyUpdates.h
src/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.cpp
src/general/optimizers/Optimizer.cpp
include/idol/general/optimizers/Algorithm.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ void idol::OptimizerWithLazyUpdates<VarImplT, CtrImplT>::remove(const Var& t_var
hook_remove(m_variables[index].object());
}

m_variables[index] = std::move(m_variables.back());
m_variables.pop_back();
m_variables.erase(m_variables.begin() + index);
}

template<class VarImplT, class CtrImplT>
Expand All @@ -381,8 +380,7 @@ void idol::OptimizerWithLazyUpdates<VarImplT, CtrImplT>::remove(const Ctr& t_ctr
hook_remove(m_constraints[index].object());
}

m_constraints[index] = std::move(m_constraints.back());
m_constraints.pop_back();
m_constraints.erase(m_constraints.begin() + index);
}

#endif //IDOL_OPTIMIZERWITHLAZYUPDATES_H
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifdef IDOL_USE_GLPK

#include "idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h"
#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
#include <glpk.h>

namespace idol::Optimizers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <memory>
#include <variant>

#include "idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h"
#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
#include "idol/mixed-integer/optimizers/callbacks/Callback.h"
#include "GurobiCallbackI.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#ifdef IDOL_USE_HIGHS

#include "idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h"
#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
#include <Highs.h>
#include <stack>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#ifdef IDOL_USE_MOSEK

#include "idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h"
#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
#include "MosekCallbackI.h"
#include <fusion.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <OsiSolverInterface.hpp>
#include <stack>
#include "idol/mixed-integer/optimizers/wrappers/OptimizerWithLazyUpdates.h"
#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"

namespace idol::Optimizers {
class Osi;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/mixed-integer/modeling/models/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ void idol::Model::set_obj_expr(Expr<Var, Var> &&t_objective) {
version.set_obj(0.);
} else {
version.set_obj((*it).second);
++it;
}
}

Expand Down Expand Up @@ -501,6 +502,7 @@ void idol::Model::set_rhs_expr(LinExpr<Ctr> &&t_rhs) {
version.set_rhs(0.);
} else {
version.set_rhs((*it).second);
++it;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ idol::Optimizers::Gurobi::Gurobi(const Model &t_model, bool t_continuous_relaxat
m_model.set(GRB_DoubleParam_TimeLimit, get_param_time_limit());
m_model.set(GRB_IntParam_Presolve, get_param_presolve());
m_model.set(GRB_IntParam_Threads, (int) get_param_thread_limit());
m_model.set(GRB_IntParam_InfUnbdInfo, get_param_infeasible_or_unbounded_info());

// Tolerances
m_model.set(GRB_DoubleParam_MIPGap, get_tol_mip_relative_gap());
Expand Down Expand Up @@ -155,12 +156,12 @@ std::variant<GRBConstr, GRBQConstr> idol::Optimizers::Gurobi::hook_add(const Ctr
const auto rhs = model.get_ctr_rhs(t_ctr);
const auto& name = t_ctr.name();

GRBQuadExpr expr = 0.;
GRBLinExpr expr = 0.;
for (const auto &[var, constant]: row) {
expr.addTerm(constant, lazy(var).impl());
expr += constant * lazy(var).impl();
}

GUROBI_CATCH(return m_model.addQConstr(expr, type, rhs, name);)
GUROBI_CATCH(return m_model.addConstr(expr, type, rhs, name);)
}

void idol::Optimizers::Gurobi::hook_update(const Var& t_var) {
Expand Down Expand Up @@ -364,10 +365,10 @@ double idol::Optimizers::Gurobi::get_best_bound() const {
}

if (m_model.get(GRB_IntParam_SolutionNumber) == 0) {
return m_model.get(GRB_DoubleAttr_ObjBound);
GUROBI_CATCH(return m_model.get(GRB_DoubleAttr_ObjBound);)
}

return m_model.get(GRB_DoubleAttr_PoolObjBound);
GUROBI_CATCH(return m_model.get(GRB_DoubleAttr_PoolObjBound);)

}

Expand Down

0 comments on commit 318c92d

Please sign in to comment.