Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Nov 19, 2024
1 parent 882107b commit 5d386c1
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 134 deletions.
12 changes: 3 additions & 9 deletions examples/bilevel/mibs-from-file.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@
//
#include <iostream>
#include "idol/modeling.h"
#include "idol/modeling/bilevel-optimization/read_from_file.h"
#include "idol/bilevel/modeling/read_from_file.h"
#include "idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h"
#include "idol/optimizers/bilevel-optimization/wrappers/MibS/MibS.h"
#include "idol/mixed-integer/optimizers/wrappers/Osi/Osi.h"
#include "idol/bilevel/optimizers/wrappers/MibS/MibS.h"

using namespace idol;

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

// const std::string aux_filename = "/home/henri/Research/counterfactual/code/bobilib/data/miblp_20_20_50_0110_15_5.aux";
const std::string aux_filename = "mibs-from-file.data.aux";

Env env;
auto [model, description] = Bilevel::read_from_file<Gurobi>(env, aux_filename);

model.use(
Bilevel::MibS(description)
.with_logs(true)
.with_osi_interface(OsiClpSolverInterface())
);
model.use(Bilevel::MibS(description).with_logs(true));

model.optimize();

Expand Down
10 changes: 4 additions & 6 deletions examples/bilevel/mibs.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
//
#include <iostream>
#include <Research/idol/lib/include/idol/modeling.h>
#include "idol/optimizers/bilevel-optimization/wrappers/MibS/MibS.h"
#include "idol/modeling/bilevel-optimization/LowerLevelDescription.h"
#include "idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h"
#include "idol/bilevel/optimizers/wrappers/MibS/MibS.h"
#include "idol/bilevel/modeling/LowerLevelDescription.h"

using namespace idol;

Expand Down Expand Up @@ -33,10 +32,9 @@ int main(int t_argc, const char** t_argv) {
// Define High Point Relaxation
Model high_point_relaxation(env);

auto x = high_point_relaxation.add_var(0, Inf, Integer, "x");
auto y = high_point_relaxation.add_var(0, Inf, Integer, "y");
auto x = high_point_relaxation.add_var(0, Inf, Integer, -1, "x");
auto y = high_point_relaxation.add_var(0, Inf, Integer, -10, "y");

high_point_relaxation.set_obj_expr(-x - 10 * y);
auto follower_c1 = high_point_relaxation.add_ctr(-25 * x + 20 * y <= 30);
auto follower_c2 = high_point_relaxation.add_ctr(x + 2 * y <= 10);
auto follower_c3 = high_point_relaxation.add_ctr(2 * x - y <= 15);
Expand Down
15 changes: 7 additions & 8 deletions examples/bilevel/padm.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
//
#include <iostream>
#include <idol/modeling.h>
#include <idol/modeling/bilevel-optimization/LowerLevelDescription.h>
#include <idol/modeling/models/KKT.h>
#include <idol/bilevel/modeling/LowerLevelDescription.h>
#include <idol/mixed-integer/modeling/models/KKT.h>
#include <idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h>
#include "idol/mixed-integer/optimizers/padm/PADM.h"
#include "idol/mixed-integer/optimizers/padm/SubProblem.h"
#include "idol/mixed-integer/optimizers/padm/PenaltyUpdates.h"

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


using namespace idol;

Env env;
Model model(env);
const auto x = model.add_var(0, Inf, Continuous, "x");
const auto y = model.add_var(0, Inf, Continuous, "y");
const auto delta = model.add_var(-Inf, Inf, Continuous, "delta");
const auto x = model.add_var(0, Inf, Continuous, 0., "x");
const auto y = model.add_var(0, Inf, Continuous, 0., "y");
const auto delta = model.add_var(-Inf, Inf, Continuous, 0., "delta");

model.set_obj_expr(delta * delta);

auto c = model.add_ctr(x + delta * x + y <= 1);
auto c = model.add_qctr(x + delta * x + y - 1, LessOrEqual);
model.add_ctr(y == 1);

Bilevel::LowerLevelDescription description(env);
description.make_follower_var(x);
description.make_follower_var(y);
description.make_follower_ctr(c);
//description.make_follower_ctr(c);
description.set_follower_obj_expr(-2 * x - y);

Reformulators::KKT reformulator(model, description);
Expand Down
2 changes: 1 addition & 1 deletion examples/mixed-integer/assignment-penalty-bap.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int t_argc, const char** t_argv) {
Annotation<Ctr, bool> penalized_constraints(env, "penalized_constraints", false);

// Create assignment variables (x_ij binaries)
auto x = model.add_vars(Dim<2>(n_agents, n_jobs), 0., 1., Binary, "x");
auto x = model.add_vars(Dim<2>(n_agents, n_jobs), 0., 1., Binary, 0., "x");

// Create knapsack constraints (i.e., capacity constraints)
for (unsigned int i = 0 ; i < n_agents ; ++i) {
Expand Down
17 changes: 3 additions & 14 deletions lib/include/idol/mixed-integer/modeling/constraints/TempCtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,9 @@ class idol::TempCtr {

};

idol::TempCtr operator<=(idol::AffExpr<idol::Var, double>&& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator<=(const idol::AffExpr<idol::Var, double>& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator<=(idol::AffExpr<idol::Var, double>&& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);
idol::TempCtr operator<=(const idol::AffExpr<idol::Var, double>& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);

idol::TempCtr operator>=(idol::AffExpr<idol::Var, double>&& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator>=(const idol::AffExpr<idol::Var, double>& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator>=(idol::AffExpr<idol::Var, double>&& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);
idol::TempCtr operator>=(const idol::AffExpr<idol::Var, double>& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);

idol::TempCtr operator==(idol::AffExpr<idol::Var, double>&& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator==(const idol::AffExpr<idol::Var, double>& t_lhs, idol::AffExpr<idol::Var, double>&& t_rhs);
idol::TempCtr operator==(idol::AffExpr<idol::Var, double>&& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);
idol::TempCtr operator==(const idol::AffExpr<idol::Var, double>& t_lhs, const idol::AffExpr<idol::Var, double>& t_rhs);
idol::TempCtr operator<=(idol::AffExpr<idol::Var, double> t_lhs, idol::AffExpr<idol::Var, double> t_rhs);
idol::TempCtr operator>=(idol::AffExpr<idol::Var, double> t_lhs, idol::AffExpr<idol::Var, double> t_rhs);
idol::TempCtr operator==(idol::AffExpr<idol::Var, double> t_lhs, idol::AffExpr<idol::Var, double> t_rhs);

namespace idol {
std::ostream &operator<<(std::ostream &t_os, const TempCtr &t_temp_ctr);
Expand Down
15 changes: 0 additions & 15 deletions lib/include/idol/mixed-integer/modeling/constraints/TempQCtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ class idol::TempQCtr {

};

idol::TempQCtr operator<=(idol::QuadExpr<idol::Var, double>&& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator<=(const idol::QuadExpr<idol::Var, double>& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator<=(idol::QuadExpr<idol::Var, double>&& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);
idol::TempQCtr operator<=(const idol::QuadExpr<idol::Var, double>& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);

idol::TempQCtr operator>=(idol::QuadExpr<idol::Var, double>&& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator>=(const idol::QuadExpr<idol::Var, double>& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator>=(idol::QuadExpr<idol::Var, double>&& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);
idol::TempQCtr operator>=(const idol::QuadExpr<idol::Var, double>& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);

idol::TempQCtr operator==(idol::QuadExpr<idol::Var, double>&& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator==(const idol::QuadExpr<idol::Var, double>& t_lhs, idol::QuadExpr<idol::Var, double>&& t_rhs);
idol::TempQCtr operator==(idol::QuadExpr<idol::Var, double>&& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);
idol::TempQCtr operator==(const idol::QuadExpr<idol::Var, double>& t_lhs, const idol::QuadExpr<idol::Var, double>& t_rhs);

namespace idol {
std::ostream &operator<<(std::ostream &t_os, const TempQCtr &t_temp_ctr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class idol::QuadExpr : public LinExpr<CommutativePair<KeyT>, ValueT> {
AffExpr<KeyT, ValueT> m_affine;
public:
QuadExpr() = default;
QuadExpr(const KeyT& t_key) : m_affine(t_key) {}
QuadExpr(KeyT&& t_key) : m_affine(std::move(t_key)) {}
QuadExpr(const ValueT& t_value) : m_affine(t_value) {}
QuadExpr(ValueT&& t_value) : m_affine(std::move(t_value)) {}
QuadExpr(const LinExpr<KeyT, ValueT>& t_expr) : m_affine(t_expr) {} // NOLINT(*-explicit-constructor)
QuadExpr(LinExpr<KeyT, ValueT>&& t_expr) : m_affine(std::move(t_expr)) {} // NOLINT(*-explicit-constructor)
QuadExpr(const AffExpr<KeyT, ValueT>& t_expr) : m_affine(t_expr) {} // NOLINT(*-explicit-constructor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,34 @@ namespace idol {
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator+(const KeyT& t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_b));
result += LinExpr<KeyT, ValueT>(t_a);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator+(QuadExpr<KeyT, ValueT>&& t_b, const KeyT& t_a) {
QuadExpr<KeyT, ValueT> result(std::move(t_b));
result += LinExpr<KeyT, ValueT>(t_a);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator+(const KeyT& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(t_b);
result += LinExpr<KeyT, ValueT>(t_a);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator+(const QuadExpr<KeyT, ValueT>& t_b, const KeyT& t_a) {
QuadExpr<KeyT, ValueT> result(t_b);
result += LinExpr<KeyT, ValueT>(t_a);
return result;
}

// Subtraction

// LinExpr
Expand Down Expand Up @@ -678,6 +706,13 @@ namespace idol {
return result;
}

template<class KeyT, class ValueT>
AffExpr<KeyT, ValueT> operator-(AffExpr<KeyT, ValueT>&& t_a, AffExpr<KeyT, ValueT>&& t_b) {
AffExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
AffExpr<KeyT, ValueT> operator-(AffExpr<KeyT, ValueT>&& t_a, const AffExpr<KeyT, ValueT>& t_b) {
AffExpr<KeyT, ValueT> result(std::move(t_a));
Expand All @@ -694,7 +729,139 @@ namespace idol {

// QuadExpr

// TODO
template<class KeyT, class ValutT>
QuadExpr<KeyT, ValutT> operator-(const QuadExpr<KeyT, ValutT>& t_expr) {
QuadExpr<KeyT, ValutT> result;
result -= t_expr;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(QuadExpr<KeyT, ValueT>&& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(QuadExpr<KeyT, ValueT>&& t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, AffExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(AffExpr<KeyT, ValueT>&& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, LinExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(LinExpr<KeyT, ValueT>&& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, const AffExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const AffExpr<KeyT, ValueT>& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(QuadExpr<KeyT, ValueT>&& t_a, const AffExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const AffExpr<KeyT, ValueT>& t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(QuadExpr<KeyT, ValueT>&& t_a, LinExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(LinExpr<KeyT, ValueT>&& t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= std::move(t_b);
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const QuadExpr<KeyT, ValueT>& t_a, double t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(const ValueT& t_a, const QuadExpr<KeyT, ValueT>& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(QuadExpr<KeyT, ValueT>&& t_a, double t_b) {
QuadExpr<KeyT, ValueT> result(std::move(t_a));
result -= t_b;
return result;
}

template<class KeyT, class ValueT>
QuadExpr<KeyT, ValueT> operator-(double t_a, QuadExpr<KeyT, ValueT>&& t_b) {
QuadExpr<KeyT, ValueT> result(t_a);
result -= std::move(t_b);
return result;
}

}

#endif //IDOL_OPERATORS_H
Loading

0 comments on commit 5d386c1

Please sign in to comment.