Skip to content

Commit

Permalink
add Robust::Description
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Nov 28, 2024
1 parent 51bc6fa commit 72aaca1
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 69 deletions.
24 changes: 17 additions & 7 deletions dev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#include "idol/general/utils/GenerationPattern.h"
#include "idol/mixed-integer/modeling/models/KKT.h"
#include "idol/mixed-integer/problems/facility-location-problem/FLP_Instance.h"
#include "idol/robust/modeling/Description.h"

using namespace idol;

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

Env env;

const double Gamma = 3;

// Generate parameters
const auto filename = "/home/henri/Research/idol/tests/data/facility-location-problem/instance_F10_C20__0.txt";

Expand All @@ -40,6 +43,10 @@ int main(int t_argc, const char** t_argv) {
auto x = Var::make_vector(env, Dim<1>(n_facilities), 0., 1., Continuous, 0., "x");
auto y = Var::make_vector(env, Dim<2>(n_facilities, n_customers), 0., 1., Continuous, 0., "y");

Model uncertainty_set(env);
const auto xi = uncertainty_set.add_vars(Dim<1>(n_facilities), 0., 1., Binary, 0., "xi");
uncertainty_set.add_ctr(idol_Sum(i, Range(n_facilities), xi[i]) <= Gamma);

Model model(env);

model.add_vector<Var, 1>(x);
Expand All @@ -53,19 +60,22 @@ int main(int t_argc, const char** t_argv) {
model.add(Ctr(env, idol_Sum(i, Range(n_facilities), y[i][j]) == 1));
}

Robust::Description description(uncertainty_set);
for (unsigned int i = 0 ; i < n_facilities ; ++i) {
for (unsigned int j = 0 ; j < n_customers ; ++j) {
description.make_stage_var(y[i][j], 1);
auto activation = model.add_ctr(y[i][j] <= 1);
description.set_uncertain_rhs(activation, -xi[i]);
}
}

model.set_obj_expr(idol_Sum(i, Range(n_facilities), instance.fixed_cost(i) * x[i] + idol_Sum(j, Range(n_customers),
instance.per_unit_transportation_cost(
i, j) *
instance.demand(j) *
y[i][j])));

Model reformulated(env);

Reformulators::KKT reformulator(model, model.get_obj_expr());

reformulator.add_strong_duality_reformulation(reformulated);

std::cout << reformulated << std::endl;
std::cout << Robust::Description::View(model, description) << std::endl;

return 0;
}
3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ add_library(idol STATIC
include/idol/bilevel/optimizers/wrappers/MibS/impl_MibSFromFile.h
src/bilevel/optimizers/wrappers/MibS/impl_MibSFromFile.cpp
include/idol/general/utils/SilentMode.h
include/idol/robust/modeling/StageDescription.h
src/mixed-integer/optimizers/padm/PADM.cpp
include/idol/mixed-integer/optimizers/padm/PADM.h
src/mixed-integer/optimizers/padm/Optimizers_PADM.cpp
Expand Down Expand Up @@ -228,6 +227,8 @@ add_library(idol STATIC
include/idol/mixed-integer/modeling/models/KKT.h
src/mixed-integer/modeling/models/KKT.cpp
include/idol/mixed-integer/optimizers/callbacks/ReducedCostFixing.h
include/idol/robust/modeling/Description.h
src/robust/modeling/Description.cpp
)

find_package(OpenMP REQUIRED)
Expand Down
10 changes: 7 additions & 3 deletions lib/include/idol/general/utils/SparseVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class idol::SparseVector {
#endif
#endif
map_t m_map;

static ValueT s_zero_value;
public:
SparseVector() = default;

Expand Down Expand Up @@ -106,7 +108,7 @@ class idol::SparseVector {

[[nodiscard]] bool has_index(const IndexT& t_index) const { return m_map.find(t_index) != m_map.end(); }

[[nodiscard]] ValueT get(const IndexT& t_index1) const;
[[nodiscard]] const ValueT& get(const IndexT& t_index1) const;

void set(const IndexT& t_index, const ValueT& t_value);

Expand Down Expand Up @@ -140,6 +142,8 @@ class idol::SparseVector {
SparseVector& merge_without_conflict(const SparseVector& t_vec);
};

template<class IndexT, class ValueT> ValueT idol::SparseVector<IndexT, ValueT>::s_zero_value {};

template<class IndexT, class ValueT>
idol::SparseVector<IndexT, ValueT>
idol::SparseVector<IndexT, ValueT>::operator-() const {
Expand Down Expand Up @@ -264,12 +268,12 @@ idol::SparseVector<IndexT, ValueT>::merge_without_conflict(const SparseVector &t
}

template<class IndexT, class ValueT>
ValueT idol::SparseVector<IndexT, ValueT>::get(const IndexT &t_index1) const {
const ValueT& idol::SparseVector<IndexT, ValueT>::get(const IndexT &t_index1) const {

const auto it = m_map.find(t_index1);

if (it == m_map.end()) {
return ValueT{};
return s_zero_value;
}

return it->second;
Expand Down
4 changes: 4 additions & 0 deletions lib/include/idol/mixed-integer/modeling/expressions/AffExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ class idol::AffExpr {
constant() = 0;
m_linear.clear();
}

static const AffExpr<KeyT, ValueT> Zero;
};

template<class KeyT, class ValueT> const idol::AffExpr<KeyT, ValueT> idol::AffExpr<KeyT, ValueT>::Zero {};

template<class KeyT, class ValueT>
idol::AffExpr<KeyT, ValueT>::AffExpr(const KeyT &t_key) : m_linear(t_key) {

Expand Down
4 changes: 4 additions & 0 deletions lib/include/idol/mixed-integer/modeling/expressions/LinExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ class idol::LinExpr : public SparseVector<KeyT, ValueT> {

LinExpr& operator-=(const LinExpr<KeyT, ValueT>& t_rhs);
LinExpr& operator-=(const KeyT& t_rhs);

static LinExpr<KeyT, ValueT> Zero;
};

template<class KeyT, class ValueT> idol::LinExpr<KeyT, ValueT> idol::LinExpr<KeyT, ValueT>::Zero {};

template<class KeyT, class ValueT>
idol::LinExpr<KeyT, ValueT> &idol::LinExpr<KeyT, ValueT>::operator-=(const KeyT &t_rhs) {
SparseVector<KeyT, ValueT>::operator-=(SparseVector<KeyT, ValueT>(t_rhs, 1));
Expand Down
71 changes: 71 additions & 0 deletions lib/include/idol/robust/modeling/Description.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Created by henri on 28.11.24.
//

#ifndef IDOL_DESCRIPTION_H
#define IDOL_DESCRIPTION_H

#include <optional>
#include "idol/mixed-integer/modeling/annotations/Annotation.h"
#include "idol/mixed-integer/modeling/constraints/Ctr.h"
#include "idol/mixed-integer/modeling/expressions/AffExpr.h"

namespace idol::Robust {
class Description;
}

class idol::Robust::Description {
mutable std::optional<Annotation<unsigned int>> m_stages;
Map<Ctr, LinExpr<Var, LinExpr<Var>>> m_uncertain_mat_coeff;
Map<Ctr, LinExpr<Var>> m_uncertain_rhs;
const Model& m_uncertainty_set;
public:
explicit Description(const Model& t_uncertainty_set) : m_uncertainty_set(t_uncertainty_set) {}

[[nodiscard]] const Annotation<unsigned int>& stage_annotation() const;

void make_stage_var(const Var& t_var, unsigned int t_stage);

void make_stage_ctr(const Ctr& t_ctr, unsigned int t_stage);

unsigned int stage(const Var& t_var) const;

unsigned int stage(const Ctr& t_var) const;

const Model& uncertainty_set() const { return m_uncertainty_set; }

auto uncertain_coefficients() const { return ConstIteratorForward(m_uncertain_mat_coeff); }

const LinExpr<Var>& uncertain_mat_coeff(const Ctr& t_ctr, const Var& t_var) const;

const LinExpr<Var, LinExpr<Var>>& uncertain_mat_coeffs(const Ctr& t_ctr) const;

const LinExpr<Var>& uncertain_rhs(const Ctr& t_ctr) const;

void set_uncertain_mat_coeff(const Ctr& t_ctr, const Var& t_var, const LinExpr<Var>& t_coeff) {
m_uncertain_mat_coeff[t_ctr].set(t_var, t_coeff);
}

void set_uncertain_rhs(const Ctr& t_ctr, const LinExpr<Var>& t_rhs) {
m_uncertain_rhs[t_ctr] = t_rhs;
}

class View {
const Description& m_description;
const Model& m_deterministic_model;
public:
View(const Model& t_deterministic_model, const Description& t_description);

[[nodiscard]] const Description& description() const { return m_description; }

[[nodiscard]] const Model& deterministic_model() const { return m_deterministic_model; }
};
};

namespace idol {

std::ostream &operator<<(std::ostream &t_os, const idol::Robust::Description::View &t_view);

}

#endif //IDOL_DESCRIPTION_H
58 changes: 0 additions & 58 deletions lib/include/idol/robust/modeling/StageDescription.h

This file was deleted.

Loading

0 comments on commit 72aaca1

Please sign in to comment.