Skip to content

Commit

Permalink
allow to use MibS through files
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 21, 2024
1 parent 96d7179 commit 82f2edd
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 105 deletions.
5 changes: 4 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ add_library(idol STATIC
src/optimizers/bilevel-optimization/wrappers/MibS/Optimizers_MibS.cpp
include/idol/optimizers/bilevel-optimization/wrappers/MibS/Optimizers_MibS.h
src/optimizers/bilevel-optimization/wrappers/MibS/impl_MibS.cpp
include/idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibS.h
include/idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibSFromAPI.h
src/optimizers/mixed-integer-optimization/wrappers/Osi/OsiIdolSolverInterface.cpp
include/idol/optimizers/mixed-integer-optimization/wrappers/Osi/OsiIdolSolverInterface.h
src/optimizers/mixed-integer-optimization/wrappers/Osi/Osi.cpp
Expand Down Expand Up @@ -258,6 +258,9 @@ add_library(idol STATIC
include/idol/optimizers/mixed-integer-optimization/padm/PenaltyUpdates.h
src/optimizers/mixed-integer-optimization/padm/PenaltyMethod.cpp
include/idol/optimizers/mixed-integer-optimization/padm/PenaltyMethod.h
include/idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibS.h
include/idol/containers/uuid.h
src/containers/uuid.cpp
)

find_package(OpenMP REQUIRED)
Expand Down
16 changes: 16 additions & 0 deletions lib/include/idol/containers/uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by henri on 21.10.24.
//

#ifndef IDOL_UUID_H
#define IDOL_UUID_H

#include <string>
#include <random>
#include <sstream>

namespace idol {
std::string generate_uuid_v4();
}

#endif //IDOL_UUID_H
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class idol::Bilevel::MibS : public OptimizerFactoryWithDefaultParameters<MibS> {
Bilevel::LowerLevelDescription m_description;
#ifdef IDOL_USE_OSI
std::unique_ptr<OsiSolverInterface> m_osi_interface;
std::optional<bool> m_use_file_interface;
#endif
public:
MibS(Bilevel::LowerLevelDescription t_description);
Expand All @@ -39,6 +40,8 @@ class idol::Bilevel::MibS : public OptimizerFactoryWithDefaultParameters<MibS> {

MibS& with_osi_interface(const OsiSolverInterface& t_osi_optimizer);

MibS& with_file_interface(bool t_value);

MibS *clone() const override;
};

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

#ifdef IDOL_USE_MIBS

#include <OsiSolverInterface.hpp>
#include "idol/optimizers/Optimizer.h"
#include "idol/modeling/annotations/Annotation.h"
#include "idol/modeling/constraints/Ctr.h"
#include "impl_MibSFromFile.h"
#include "idol/modeling/bilevel-optimization/LowerLevelDescription.h"
#include "impl_MibS.h"

Expand All @@ -23,10 +23,12 @@ class idol::Optimizers::Bilevel::MibS : public Optimizer {

std::unique_ptr<idol::impl::MibS> m_mibs;
std::unique_ptr<OsiSolverInterface> m_osi_solver;
const bool m_use_file;
public:
MibS(const idol::Model& t_parent,
idol::Bilevel::LowerLevelDescription t_description,
OsiSolverInterface* t_osi_solver);
OsiSolverInterface* t_osi_solver,
bool t_use_file);

std::string name() const override { return "mibs"; }
void throw_if_no_mibs() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,29 @@
//
// Created by henri on 01.02.24.
// Created by henri on 21.10.24.
//

#ifdef IDOL_USE_MIBS

#ifndef IDOL_IMPL_MIBS_H
#define IDOL_IMPL_MIBS_H

#include "idol/modeling/models/Model.h"

#include "MibSModel.hpp"
#include "idol/modeling/bilevel-optimization/LowerLevelDescription.h"

namespace idol::impl {
class MibS;
}

class idol::impl::MibS {
const idol::Model& m_model;
const idol::Bilevel::LowerLevelDescription& m_description;
const bool m_logs;

MibSModel m_mibs;
std::unique_ptr<AlpsKnowledgeBroker> m_broker;
std::unique_ptr<OsiSolverInterface> m_osi_solver;

static char to_mibs_type(VarType t_type);

void load_auxiliary_data();
std::pair<std::vector<int>, std::vector<int>> dispatch_variable_indices();
std::pair<std::vector<int>, std::vector<int>> dispatch_constraint_indices();
std::vector<double> find_lower_level_objective_coefficients(const std::vector<int>& t_lower_level_variables_indices);
std::pair<std::vector<double>, std::vector<double>> find_lower_level_bounds(const std::vector<int>& t_lower_level_variables_indices);

void load_problem_data();
std::tuple<std::vector<double>, std::vector<double>, std::vector<char>> parse_variables();
std::tuple<std::vector<double>, std::vector<double>, std::vector<char>> parse_constraints();
CoinPackedMatrix parse_matrix();
std::vector<double> parse_objective();
public:
MibS(const idol::Model& t_model,
const idol::Bilevel::LowerLevelDescription& t_description,
OsiSolverInterface* t_osi_solver,
bool t_logs);
virtual ~MibS() = default;

void solve();
virtual SolutionStatus get_status() const = 0;

double get_best_obj() const;
virtual SolutionReason get_reason() const = 0;

double get_best_bound() const;
virtual double get_best_obj() const = 0;

double get_var_primal(const Var& t_var) const;
virtual double get_best_bound() const = 0;

idol::SolutionStatus get_status() const;

idol::SolutionReason get_reason() const;
virtual double get_var_primal(const Var& t_var) const = 0;

virtual void solve() = 0;
};

#endif

#endif //IDOL_IMPL_MIBS_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by henri on 01.02.24.
//

#ifdef IDOL_USE_MIBS

#ifndef IDOL_IMPL_MIBS_FROM_API_H
#define IDOL_IMPL_MIBS_FROM_API_H

#include "idol/modeling/models/Model.h"

#include "MibSModel.hpp"
#include "idol/modeling/bilevel-optimization/LowerLevelDescription.h"
#include "impl_MibS.h"

namespace idol::impl {
class MibSFromAPI;
}

class idol::impl::MibSFromAPI : public idol::impl::MibS {
const idol::Model& m_model;
const idol::Bilevel::LowerLevelDescription& m_description;
const bool m_logs;

MibSModel m_mibs;
std::unique_ptr<AlpsKnowledgeBroker> m_broker;
std::unique_ptr<OsiSolverInterface> m_osi_solver;

static char to_mibs_type(VarType t_type);

void load_auxiliary_data();
std::pair<std::vector<int>, std::vector<int>> dispatch_variable_indices();
std::pair<std::vector<int>, std::vector<int>> dispatch_constraint_indices();
std::vector<double> find_lower_level_objective_coefficients(const std::vector<int>& t_lower_level_variables_indices);
std::pair<std::vector<double>, std::vector<double>> find_lower_level_bounds(const std::vector<int>& t_lower_level_variables_indices);

void load_problem_data();
std::tuple<std::vector<double>, std::vector<double>, std::vector<char>> parse_variables();
std::tuple<std::vector<double>, std::vector<double>, std::vector<char>> parse_constraints();
CoinPackedMatrix parse_matrix();
std::vector<double> parse_objective();
public:
MibSFromAPI(const idol::Model& t_model,
const idol::Bilevel::LowerLevelDescription& t_description,
OsiSolverInterface* t_osi_solver,
bool t_logs);

void solve() override;

double get_best_obj() const override;

double get_best_bound() const override;

double get_var_primal(const Var& t_var) const override;

idol::SolutionStatus get_status() const override;

idol::SolutionReason get_reason() const override;

};

#endif

#endif //IDOL_IMPL_MIBS_FROM_API_H
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

#include "MibSModel.hpp"
#include "idol/modeling/bilevel-optimization/LowerLevelDescription.h"
#include "impl_MibS.h"

namespace idol::impl {
class MibSFromFile;
}

class idol::impl::MibSFromFile {
class idol::impl::MibSFromFile : public idol::impl::MibS {
const idol::Model& m_model;
const idol::Bilevel::LowerLevelDescription& m_description;
const bool m_logs;
Expand All @@ -30,19 +31,20 @@ class idol::impl::MibSFromFile {
public:
MibSFromFile(const idol::Model& t_model,
const idol::Bilevel::LowerLevelDescription& t_description,
OsiSolverInterface* t_osi_solver,
bool t_logs);

void solve();
void solve() override;

[[nodiscard]] double get_best_obj() const;
[[nodiscard]] double get_best_obj() const override;

[[nodiscard]] double get_best_bound() const;
[[nodiscard]] double get_best_bound() const override;

[[nodiscard]] double get_var_primal(const Var& t_var) const;
[[nodiscard]] double get_var_primal(const Var& t_var) const override;

[[nodiscard]] idol::SolutionStatus get_status() const;
[[nodiscard]] idol::SolutionStatus get_status() const override;

[[nodiscard]] idol::SolutionReason get_reason() const;
[[nodiscard]] idol::SolutionReason get_reason() const override;

};

Expand Down
24 changes: 24 additions & 0 deletions lib/src/containers/uuid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by henri on 21.10.24.
//
#include "idol/containers/uuid.h"

std::string idol::generate_uuid_v4() {
std::random_device rd;
std::uniform_int_distribution<int> dist(0, 15);
std::uniform_int_distribution<int> dist2(8, 11);

std::stringstream ss;
ss << std::hex;
for (int i = 0; i < 8; i++) ss << dist(rd);
ss << "-";
for (int i = 0; i < 4; i++) ss << dist(rd);
ss << "-4"; // UUID version 4
for (int i = 0; i < 3; i++) ss << dist(rd);
ss << "-";
ss << dist2(rd); // UUID variant 1
for (int i = 0; i < 3; i++) ss << dist(rd);
ss << "-";
for (int i = 0; i < 12; i++) ss << dist(rd);
return ss.str();
}
24 changes: 20 additions & 4 deletions lib/src/optimizers/bilevel-optimization/wrappers/MibS/MibS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <OsiCpxSolverInterface.hpp>
#endif

#ifdef IDOL_USE_CLP
#include <OsiClpSolverInterface.hpp>
#endif

idol::Bilevel::MibS::MibS(Bilevel::LowerLevelDescription t_description)
: m_description(std::move(t_description)) {

Expand All @@ -23,17 +27,18 @@ idol::Optimizer *idol::Bilevel::MibS::operator()(const idol::Model &t_model) con
if (m_osi_interface) {
osi_interface = m_osi_interface->clone();
} else {
if constexpr (IDOL_USE_CPLEX) {
#ifdef IDOL_USE_CPLEX
osi_interface = new OsiCpxSolverInterface();
} else {
#else
osi_interface = new OsiClpSolverInterface();
}
#endif
}

auto* result = new Optimizers::Bilevel::MibS(
t_model,
m_description,
osi_interface
osi_interface,
m_use_file_interface.value_or(false)
);

this->handle_default_parameters(result);
Expand Down Expand Up @@ -71,3 +76,14 @@ idol::Bilevel::MibS &idol::Bilevel::MibS::with_osi_interface(const OsiSolverInte
throw Exception("idol was not linked with Osi.");
#endif
}

idol::Bilevel::MibS &idol::Bilevel::MibS::with_file_interface(bool t_value) {

if (m_use_file_interface) {
throw Exception("The file interface has already been set.");
}

m_use_file_interface = t_value;

return *this;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
#ifdef IDOL_USE_MIBS

#include "idol/optimizers/bilevel-optimization/wrappers/MibS/Optimizers_MibS.h"
#include "idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibS.h"
#include "idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibSFromAPI.h"
#include "idol/optimizers/bilevel-optimization/wrappers/MibS/impl_MibSFromFile.h"

#include <utility>

idol::Optimizers::Bilevel::MibS::MibS(const idol::Model &t_parent,
idol::Bilevel::LowerLevelDescription t_description,
OsiSolverInterface* t_osi_solver)
OsiSolverInterface* t_osi_solver,
bool t_use_file)
: Optimizer(t_parent),
m_description(std::move(t_description)),
m_osi_solver(t_osi_solver) {
m_osi_solver(t_osi_solver),
m_use_file(t_use_file) {

}

Expand Down Expand Up @@ -105,10 +108,17 @@ void idol::Optimizers::Bilevel::MibS::hook_optimize() {
return;
}

m_mibs = std::make_unique<impl::MibS>(parent(),
m_description,
m_osi_solver->clone(),
get_param_logs());
if (m_use_file) {
m_mibs = std::make_unique<impl::MibSFromFile>(parent(),
m_description,
m_osi_solver->clone(),
get_param_logs());
} else {
m_mibs = std::make_unique<impl::MibSFromAPI>(parent(),
m_description,
m_osi_solver->clone(),
get_param_logs());
}

m_mibs->solve();
}
Expand Down
Loading

0 comments on commit 82f2edd

Please sign in to comment.