Skip to content

Commit

Permalink
make CutSeparation handle any type of constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 11, 2023
1 parent 13f6726 commit 1349087
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_library(idol STATIC include/modeling/numericals.h include/containers/Optional.h include/containers/Map.h include/modeling.h include/modeling/parameters/Param.h include/modeling/Types.h include/modeling/expressions/Constant.h src/modeling/expressions/Constant.cpp include/modeling/matrix/AbstractMatrixCoefficient.h include/modeling/matrix/MatrixCoefficientReference.h include/modeling/matrix/MatrixCoefficient.h src/modeling/matrix/MatrixCoefficient.cpp src/modeling/matrix/MatrixCoefficientReference.cpp include/modeling/constraints/TempCtr.h src/modeling/constraints/TempCtr.cpp include/containers/IteratorForward.h include/modeling/solutions/AbstractSolution.h include/modeling/solutions/Solution.h include/modeling/variables/TempVar.h include/containers/Set.h include/errors/NotImplemented.h include/modeling/expressions/LinExpr.h include/modeling/matrix/Row.h src/modeling/matrix/Row.cpp src/modeling/matrix/Column.cpp include/modeling/matrix/Column.h include/modeling/matrix/Matrix.h src/modeling/matrix/Matrix.cpp src/problems/generalized-assignment-problem/GAP_Instance.cpp include/problems/knapsack-problem/KP_Instance.h src/problems/knapsack-problem/KP_Instance.cpp include/problems/multiple-knapsack-problem/MKP_Instance.h src/problems/multiple-knapsack-problem/MKP_Instance.cpp include/modeling/expressions/Expr.h include/modeling/expressions/operations/operators_Var.h src/modeling/expressions/operations/operators_Var.cpp src/modeling/expressions/operations/operators_Ctr.cpp src/modeling/expressions/operations/operators_Constant.cpp include/modeling/expressions/operations/operators_Constant.h include/modeling/expressions/operations/operators_Ctr.h include/modeling/expressions/operations/operators.h include/problems/multiple-knapsack-problem/MKP_Instance.h src/problems/multiple-knapsack-problem/MKP_Instance.cpp src/problems/facility-location-problem/FLP_Instance.cpp include/problems/facility-location-problem/FLP_Instance.h include/problems/helpers/distances.h include/problems/helpers/parse_delimited.h include/modeling/expressions/operations/operators_utils.h include/modeling/expressions/QuadExpr.h include/modeling/expressions/AbstractExpr.h include/modeling/models/Model.h include/modeling/objects/Env.h include/modeling/variables/Var.h src/modeling/objects/Env.cpp include/modeling/variables/VarVersion.h include/modeling/objects/Versions.h include/modeling/objects/Object.h include/modeling/objects/ObjectId.h src/modeling/models/Model.cpp include/modeling/constraints/Ctr.h include/modeling/constraints/CtrVersion.h include/modeling/objects/Version.h src/modeling/variables/Var.cpp src/modeling/constraints/Ctr.cpp include/optimizers/Optimizer.h include/optimizers/solvers/gurobi/Optimizers_Gurobi.h include/optimizers/solvers/OptimizerWithLazyUpdates.h src/optimizers/solvers/gurobi/Optimizers_Gurobi.cpp src/optimizers/Optimizer.cpp src/optimizers/Logger.cpp include/optimizers/Algorithm.h src/optimizers/Algorithm.cpp src/optimizers/Timer.cpp include/modeling/annotations/Annotation.h src/modeling/annotations/impl_Annotation.cpp include/modeling/annotations/impl_Annotation.h include/modeling/models/Model.h include/containers/GeneratorPool.h include/modeling/solutions/types.h include/optimizers/solvers/Optimizers_GLPK.h include/solvers.h src/optimizers/solvers/Optimizers_GLPK.cpp include/optimizers/solvers/Optimizers_Mosek.h src/optimizers/solvers/Optimizers_Mosek.cpp include/linear-algebra/MatrixIndices.h include/linear-algebra/SquareMatrix.h include/linear-algebra/to_rotated_quadratic_cone.h include/optimizers/branch-and-bound/Optimizers_BranchAndBound.h include/optimizers/OptimizerFactory.h include/optimizers/solvers/DefaultOptimizer.h include/optimizers/branch-and-bound/BranchAndBound.h include/optimizers/branch-and-bound/branching-rules/factories/BranchingRuleFactory.h include/optimizers/branch-and-bound/node-selection-rules/factories/NodeSelectionRuleFactory.h include/optimizers/branch-and-bound/branching-rules/factories/MostInfeasible.h include/optimizers/branch-and-bound/node-selection-rules/factories/DepthFirst.h include/optimizers/branch-and-bound/branching-rules/impls/BranchingRule.h include/optimizers/branch-and-bound/node-selection-rules/impls/NodeSelectionRule.h include/optimizers/branch-and-bound/nodes/Node.h include/optimizers/branch-and-bound/nodes/NodeUpdator.h include/optimizers/branch-and-bound/nodes/NodeUpdatorByBound.h src/optimizers/branch-and-bound/nodes/NodeInfo.cpp include/optimizers/branch-and-bound/nodes/NodeInfo.h include/optimizers/branch-and-bound/branching-rules/impls/MostInfeasbile.h include/optimizers/branch-and-bound/node-selection-rules/impls/DepthFirst.h include/optimizers/branch-and-bound/branching-rules/impls/VariableBranchingRule.h include/optimizers/branch-and-bound/node-selection-rules/factories/BreadthFirst.h include/optimizers/branch-and-bound/node-selection-rules/factories/WorstBound.h include/optimizers/branch-and-bound/node-selection-rules/factories/BestBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/WorstBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/BestBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/BreadthFirst.h include/optimizers/solvers/gurobi/Gurobi.h include/optimizers/solvers/GLPK.h include/optimizers/solvers/Mosek.h include/optimizers/column-generation/ColumnGeneration.h include/optimizers/column-generation/Optimizers_ColumnGeneration.h src/optimizers/column-generation/ColumnGeneration.cpp src/optimizers/column-generation/Optimizers_ColumnGeneration.cpp include/optimizers/dantzig-wolfe/DantzigWolfeDecomposition.h src/optimizers/dantzig-wolfe/DantzigWolfeDecomposition.cpp include/optimizers/dantzig-wolfe/Optimizers_DantzigWolfeDecomposition.h src/optimizers/dantzig-wolfe/Optimizers_DantzigWolfeDecomposition.cpp src/optimizers/solvers/gurobi/Gurobi.cpp src/optimizers/solvers/GLPK.cpp src/optimizers/solvers/Mosek.cpp include/optimizers/column-generation/OptimizerFactoryWithColumnGenerationParameters.h include/optimizers/branch-and-bound/callbacks/BranchAndBoundCallbackFactory.h include/optimizers/column-generation/IntegerMasterHeuristic.h src/optimizers/column-generation/IntegerMasterHeuristic.cpp src/optimizers/solvers/gurobi/GurobiCallbackI.cpp include/optimizers/callbacks/Callback.h src/optimizers/branch-and-bound/callbacks/Callback.cpp include/optimizers/callbacks/CutSeparation.h src/optimizers/branch-and-bound/callbacks/CutSeparation.cpp include/optimizers/callbacks/LazyCutCallback.h include/optimizers/callbacks/UserCutCallback.h include/optimizers/callbacks/CallbackFactory.h include/optimizers/branch-and-bound/callbacks/BranchAndBoundCallback.h include/optimizers/branch-and-bound/callbacks/AbstractBranchAndBoundCallbackI.h include/optimizers/branch-and-bound/callbacks/CallbackAsBranchAndBoundCallback.h include/optimizers/branch-and-bound/cutting-planes/CuttingPlaneGenerator.h include/optimizers/branch-and-bound/cutting-planes/CoverCuts.h include/problems/staff-rostering-problem/SRP_Instance.h src/problems/staff-rostering-problem/SRP_Instance.cpp
add_library(idol STATIC include/modeling/numericals.h include/containers/Optional.h include/containers/Map.h include/modeling.h include/modeling/parameters/Param.h include/modeling/Types.h include/modeling/expressions/Constant.h src/modeling/expressions/Constant.cpp include/modeling/matrix/AbstractMatrixCoefficient.h include/modeling/matrix/MatrixCoefficientReference.h include/modeling/matrix/MatrixCoefficient.h src/modeling/matrix/MatrixCoefficient.cpp src/modeling/matrix/MatrixCoefficientReference.cpp include/modeling/constraints/TempCtr.h src/modeling/constraints/TempCtr.cpp include/containers/IteratorForward.h include/modeling/solutions/AbstractSolution.h include/modeling/solutions/Solution.h include/modeling/variables/TempVar.h include/containers/Set.h include/errors/NotImplemented.h include/modeling/expressions/LinExpr.h include/modeling/matrix/Row.h src/modeling/matrix/Row.cpp src/modeling/matrix/Column.cpp include/modeling/matrix/Column.h include/modeling/matrix/Matrix.h src/modeling/matrix/Matrix.cpp src/problems/generalized-assignment-problem/GAP_Instance.cpp include/problems/knapsack-problem/KP_Instance.h src/problems/knapsack-problem/KP_Instance.cpp include/problems/multiple-knapsack-problem/MKP_Instance.h src/problems/multiple-knapsack-problem/MKP_Instance.cpp include/modeling/expressions/Expr.h include/modeling/expressions/operations/operators_Var.h src/modeling/expressions/operations/operators_Var.cpp src/modeling/expressions/operations/operators_Ctr.cpp src/modeling/expressions/operations/operators_Constant.cpp include/modeling/expressions/operations/operators_Constant.h include/modeling/expressions/operations/operators_Ctr.h include/modeling/expressions/operations/operators.h include/problems/multiple-knapsack-problem/MKP_Instance.h src/problems/multiple-knapsack-problem/MKP_Instance.cpp src/problems/facility-location-problem/FLP_Instance.cpp include/problems/facility-location-problem/FLP_Instance.h include/problems/helpers/distances.h include/problems/helpers/parse_delimited.h include/modeling/expressions/operations/operators_utils.h include/modeling/expressions/QuadExpr.h include/modeling/expressions/AbstractExpr.h include/modeling/models/Model.h include/modeling/objects/Env.h include/modeling/variables/Var.h src/modeling/objects/Env.cpp include/modeling/variables/VarVersion.h include/modeling/objects/Versions.h include/modeling/objects/Object.h include/modeling/objects/ObjectId.h src/modeling/models/Model.cpp include/modeling/constraints/Ctr.h include/modeling/constraints/CtrVersion.h include/modeling/objects/Version.h src/modeling/variables/Var.cpp src/modeling/constraints/Ctr.cpp include/optimizers/Optimizer.h include/optimizers/solvers/gurobi/Optimizers_Gurobi.h include/optimizers/solvers/OptimizerWithLazyUpdates.h src/optimizers/solvers/gurobi/Optimizers_Gurobi.cpp src/optimizers/Optimizer.cpp src/optimizers/Logger.cpp include/optimizers/Algorithm.h src/optimizers/Algorithm.cpp src/optimizers/Timer.cpp include/modeling/annotations/Annotation.h src/modeling/annotations/impl_Annotation.cpp include/modeling/annotations/impl_Annotation.h include/modeling/models/Model.h include/containers/GeneratorPool.h include/modeling/solutions/types.h include/optimizers/solvers/Optimizers_GLPK.h include/solvers.h src/optimizers/solvers/Optimizers_GLPK.cpp include/optimizers/solvers/Optimizers_Mosek.h src/optimizers/solvers/Optimizers_Mosek.cpp include/linear-algebra/MatrixIndices.h include/linear-algebra/SquareMatrix.h include/linear-algebra/to_rotated_quadratic_cone.h include/optimizers/branch-and-bound/Optimizers_BranchAndBound.h include/optimizers/OptimizerFactory.h include/optimizers/solvers/DefaultOptimizer.h include/optimizers/branch-and-bound/BranchAndBound.h include/optimizers/branch-and-bound/branching-rules/factories/BranchingRuleFactory.h include/optimizers/branch-and-bound/node-selection-rules/factories/NodeSelectionRuleFactory.h include/optimizers/branch-and-bound/branching-rules/factories/MostInfeasible.h include/optimizers/branch-and-bound/node-selection-rules/factories/DepthFirst.h include/optimizers/branch-and-bound/branching-rules/impls/BranchingRule.h include/optimizers/branch-and-bound/node-selection-rules/impls/NodeSelectionRule.h include/optimizers/branch-and-bound/nodes/Node.h include/optimizers/branch-and-bound/nodes/NodeUpdator.h include/optimizers/branch-and-bound/nodes/NodeUpdatorByBound.h src/optimizers/branch-and-bound/nodes/NodeInfo.cpp include/optimizers/branch-and-bound/nodes/NodeInfo.h include/optimizers/branch-and-bound/branching-rules/impls/MostInfeasbile.h include/optimizers/branch-and-bound/node-selection-rules/impls/DepthFirst.h include/optimizers/branch-and-bound/branching-rules/impls/VariableBranchingRule.h include/optimizers/branch-and-bound/node-selection-rules/factories/BreadthFirst.h include/optimizers/branch-and-bound/node-selection-rules/factories/WorstBound.h include/optimizers/branch-and-bound/node-selection-rules/factories/BestBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/WorstBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/BestBound.h include/optimizers/branch-and-bound/node-selection-rules/impls/BreadthFirst.h include/optimizers/solvers/gurobi/Gurobi.h include/optimizers/solvers/GLPK.h include/optimizers/solvers/Mosek.h include/optimizers/column-generation/ColumnGeneration.h include/optimizers/column-generation/Optimizers_ColumnGeneration.h src/optimizers/column-generation/ColumnGeneration.cpp src/optimizers/column-generation/Optimizers_ColumnGeneration.cpp include/optimizers/dantzig-wolfe/DantzigWolfeDecomposition.h src/optimizers/dantzig-wolfe/DantzigWolfeDecomposition.cpp include/optimizers/dantzig-wolfe/Optimizers_DantzigWolfeDecomposition.h src/optimizers/dantzig-wolfe/Optimizers_DantzigWolfeDecomposition.cpp src/optimizers/solvers/gurobi/Gurobi.cpp src/optimizers/solvers/GLPK.cpp src/optimizers/solvers/Mosek.cpp include/optimizers/column-generation/OptimizerFactoryWithColumnGenerationParameters.h include/optimizers/branch-and-bound/callbacks/BranchAndBoundCallbackFactory.h include/optimizers/column-generation/IntegerMasterHeuristic.h src/optimizers/column-generation/IntegerMasterHeuristic.cpp src/optimizers/solvers/gurobi/GurobiCallbackI.cpp include/optimizers/callbacks/Callback.h src/optimizers/callbacks/Callback.cpp include/optimizers/callbacks/CutSeparation.h src/optimizers/callbacks/CutSeparation.cpp include/optimizers/callbacks/LazyCutCallback.h include/optimizers/callbacks/UserCutCallback.h include/optimizers/callbacks/CallbackFactory.h include/optimizers/branch-and-bound/callbacks/BranchAndBoundCallback.h include/optimizers/branch-and-bound/callbacks/AbstractBranchAndBoundCallbackI.h include/optimizers/branch-and-bound/callbacks/CallbackAsBranchAndBoundCallback.h include/optimizers/branch-and-bound/cutting-planes/CuttingPlaneGenerator.h include/optimizers/branch-and-bound/cutting-planes/CoverCuts.h include/problems/staff-rostering-problem/SRP_Instance.h src/problems/staff-rostering-problem/SRP_Instance.cpp
src/modeling/numericals.cpp)

find_package(OpenMP REQUIRED)
Expand Down
4 changes: 4 additions & 0 deletions lib/include/optimizers/callbacks/CutSeparation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class idol::impl::CutSeparation : public Callback {
CallbackEvent m_triggering_event;
std::unique_ptr<Model> m_separation_problem;
TempCtr m_cut;

double m_tolerance = idol::Tolerance::Feasibility;
protected:
std::pair<::idol::Expr<Var, Var>, ObjectiveSense> create_separation_objective(const Solution::Primal& t_primal_solution);

void operator()(CallbackEvent t_event) override;

virtual void hook_add_cut(const TempCtr& t_cut) = 0;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ idol::impl::CutSeparation::CutSeparation(CallbackEvent t_triggering_event,
m_log_level(t_level),
m_log_color(t_color) {

if (!m_cut.row().quadratic().empty()) {
throw Exception("Adding non-linear cut is not available.");
}

}

void idol::impl::CutSeparation::operator()(CallbackEvent t_event) {
Expand All @@ -26,27 +30,10 @@ void idol::impl::CutSeparation::operator()(CallbackEvent t_event) {

const auto& current_solution = primal_solution();

const auto& row = m_cut.row();

if (!row.quadratic().empty()) {
throw Exception("Adding non-linear cut is not available in Gurobi.");
}

::idol::Expr objective = row.rhs().numerical();

for (const auto& [param, coeff] : row.rhs().linear()) {
objective += coeff * param.as<Var>();
}

for (const auto& [var, constant] : row.linear()) {
::idol::Expr term = -constant.numerical();
for (const auto& [param, coeff] : constant.linear()) {
term += -coeff * param.as<Var>();
}
objective += term * current_solution.get(var);
}
auto [objective, sense] = create_separation_objective(current_solution);

m_separation_problem->set_obj_expr(std::move(objective));
m_separation_problem->set_obj_sense(sense);

idol_Log(Debug, "Start solving separation problem");
m_separation_problem->optimize();
Expand All @@ -65,22 +52,45 @@ void idol::impl::CutSeparation::operator()(CallbackEvent t_event) {

m_separation_problem->set_solution_index(k);

if (k == 0 && m_separation_problem->get_best_obj() >= -1e-3) {
std::cout << "STOP" << std::endl;
idol_Log(Debug, "Solution " << k << " has a violation of " << std::abs(m_separation_problem->get_best_obj()) << ".")

if (k == 0 && m_separation_problem->get_best_obj() >= -m_tolerance) {
break;
}

idol_Log(Debug, "Violation: " << m_separation_problem->get_best_obj() << ".")

const auto& solution = save_primal(*m_separation_problem);

TempCtr cut(m_cut.row().fix(solution), m_cut.type());

idol_Log(Trace, "Added " << cut << ".")
idol_Log(Trace, "The following cut was added: " << cut << ".")

hook_add_cut(cut);
}

idol_Log(Info, "Added " << k << " cuts.")
idol_Log(Info, "In total, " << k << " cuts have been added.")

}

std::pair<idol::Expr<idol::Var, idol::Var>, idol::ObjectiveSense>
idol::impl::CutSeparation::create_separation_objective(const idol::Solution::Primal &t_primal_solution) {

const auto sense = m_cut.type() == LessOrEqual ? Minimize : Maximize;

const auto& row = m_cut.row();

::idol::Expr result = row.rhs().numerical();

for (const auto& [param, coeff] : row.rhs().linear()) {
result += coeff * param.as<Var>();
}

for (const auto& [var, constant] : row.linear()) {
::idol::Expr term = -constant.numerical();
for (const auto& [param, coeff] : constant.linear()) {
term += -coeff * param.as<Var>();
}
result += term * t_primal_solution.get(var);
}

return std::make_pair(std::move(result), sense);
}

0 comments on commit 1349087

Please sign in to comment.