Skip to content

Commit

Permalink
start CCG
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Dec 11, 2024
1 parent fd99d49 commit 0b5fe4c
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 3 deletions.
9 changes: 6 additions & 3 deletions dev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "idol/bilevel/modeling/Description.h"
#include "idol/robust/optimizers/deterministic/Deterministic.h"
#include "idol/robust/optimizers/affine-decision-rule/AffineDecisionRule.h"
#include "idol/robust/optimizers/column-and-constraint-generation/ColumnAndConstraintGeneration.h"

using namespace idol;

Expand Down Expand Up @@ -80,14 +81,16 @@ int main(int t_argc, const char** t_argv) {
)
);

model.dump(std::cout);

model.use(Gurobi());
model.optimize();

std::cout << "Deterministic Problem has value: " << model.get_best_obj() << std::endl;

model.use(Robust::AffineDecisionRule(description).with_deterministic_optimizer(Gurobi()));
auto ccg = Robust::ColumnAndConstraintGeneration(description);

ccg.with_master_optimizer(Gurobi());

model.use(ccg);
model.optimize();

std::cout << "ADR Problem has value: " << model.get_best_obj() << std::endl;
Expand Down
6 changes: 6 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ add_library(idol STATIC
src/bilevel/optimizers/StrongDuality/StrongDuality.cpp
src/bilevel/optimizers/KKT/Optimizers_KKT.cpp
src/bilevel/optimizers/StrongDuality/Optimizers_StrongDuality.cpp
include/idol/robust/optimizers/column-and-constraint-generation/ColumnAndConstraintGeneration.h
src/robust/optimizers/column-and-constraint-generation/ColumnAndConstraintGeneration.cpp
include/idol/robust/optimizers/column-and-constraint-generation/Optimizers_ColumnAndConstraintGeneration.h
src/robust/optimizers/column-and-constraint-generation/Optimizers_ColumnAndConstraintGeneration.cpp
include/idol/robust/optimizers/column-and-constraint-generation/Formulation.h
src/robust/optimizers/column-and-constraint-generation/Formulation.cpp
)

find_package(OpenMP REQUIRED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by henri on 11.12.24.
//

#ifndef IDOL_COLUMNANDCONSTRAINTGENERATION_H
#define IDOL_COLUMNANDCONSTRAINTGENERATION_H

#include "idol/general/optimizers/OptimizerFactory.h"
#include "idol/robust/modeling/Description.h"

namespace idol::Robust {
class ColumnAndConstraintGeneration;
}

class idol::Robust::ColumnAndConstraintGeneration : public OptimizerFactoryWithDefaultParameters<ColumnAndConstraintGeneration> {
const Robust::Description& m_description;
std::unique_ptr<OptimizerFactory> m_master_optimizer;
public:
explicit ColumnAndConstraintGeneration(const Robust::Description& t_description);

ColumnAndConstraintGeneration(const ColumnAndConstraintGeneration& t_src);

Optimizer *operator()(const Model &t_model) const override;

[[nodiscard]] OptimizerFactory *clone() const override;

ColumnAndConstraintGeneration& with_master_optimizer(const idol::OptimizerFactory &t_deterministic_optimizer);
};

#endif //IDOL_COLUMNANDCONSTRAINTGENERATION_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by henri on 11.12.24.
//

#ifndef IDOL_CCG_FORMULATION_H
#define IDOL_CCG_FORMULATION_H

#include "idol/mixed-integer/modeling/models/Model.h"
#include "idol/robust/modeling/Description.h"

namespace idol::CCG {
class Formulation;
}

class idol::CCG::Formulation {
const Model& m_parent;
const ::idol::Robust::Description &m_description;
public:
Formulation(const Model& t_parent, const ::idol::Robust::Description &t_description);
};

#endif //IDOL_CCG_FORMULATION_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Created by henri on 11.12.24.
//

#ifndef IDOL_OPTIMIZERS_COLUMNANDCONSTRAINTGENERATION_H
#define IDOL_OPTIMIZERS_COLUMNANDCONSTRAINTGENERATION_H

#include "idol/robust/modeling/Description.h"
#include "idol/general/optimizers/Algorithm.h"
#include "idol/general/optimizers/OptimizerFactory.h"
#include "Formulation.h"

namespace idol::Optimizers::Robust {
class ColumnAndConstraintGeneration;
}

class idol::Optimizers::Robust::ColumnAndConstraintGeneration : public Algorithm {
const ::idol::Robust::Description &m_description;
std::unique_ptr<OptimizerFactory> m_master_optimizer;
std::unique_ptr<idol::CCG::Formulation> m_formulation;
public:
ColumnAndConstraintGeneration(const Model& t_parent,
const ::idol::Robust::Description &t_description,
const OptimizerFactory &t_master_optimizer);

[[nodiscard]] std::string name() const override;

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

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

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

[[nodiscard]] double get_ctr_dual(const Ctr &t_ctr) const override;

[[nodiscard]] double get_ctr_farkas(const Ctr &t_ctr) const override;

[[nodiscard]] unsigned int get_n_solutions() const override;

[[nodiscard]] unsigned int get_solution_index() const override;

protected:
void add(const Var &t_var) override;

void add(const Ctr &t_ctr) override;

void add(const QCtr &t_ctr) override;

void remove(const Var &t_var) override;

void remove(const Ctr &t_ctr) override;

void remove(const QCtr &t_ctr) override;

void update() override;

void write(const std::string &t_name) override;

void hook_before_optimize() override;

void hook_optimize() override;

void set_solution_index(unsigned int t_index) override;

void update_obj_sense() override;

void update_obj() override;

void update_rhs() override;

void update_obj_constant() override;

void update_mat_coeff(const Ctr &t_ctr, const Var &t_var) override;

void update_ctr_type(const Ctr &t_ctr) override;

void update_ctr_rhs(const Ctr &t_ctr) override;

void update_var_type(const Var &t_var) override;

void update_var_lb(const Var &t_var) override;

void update_var_ub(const Var &t_var) override;

void update_var_obj(const Var &t_var) override;
};


#endif //IDOL_OPTIMIZERS_COLUMNANDCONSTRAINTGENERATION_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Created by henri on 11.12.24.
//
#include "idol/robust/optimizers/column-and-constraint-generation/ColumnAndConstraintGeneration.h"
#include "idol/robust/optimizers/column-and-constraint-generation/Optimizers_ColumnAndConstraintGeneration.h"

idol::Robust::ColumnAndConstraintGeneration::ColumnAndConstraintGeneration(const Robust::Description &t_description)
: m_description(t_description) {

}

idol::Robust::ColumnAndConstraintGeneration::ColumnAndConstraintGeneration(
const idol::Robust::ColumnAndConstraintGeneration &t_src) : m_description(t_src.m_description) {

}

idol::OptimizerFactory *idol::Robust::ColumnAndConstraintGeneration::clone() const {
return new ColumnAndConstraintGeneration(*this);
}

idol::Robust::ColumnAndConstraintGeneration &idol::Robust::ColumnAndConstraintGeneration::with_master_optimizer(
const idol::OptimizerFactory &t_deterministic_optimizer) {

if (m_master_optimizer) {
throw Exception("Master optimizer already set");
}

m_master_optimizer.reset(t_deterministic_optimizer.clone());

return *this;
}

idol::Optimizer *idol::Robust::ColumnAndConstraintGeneration::operator()(const idol::Model &t_model) const {

if (!m_master_optimizer) {
throw Exception("Master optimizer not set");
}

auto* result = new Optimizers::Robust::ColumnAndConstraintGeneration(t_model,
m_description,
*m_master_optimizer);

handle_default_parameters(result);

return result;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by henri on 11.12.24.
//
#include <idol/robust/optimizers/column-and-constraint-generation/Formulation.h>


idol::CCG::Formulation::Formulation(const idol::Model &t_parent, const ::idol::Robust::Description &t_description)
: m_parent(t_parent), m_description(t_description) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// Created by henri on 11.12.24.
//
#include <idol/robust/optimizers/column-and-constraint-generation/Optimizers_ColumnAndConstraintGeneration.h>

idol::Optimizers::Robust::ColumnAndConstraintGeneration::ColumnAndConstraintGeneration(const idol::Model &t_parent,
const idol::Robust::Description &t_description,
const idol::OptimizerFactory &t_master_optimizer)
: Algorithm(t_parent),
m_description(t_description),
m_master_optimizer(t_master_optimizer.clone()) {

}

std::string idol::Optimizers::Robust::ColumnAndConstraintGeneration::name() const {
return "column-and-constraint generation";
}

double idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_var_primal(const idol::Var &t_var) const {
throw Exception("Not implemented get_var_primal");
}

double idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_var_reduced_cost(const idol::Var &t_var) const {
throw Exception("Not implemented get_var_reduced_cost");
}

double idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_var_ray(const idol::Var &t_var) const {
throw Exception("Not implemented get_var_ray");
}

double idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_ctr_dual(const idol::Ctr &t_ctr) const {
throw Exception("Not implemented get_ctr_dual");
}

double idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_ctr_farkas(const idol::Ctr &t_ctr) const {
throw Exception("Not implemented get_ctr_farkas");
}

unsigned int idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_n_solutions() const {
throw Exception("Not implemented get_n_solutions");
}

unsigned int idol::Optimizers::Robust::ColumnAndConstraintGeneration::get_solution_index() const {
throw Exception("Not implemented get_solution_index");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::add(const idol::Var &t_var) {
throw Exception("Not implemented add");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::add(const idol::Ctr &t_ctr) {
throw Exception("Not implemented add");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::add(const idol::QCtr &t_ctr) {
throw Exception("Not implemented add");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::remove(const idol::Var &t_var) {
throw Exception("Not implemented remove");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::remove(const idol::Ctr &t_ctr) {
throw Exception("Not implemented remove");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::remove(const idol::QCtr &t_ctr) {
throw Exception("Not implemented remove");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update() {
throw Exception("Not implemented update");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::write(const std::string &t_name) {
throw Exception("Not implemented write");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::hook_before_optimize() {
Optimizer::hook_before_optimize();
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::hook_optimize() {
throw Exception("Not implemented hook_optimize");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::set_solution_index(unsigned int t_index) {
throw Exception("Not implemented set_solution_index");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_obj_sense() {
throw Exception("Not implemented update_obj_sense");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_obj() {
throw Exception("Not implemented update_obj");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_rhs() {
throw Exception("Not implemented update_rhs");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_obj_constant() {
throw Exception("Not implemented update_obj_constant");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_mat_coeff(const idol::Ctr &t_ctr,
const idol::Var &t_var) {
throw Exception("Not implemented update_mat_coeff");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_ctr_type(const idol::Ctr &t_ctr) {
throw Exception("Not implemented update_ctr_type");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_ctr_rhs(const idol::Ctr &t_ctr) {
throw Exception("Not implemented update_ctr_rhs");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_var_type(const idol::Var &t_var) {
throw Exception("Not implemented update_var_type");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_var_lb(const idol::Var &t_var) {
throw Exception("Not implemented update_var_lb");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_var_ub(const idol::Var &t_var) {
throw Exception("Not implemented update_var_ub");
}

void idol::Optimizers::Robust::ColumnAndConstraintGeneration::update_var_obj(const idol::Var &t_var) {
throw Exception("Not implemented update_var_obj");
}

0 comments on commit 0b5fe4c

Please sign in to comment.