Skip to content

Commit

Permalink
fix red. cost fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Nov 25, 2024
1 parent 7942057 commit 9032465
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,7 @@ void idol::Optimizers::BranchAndBound<NodeInfoT>::solve(TreeNode& t_node,
relaxation.optimizer().set_param_best_bound_stop(std::min(get_best_obj(), get_param_best_bound_stop()));
relaxation.optimizer().set_param_time_limit(get_remaining_time());

std::cout << "Preparing node " << t_node.id() << std::endl;
node_updator.prepare(t_node);
std::cout << "END" << std::endl;

for (const auto& var : parent().vars()) {
const double lb = relaxation.get_var_lb(var);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ void idol::BranchingRules::PseudoCost<NodeInfoT>::on_node_solved(const idol::Nod

const double parent_var_primal_value = parent_solution.get(var);

const double objective_gain_per_unit_change =
(node_objective_value - parent_objective_value) / std::abs(bound - parent_var_primal_value);
double objective_gain_per_unit_change = 0;
if (!equals(bound, parent_var_primal_value, 1e-5)) {
objective_gain_per_unit_change =
(node_objective_value - parent_objective_value) / std::abs(bound - parent_var_primal_value);
}

PseudoCostData data;

Expand Down Expand Up @@ -204,6 +207,10 @@ double idol::BranchingRules::PseudoCost<NodeInfoT>::compute_average_lower_boundi
n += 1;
}

if (n == 0) {
return 0.;
}

return sum / (double) n;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class idol::BranchAndBoundCallbackI : public AbstractBranchAndBoundCallbackI<Nod

void submit_bound(double t_bound);

double best_bound() const;
[[nodiscard]] double best_bound() const;

double best_obj() const;
[[nodiscard]] double best_obj() const;

void terminate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class idol::DefaultNodeUpdator : public NodeUpdator<NodeInfoT> {
Map<Var, double> m_changed_upper_bounds;
std::list<Ctr> m_added_constraints;


void apply_variable_branching_decisions(const Node<NodeInfoT> &t_node,
Map<Var, double> &t_changed_lower_bounds,
Map<Var, double> &t_changed_upper_bounds);
Expand All @@ -51,7 +50,7 @@ class idol::DefaultNodeUpdator : public NodeUpdator<NodeInfoT> {
protected:
Model& relaxation() { return m_relaxation; }

const Model& relaxation() const { return m_relaxation; }
[[nodiscard]] const Model& relaxation() const { return m_relaxation; }
};

template<class NodeInfoT>
Expand Down Expand Up @@ -121,8 +120,6 @@ void idol::DefaultNodeUpdator<NodeInfoT>::apply_variable_branching_decisions(con

for (const auto& branching_decision : t_node.info().variable_branching_decisions()) {

std::cout << branching_decision.variable << " " << branching_decision.type << " " << branching_decision.bound << std::endl;

switch (branching_decision.type) {
case LessOrEqual:
apply_variable_branching_decision(branching_decision, m_changed_upper_bounds, t_changed_upper_bounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ class idol::ReducedCostFixing : public BranchAndBoundCallbackFactory<NodeInfoT>
double reduced_cost = relaxation.get_var_reduced_cost(var);

if (current_obj + reduced_cost > best_obj + Tolerance::MIPAbsoluteGap) {
const double relaxation_lb = original_model.get_var_lb(var);
this->add_local_variable_branching(var, LessOrEqual, relaxation_lb);
const double relaxation_lb = relaxation.get_var_lb(var);
const double current_ub = relaxation.get_var_ub(var);
if (!equals(relaxation_lb, current_ub, Tolerance::Integer)) {
this->add_local_variable_branching(var, LessOrEqual, relaxation_lb);
}
}

if (current_obj - reduced_cost > best_obj + Tolerance::MIPAbsoluteGap) {
const double relaxation_ub = original_model.get_var_ub(var);
this->add_local_variable_branching(var, GreaterOrEqual, relaxation_ub);
const double relaxation_ub = relaxation.get_var_ub(var);
const double current_lb = relaxation.get_var_lb(var);
if (!equals(relaxation_ub, current_lb, Tolerance::Integer)) {
this->add_local_variable_branching(var, GreaterOrEqual, relaxation_ub);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void idol::Optimizers::HiGHS::update_objective_constant() {
}

double idol::Optimizers::HiGHS::get_var_reduced_cost(const idol::Var &t_var) const {
throw Exception("Not implemented get_var_reduced_cost");
return m_model.getSolution().col_dual[lazy(t_var).impl()];
}

int idol::Optimizers::HiGHS::hook_add(const idol::QCtr &t_ctr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ void idol::Optimizers::Mosek::set_param_logs(bool t_value) {
}

double idol::Optimizers::Mosek::get_var_reduced_cost(const idol::Var &t_var) const {
throw Exception("Not implemented get_var_reduced_cost");
return lazy(t_var).impl().variable->dual()->operator[](0);
}

void idol::Optimizers::Mosek::add_callback(idol::Callback *t_ptr_to_callback) {
Expand Down
2 changes: 2 additions & 0 deletions tests/mixed-integer/branch-and-bound/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "idol/mixed-integer/optimizers/wrappers/Gurobi/Gurobi.h"
#include "idol/mixed-integer/optimizers/wrappers/HiGHS/HiGHS.h"
#include "idol/mixed-integer/optimizers/wrappers/Osi/Osi.h"
#include "idol/mixed-integer/optimizers/callbacks/ReducedCostFixing.h"

using namespace Catch::literals;
using namespace idol;
Expand Down Expand Up @@ -97,6 +98,7 @@ TEMPLATE_LIST_TEST_CASE("Solve Facility Location Problem instances with differen
.with_node_optimizer(OPTIMIZER::ContinuousRelaxation())
.with_branching_rule(BranchingRuleT())
.with_node_selection_rule(NodeSelectionRuleT())
.add_callback(ReducedCostFixing())
.with_subtree_depth(subtree_depth)
);

Expand Down

0 comments on commit 9032465

Please sign in to comment.