Skip to content

Commit

Permalink
Merge branch 'env' of github.com:hlefebvr/idol into env
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Dec 11, 2024
2 parents 0b5fe4c + 3d3202d commit 129599d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class idol::CtrVersion : public Version {

void set_row(LinExpr<Var>&& t_row) { m_lhs = std::make_unique<LinExpr<Var>>(std::move(t_row)); }

bool has_row() const { return m_lhs != nullptr; }
bool has_row() const { return m_lhs.operator bool(); }

void set_row(const LinExpr<Var>& t_row) { m_lhs = std::make_unique<LinExpr<Var>>(t_row); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class idol::VarVersion : public Version {

void set_column(LinExpr<Ctr>&& t_column) { m_column = std::make_unique<LinExpr<Ctr>>(std::move(t_column)); }

bool has_column() const { return m_column != nullptr; }
bool has_column() const { return m_column.operator bool(); }

void set_column(const LinExpr<Ctr>& t_column) { m_column = std::make_unique<LinExpr<Ctr>>(t_column); }

Expand Down
1 change: 1 addition & 0 deletions lib/src/bilevel/optimizers/StrongDuality/StrongDuality.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// Created by henri on 29.11.24.
//
#include <cassert>
#include "idol/bilevel/optimizers/StrongDuality/StrongDuality.h"
#include "idol/bilevel/optimizers/StrongDuality/Optimizers_StrongDuality.h"
#include "idol/mixed-integer/modeling/models/KKT.h"
Expand Down
8 changes: 4 additions & 4 deletions lib/src/mixed-integer/modeling/models/KKT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ void idol::Reformulators::KKT::create_dual_constraints() {
std::move(dual_constraint_expression.affine().linear()),
Equal,
-dual_constraint_expression.affine().constant()),
"dual_constraint_" + var.name());
"dual_" + var.name());
} else {
m_dual_constraints[index] = QCtr(env,
TempQCtr(
std::move(dual_constraint_expression),
Equal),
"dual_constraint_" + var.name());
"dual_" + var.name());
}

}
Expand Down Expand Up @@ -533,9 +533,9 @@ void idol::Reformulators::KKT::add_strong_duality_reformulation(idol::Model &t_d
auto duality_gap = m_primal_objective - m_dual_objective;

if (duality_gap.has_quadratic()) {
t_destination.add_qctr(std::move(duality_gap), LessOrEqual);
t_destination.add_qctr(std::move(duality_gap), LessOrEqual, "strong_duality");
} else {
t_destination.add_ctr(std::move(duality_gap.affine()) <= 0);
t_destination.add_ctr(std::move(duality_gap.affine()) <= 0, "strong_duality");
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/mixed-integer/modeling/models/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ idol::Model::Model(idol::Model && t_src) noexcept
m_sense(t_src.m_sense),
m_variables(std::move(t_src.m_variables)),
m_constraints(std::move(t_src.m_constraints)),
m_qconstraints(std::move(t_src.m_qconstraints)),
m_optimizer_factory(std::move(t_src.m_optimizer_factory)),
m_objective(std::move(t_src.m_objective)),
m_rhs(std::move(t_src.m_rhs)),
Expand Down
36 changes: 22 additions & 14 deletions lib/src/mixed-integer/optimizers/padm/Formulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "idol/mixed-integer/modeling/constraints/TempCtr.h"

#include <utility>
#include <cassert>

idol::ADM::Formulation::Formulation(const Model& t_src_model,
Annotation<unsigned int> t_decomposition,
Expand Down Expand Up @@ -251,8 +252,6 @@ void idol::ADM::Formulation::set_penalty_in_all_sub_problems(const Var &t_var, d

void idol::ADM::Formulation::initialize_penalty_parameters(bool t_use_inverse_penalties) {

const unsigned int n_sub_problems = m_sub_problems.size();

for (auto& sub_problem : m_sub_problems) {

for (const auto& var : sub_problem.l1_epigraph_vars) {
Expand Down Expand Up @@ -417,32 +416,41 @@ void idol::ADM::Formulation::dispatch_qctr(const idol::Model &t_src_model,
}

auto& sub_problem = m_sub_problems[t_sub_problem_id];
std::optional<Ctr> new_linear_ctr;

if (!full_row) {

// here, we are still on RHS fixation

if (rhs_quad.empty_all()) {
sub_problem.model.add_ctr(TempCtr(LinExpr(lhs), type, rhs_quad.affine().constant()), t_ctr.name());
new_linear_ctr = sub_problem.model.add_ctr(TempCtr(LinExpr(lhs), type, rhs_quad.affine().constant()), t_ctr.name());
} else {
const auto c = sub_problem.model.add_ctr(TempCtr(std::move(lhs), type, 0), t_ctr.name());
sub_problem.rhs_fixations.emplace_back(c, std::move(rhs_quad));
new_linear_ctr = sub_problem.model.add_ctr(TempCtr(std::move(lhs), type, 0), t_ctr.name());
sub_problem.rhs_fixations.emplace_back(*new_linear_ctr, std::move(rhs_quad));
}

return;
}
} else {

// here, we are on LHS fixation
// here, we are on LHS fixation

if (full_row->has_quadratic()) {
// we have to add a quadratic constraint here
throw Exception("Quadratic constraints in fixed problems are not implemented");
}

new_linear_ctr = sub_problem.model.add_ctr(TempCtr(LinExpr<Var>(), type, 0.), t_ctr.name());
sub_problem.row_fixations.emplace_back(*new_linear_ctr, std::move(*full_row));

if (full_row->has_quadratic()) {
// we have to add a quadratic constraint here
throw Exception("Quadratic constraints in fixed problems are not implemented");
}

const auto c = sub_problem.model.add_ctr(TempCtr(LinExpr<Var>(), type, 0.), t_ctr.name());
sub_problem.row_fixations.emplace_back(c, std::move(*full_row));
assert(new_linear_ctr.has_value());

if (m_initial_penalty_parameters.has_value()) {
new_linear_ctr->set(*m_initial_penalty_parameters, t_ctr.get(*m_initial_penalty_parameters));
}

}

double idol::ADM::Formulation::evaluate(const QuadExpr<Var>& t_expr, const std::vector<Point<Var>>& t_primals) {
double result = t_expr.affine().constant();
for (const auto& [var, coeff] : t_expr.affine().linear()) {
Expand Down Expand Up @@ -479,7 +487,7 @@ idol::LinExpr<idol::Var> idol::ADM::Formulation::add_l1_vars(unsigned int t_ctr_
if (const auto it = m_l1_epigraph_vars.find(t_ctr_id) ; it != m_l1_epigraph_vars.end()) {
var = it->second;
} else {
var = Var(model.env(), 0, Inf, Continuous, 0);
var = Var(model.env(), 0, Inf, Continuous, 0, "l1_epigraph_" + std::to_string(t_ctr_id));
var->set(*m_initial_penalty_parameters, t_initial_penalty_parameter);
m_l1_epigraph_vars.emplace_hint(it, t_ctr_id, *var);
m_sub_problems[t_sub_problem_id].l1_epigraph_vars.emplace_back(*var);
Expand Down
5 changes: 3 additions & 2 deletions lib/src/mixed-integer/optimizers/padm/Optimizers_PADM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,12 @@ idol::Optimizers::PADM::solve_sub_problem(unsigned int t_sub_problem_id) {

auto& sub_problem = m_formulation.sub_problem(t_sub_problem_id);


//sub_problem.model.write("sub_problem_" + std::to_string(t_sub_problem_id) + "_" + std::to_string(m_outer_loop_iteration) + "_" + std::to_string(m_inner_loop_iterations) + ".lp");

sub_problem.model.optimizer().set_param_time_limit(get_remaining_time());
sub_problem.model.optimize();

//sub_problem.model.write("iter_" + std::to_string(m_outer_loop_iteration) + "_" + std::to_string(m_inner_loop_iterations) + ".sub_problem_" + std::to_string(t_sub_problem_id) + ".lp");

const auto status = sub_problem.model.get_status();

if (status != Optimal && status != Feasible) {
Expand Down

0 comments on commit 129599d

Please sign in to comment.