16#include "absl/flags/flag.h"
23 "File to store the solver specific optimization proto.");
29double ConvertVectorBinPackingProblem(
const vbp::VectorBinPackingProblem&
input,
30 ArcFlowGraph* graph) {
33 const int num_items =
input.item_size();
34 const int num_dims =
input.resource_capacity_size();
37 std::vector<std::vector<int>> shapes(num_items);
38 std::vector<int> demands(num_items);
39 std::vector<int> capacities(num_dims);
40 for (
int i = 0; i < num_items; ++i) {
41 shapes[i].assign(
input.item(i).resource_usage().begin(),
42 input.item(i).resource_usage().end());
43 demands[i] =
input.item(i).num_copies();
45 for (
int i = 0; i < num_dims; ++i) {
46 capacities[i] =
input.resource_capacity(i);
50 for (
int i = 0; i < num_items; ++i) {
51 const int max_copies =
input.item(i).max_number_of_copies_per_bin();
52 if (max_copies == 0 || max_copies >= demands[i])
continue;
53 capacities.push_back(max_copies);
54 for (
int j = 0; j < num_items; ++j) {
55 shapes[j].push_back(i == j);
60 const double arc_flow_time = timer.
Get();
62 VLOG(1) <<
"The arc-flow grah has " << graph->nodes.size() <<
" nodes, and "
63 << graph->arcs.size() <<
" arcs. It was created by exploring "
64 << graph->num_dp_states
65 <<
" states in the dynamic programming phase in " << arc_flow_time
74 const std::string& mip_params,
double time_limit,
int num_threads,
75 bool log_statistics) {
77 const double arc_flow_time = ConvertVectorBinPackingProblem(problem, &graph);
80 for (
const auto& item : problem.
item()) {
81 max_num_bins += item.num_copies();
83 const int num_types = problem.
item_size();
84 std::vector<std::vector<MPVariable*>> incoming_vars(graph.
nodes.size());
85 std::vector<std::vector<MPVariable*>> outgoing_vars(graph.
nodes.size());
86 std::vector<MPVariable*> arc_to_var(graph.
arcs.size());
87 std::vector<std::vector<MPVariable*>> item_to_vars(num_types);
89 MPSolver solver(
"VectorBinPacking", solver_type);
92 for (
int v = 0; v < graph.
arcs.size(); ++v) {
95 solver.
MakeIntVar(0, max_num_bins, absl::StrCat(
"a", v));
104 for (
int i = 0; i < num_types; ++i) {
108 ct->SetCoefficient(
var, 1.0);
113 for (
int n = 1; n < graph.
nodes.size() - 1; ++n) {
116 ct->SetCoefficient(
var, 1.0);
119 ct->SetCoefficient(
var, -1.0);
127 ct->SetCoefficient(
var, 1.0);
129 ct->SetCoefficient(obj_var, -1.0);
134 const int sink_node = graph.
nodes.size() - 1;
136 ct->SetCoefficient(
var, 1.0);
138 ct->SetCoefficient(obj_var, -1.0);
144 if (!absl::GetFlag(FLAGS_arc_flow_dump_model).empty()) {
172 if (log_statistics) {
175 absl::PrintF(
"%-12s: %s\n",
"Status",
179 absl::PrintF(
"%-12s: %15.15e\n",
"Objective",
181 absl::PrintF(
"%-12s: %15.15e\n",
"BestBound",
183 absl::PrintF(
"%-12s: %d\n",
"Iterations", solver.
iterations());
184 absl::PrintF(
"%-12s: %d\n",
"Nodes", solver.
nodes());
ABSL_FLAG(std::string, arc_flow_dump_model, "", "File to store the solver specific optimization proto.")
#define VLOG(verboselevel)
The class for constraints of a Mathematical Programming (MP) model.
A class to express a linear objective.
void SetCoefficient(const MPVariable *const var, double coeff)
Sets the coefficient of the variable in the objective.
double Value() const
Returns the objective value of the best solution found so far.
double BestBound() const
Returns the best objective bound.
This mathematical programming (MP) solver class is the main class though which users build and solve ...
int64_t iterations() const
Returns the number of simplex iterations.
MPConstraint * MakeRowConstraint(double lb, double ub)
Creates a linear constraint with given bounds.
ResultStatus
The status of solving the problem.
@ FEASIBLE
feasible, or stopped by limit.
@ INFEASIBLE
proven infeasible.
int64_t wall_time() const
OptimizationProblemType
The type of problems (LP or MIP) that will be solved and the underlying solver (GLOP,...
bool SetSolverSpecificParametersAsString(const std::string ¶meters)
Advanced usage: pass solver specific parameters in text format.
absl::Status SetNumThreads(int num_threads)
Sets the number of threads to use by the underlying solver.
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
int64_t nodes() const
Returns the number of branch-and-bound nodes evaluated during the solve.
MPVariable * MakeIntVar(double lb, double ub, const std::string &name)
Creates an integer variable.
const MPObjective & Objective() const
Returns the objective object.
ResultStatus Solve()
Solves the problem using the default parameter values.
void EnableOutput()
Enables solver logging.
MPObjective * MutableObjective()
Returns the mutable objective object.
void SetTimeLimit(absl::Duration time_limit)
The class for variables of a Mathematical Programming (MP) model.
::PROTOBUF_NAMESPACE_ID::int32 num_copies() const
const ::operations_research::packing::vbp::Item & item(int index) const
double solve_time_in_seconds() const
void set_solve_time_in_seconds(double value)
void set_objective_value(double value)
void set_arc_flow_time_in_seconds(double value)
void set_status(::operations_research::packing::vbp::VectorBinPackingSolveStatus value)
ModelSharedTimeLimit * time_limit
absl::Status SetTextProto(const absl::string_view &filename, const google::protobuf::Message &proto, int flags)
vbp::VectorBinPackingSolution SolveVectorBinPackingWithArcFlow(const vbp::VectorBinPackingProblem &problem, MPSolver::OptimizationProblemType solver_type, const std::string &mip_params, double time_limit, int num_threads, bool log_statistics)
ArcFlowGraph BuildArcFlowGraph(const std::vector< int > &bin_dimensions, const std::vector< std::vector< int > > &item_dimensions_by_type, const std::vector< int > &demand_by_type)
Collection of objects used to extend the Constraint Solver library.
const std::string & MPSolverResponseStatus_Name(T enum_t_value)
static int input(yyscan_t yyscanner)
std::vector< std::vector< int > > nodes