Skip to content

Commit

Permalink
make Annotation type free
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Nov 20, 2024
1 parent 5d386c1 commit 9cc8a84
Show file tree
Hide file tree
Showing 43 changed files with 227 additions and 323 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ auto follower_c3 = high_point_relaxation.add_ctr(2 * x - y <= 15);
auto follower_c4 = high_point_relaxation.add_ctr(2 * x + 10 * y >= 15);
// Prepare bilevel description
Bilevel::LowerLevelDescription description(env);
description.set_follower_obj_expr(y);
Bilevel::Description description(env);
description.set_lower_objective(y);
description.set_follower_var(y);
description.set_follower_ctr(follower_c1);
description.set_follower_ctr(follower_c2);
Expand Down
16 changes: 8 additions & 8 deletions examples/bilevel/kkt.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <Research/idol/lib/include/idol/modeling.h>
#include "idol/bilevel/optimizers/wrappers/MibS/MibS.h"
#include "idol/bilevel/modeling/LowerLevelDescription.h"
#include "idol/bilevel/modeling/Description.h"
#include "idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h"
#include "idol/bilevel/modeling/read_from_file.h"
#include "idol/mixed-integer/modeling/models/KKT.h"
Expand Down Expand Up @@ -48,13 +48,13 @@ int main(int t_argc, const char** t_argv) {
high_point_relaxation.add_ctr(-x + 5 * y <= 12.5);

// Prepare bilevel description
Bilevel::LowerLevelDescription description(env);
description.set_follower_obj_expr(- y);
description.make_follower_var(y);
description.make_follower_ctr(follower_c1);
description.make_follower_ctr(follower_c2);
description.make_follower_ctr(follower_c3);
description.make_follower_ctr(follower_c4);
Bilevel::Description description(env);
description.set_lower_objective(-y);
description.make_lower_level(y);
description.make_follower(follower_c1);
description.make_follower(follower_c2);
description.make_follower(follower_c3);
description.make_follower(follower_c4);

Reformulators::KKT reformulator(high_point_relaxation, description);

Expand Down
16 changes: 8 additions & 8 deletions examples/bilevel/mibs.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <Research/idol/lib/include/idol/modeling.h>
#include "idol/bilevel/optimizers/wrappers/MibS/MibS.h"
#include "idol/bilevel/modeling/LowerLevelDescription.h"
#include "idol/bilevel/modeling/Description.h"

using namespace idol;

Expand Down Expand Up @@ -41,13 +41,13 @@ int main(int t_argc, const char** t_argv) {
auto follower_c4 = high_point_relaxation.add_ctr(2 * x + 10 * y >= 15);

// Prepare bilevel description
Bilevel::LowerLevelDescription description(env);
description.set_follower_obj_expr(y);
description.make_follower_var(y);
description.make_follower_ctr(follower_c1);
description.make_follower_ctr(follower_c2);
description.make_follower_ctr(follower_c3);
description.make_follower_ctr(follower_c4);
Bilevel::Description description(env);
description.set_lower_objective(y);
description.make_lower_level(y);
description.make_follower(follower_c1);
description.make_follower(follower_c2);
description.make_follower(follower_c3);
description.make_follower(follower_c4);

// Use coin-or/MibS as external solver
high_point_relaxation.use(
Expand Down
16 changes: 8 additions & 8 deletions examples/bilevel/padm.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
#include <iostream>
#include <idol/modeling.h>
#include <idol/bilevel/modeling/LowerLevelDescription.h>
#include <idol/bilevel/modeling/Description.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"
Expand All @@ -25,23 +25,23 @@ int main(int t_argc, const char** t_argv) {
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.set_follower_obj_expr(-2 * x - y);
Bilevel::Description description(env);
description.make_lower_level(x);
description.make_lower_level(y);
description.make_lower_level(c);
description.set_lower_objective(-2 * x - y);

Reformulators::KKT reformulator(model, description);

Model single_level(env);
reformulator.add_strong_duality_reformulation(single_level);

Annotation<Var, unsigned int> decomposition(env, "sub_problem");
Annotation<unsigned int> decomposition(env, "sub_problem");
for (const auto& var : single_level.vars()) {
var.set(decomposition, var.id() == delta.id());
}

Annotation<Ctr, bool> penalized_constraints(env, "penalized_constraints");
Annotation<bool> penalized_constraints(env, "penalized_constraints");
for (const auto& ctr : single_level.ctrs()) {
ctr.set(penalized_constraints, true);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mixed-integer/assignment-bap.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(int t_argc, const char** t_argv) {
Model model(env);

// Create decomposition annotation
Annotation<Ctr> decomposition(env, "decomposition", MasterId);
Annotation decomposition(env, "decomposition", MasterId);

// Create assignment variables (x_ij binaries)
auto x = model.add_vars(Dim<2>(n_agents, n_jobs), 0., 1., Binary, 0., "x");
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 @@ -31,7 +31,7 @@ int main(int t_argc, const char** t_argv) {
Model model(env);

// Create decomposition annotation
Annotation<Ctr> decomposition(env, "decomposition", MasterId);
Annotation decomposition(env, "decomposition", MasterId);

// Create penalized constraints annotation
Annotation<Ctr, bool> penalized_constraints(env, "penalized_constraints", false);
Expand Down
2 changes: 1 addition & 1 deletion examples/mixed-integer/facility.example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int t_argc, const char** t_argv) {
Env env;

// Read instance
const auto instance = Problems::FLP::read_instance_1991_Cornuejols_et_al("/home/henri/Research/idol/tests/data/facility-location-problem/instance_F10_C20__4.txt");
const auto instance = Problems::FLP::read_instance_1991_Cornuejols_et_al("facility.data.txt");
const unsigned int n_customers = instance.n_customers();
const unsigned int n_facilities = instance.n_facilities();

Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ add_library(idol STATIC
src/bilevel/modeling/read_from_file.cpp
include/idol/mixed-integer/optimizers/wrappers/Mosek/MosekCallbackI.h
src/mixed-integer/optimizers/wrappers/Mosek/MosekCallbackI.cpp
include/idol/bilevel/modeling/LowerLevelDescription.h
include/idol/bilevel/modeling/Description.h
include/idol/bilevel/modeling/write_to_file.h
src/bilevel/modeling/write_to_file.cpp
include/idol/general/utils/LimitedWidthStream.h
Expand Down
65 changes: 65 additions & 0 deletions lib/include/idol/bilevel/modeling/Description.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Created by henri on 20.06.24.
//

#ifndef IDOL_DESCRIPTION_H
#define IDOL_DESCRIPTION_H

#include <utility>

#include "idol/mixed-integer/modeling/constraints/Ctr.h"
#include "idol/mixed-integer/modeling/variables/Var.h"
#include "idol/mixed-integer/modeling/objects/Versions.h"
#include "idol/mixed-integer/modeling/models/Model.h"

namespace idol::Bilevel {
class Description;
}

class idol::Bilevel::Description {
Annotation<unsigned int> m_lower_level;
AffExpr<Var> m_follower_objective;
public:
Description(Env& t_env, const std::string& t_name) : m_lower_level(t_env, t_name + "_lower_level", MasterId) {}

explicit Description(Env& t_env) : Description(t_env, "bilevel") {}

explicit Description(const Annotation<unsigned int>& t_lower_level) : m_lower_level(t_lower_level) {}

Description(const Annotation<unsigned int>& t_lower_level,
AffExpr<Var> t_follower_objective)
: m_lower_level(t_lower_level),
m_follower_objective(std::move(t_follower_objective)) {}

[[nodiscard]] const Annotation<unsigned int>& lower_level() const { return m_lower_level; }

[[nodiscard]] const AffExpr<Var>& follower_obj() const { return m_follower_objective; }

void make_leader(const Var& t_var) { t_var.set(m_lower_level, MasterId); }

void make_leader(const Ctr& t_ctr) { t_ctr.set(m_lower_level, MasterId); }

void make_leader(const QCtr& t_ctr) { t_ctr.set(m_lower_level, MasterId); }

void make_lower_level(const Var& t_var) { t_var.set(m_lower_level, 0); }

void make_follower(const Ctr& t_ctr) { t_ctr.set(m_lower_level, 0); }

void make_lower_level(const QCtr& t_ctr) { t_ctr.set(m_lower_level, 0); }

void set_lower_objective(AffExpr<Var> t_objective) { m_follower_objective = std::move(t_objective); }

[[nodiscard]] bool is_leader(const Var& t_var) const { return t_var.get(m_lower_level) == MasterId; }

[[nodiscard]] bool is_leader(const Ctr& t_ctr) const { return t_ctr.get(m_lower_level) == MasterId; }

[[nodiscard]] bool is_leader(const QCtr& t_ctr) const { return t_ctr.get(m_lower_level) == MasterId; }

[[nodiscard]] bool is_follower(const Var& t_var) const { return t_var.get(m_lower_level) != MasterId; }

[[nodiscard]] bool is_follower(const Ctr& t_ctr) const { return t_ctr.get(m_lower_level) != MasterId; }

[[nodiscard]] bool is_follower(const QCtr& t_ctr) const { return t_ctr.get(m_lower_level) != MasterId; }
};

#endif //IDOL_DESCRIPTION_H
67 changes: 0 additions & 67 deletions lib/include/idol/bilevel/modeling/LowerLevelDescription.h

This file was deleted.

14 changes: 7 additions & 7 deletions lib/include/idol/bilevel/modeling/read_from_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@

#include <string>
#include "idol/mixed-integer/modeling/models/Model.h"
#include "LowerLevelDescription.h"
#include "Description.h"

namespace idol::Bilevel {

namespace impl {
idol::Model
read_from_file(Env &t_env,
const std::string &t_path_to_aux,
idol::Bilevel::LowerLevelDescription& t_lower_level_description,
idol::Bilevel::Description& t_lower_level_description,
const std::function<Model(Env &, const std::string &)> &t_mps_reader);
}

template<class BackendT> Model read_from_file(Env& t_env, const std::string& t_path_to_aux, Bilevel::LowerLevelDescription& t_lower_level_description);
template<class BackendT> Model read_from_file(Env& t_env, const std::string& t_path_to_aux, Bilevel::Description& t_lower_level_description);

template<class BackendT> std::pair<Model, Bilevel::LowerLevelDescription> read_from_file(Env& t_env, const std::string& t_path_to_aux);
template<class BackendT> std::pair<Model, Bilevel::Description> read_from_file(Env& t_env, const std::string& t_path_to_aux);

}

template<class BackendT>
idol::Model
idol::Bilevel::read_from_file(idol::Env& t_env,
const std::string& t_path_to_aux,
idol::Bilevel::LowerLevelDescription& t_lower_level_description) {
idol::Bilevel::Description& t_lower_level_description) {

return idol::Bilevel::impl::read_from_file(t_env, t_path_to_aux, t_lower_level_description, [](Env& t_env, const std::string& t_file) { return BackendT::read_from_file(t_env, t_file); });
}

template<class BackendT>
std::pair<idol::Model, idol::Bilevel::LowerLevelDescription>
std::pair<idol::Model, idol::Bilevel::Description>
idol::Bilevel::read_from_file(idol::Env& t_env,
const std::string& t_path_to_aux) {

Bilevel::LowerLevelDescription lower_level_description(t_env);
Bilevel::Description lower_level_description(t_env);

auto high_point_relaxation = Bilevel::impl::read_from_file(t_env, t_path_to_aux, lower_level_description,[](Env& t_env, const std::string& t_file) { return BackendT::read_from_file(t_env, t_file); });

Expand Down
4 changes: 2 additions & 2 deletions lib/include/idol/bilevel/modeling/write_to_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include <fstream>
#include "idol/mixed-integer/modeling/models/Model.h"
#include "LowerLevelDescription.h"
#include "Description.h"

namespace idol::Bilevel {
void write_to_file(const Model& t_model, const Bilevel::LowerLevelDescription& t_description, const std::string& t_filename);
void write_to_file(const Model& t_model, const Bilevel::Description& t_description, const std::string& t_filename);
}

#endif //IDOL_WRITE_TO_FILE_H
6 changes: 3 additions & 3 deletions lib/include/idol/bilevel/optimizers/wrappers/MibS/MibS.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "idol/general/optimizers/OptimizerFactory.h"
#include "idol/mixed-integer/modeling/annotations/Annotation.h"
#include "idol/mixed-integer/modeling/constraints/Ctr.h"
#include "idol/bilevel/modeling/LowerLevelDescription.h"
#include "idol/bilevel/modeling/Description.h"
#include "idol/mixed-integer/optimizers/callbacks/CallbackFactory.h"

#ifdef IDOL_USE_OSI
Expand All @@ -23,15 +23,15 @@ namespace idol::Bilevel {
class OsiSolverInterface;

class idol::Bilevel::MibS : public OptimizerFactoryWithDefaultParameters<MibS> {
Bilevel::LowerLevelDescription m_description;
Bilevel::Description m_description;
std::optional<bool> m_use_file_interface;
std::optional<bool> m_use_cplex_for_feasibility;
std::list<std::unique_ptr<CallbackFactory>> m_callbacks;
#ifdef IDOL_USE_OSI
std::unique_ptr<OsiSolverInterface> m_osi_interface;
#endif
public:
MibS(Bilevel::LowerLevelDescription t_description);
MibS(Bilevel::Description t_description);

MibS(const MibS& t_src);
MibS(MibS&&) noexcept = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "idol/general/optimizers/Optimizer.h"
#include "idol/mixed-integer/modeling/annotations/Annotation.h"
#include "idol/mixed-integer/modeling/constraints/Ctr.h"
#include "idol/bilevel/modeling/LowerLevelDescription.h"
#include "idol/bilevel/modeling/Description.h"
#include "impl_MibS.h"
#include "idol/mixed-integer/optimizers/callbacks/Callback.h"

Expand All @@ -20,7 +20,7 @@ namespace idol::Optimizers::Bilevel {
}

class idol::Optimizers::Bilevel::MibS : public Optimizer {
const idol::Bilevel::LowerLevelDescription m_description;
const idol::Bilevel::Description m_description;

std::unique_ptr<idol::impl::MibS> m_mibs;
std::unique_ptr<OsiSolverInterface> m_osi_solver;
Expand All @@ -30,7 +30,7 @@ class idol::Optimizers::Bilevel::MibS : public Optimizer {
std::list<std::unique_ptr<Callback>> m_callbacks;
public:
MibS(const idol::Model& t_parent,
idol::Bilevel::LowerLevelDescription t_description,
idol::Bilevel::Description t_description,
OsiSolverInterface* t_osi_solver,
bool t_use_file,
bool t_use_cplex_for_feasibility);
Expand Down
Loading

0 comments on commit 9cc8a84

Please sign in to comment.