OR-Tools  9.3
arc_flow_builder.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// This code builds an arc-flow generator for vector-bin-packing problems.
15// see https://people.math.gatech.edu/~tetali/PUBLIS/CKPT.pdf
16// It implements a non-recursive version of algorithm 1 described in:
17// http://www.dcc.fc.up.pt/~fdabrandao/papers/arcflow_manuscript.pdf
18// And in (poster version):
19// http://www.dcc.fc.up.pt/~fdabrandao/papers/arcflow_poster.pdf
20// Available at:
21// https://drive.google.com/open?id=1y-Vs1orv-QHO4lb2sjVWrZr9GQd5d2st
22// https://drive.google.com/open?id=1fsWRqgNJ_3ClrhoKIeVc1EOd5s8Mj33i (poster)
23// Some improvements are not yet implemented:
24// - Lifted stated: when storing a state of the dynamic programming forward
25// pass, one can lift a state. A lifted state of a state S is a maximal
26// increase of S that does not lose any state in the forward pass.
27// A simple example is the following:
28// bin, 1 dimension, capacity 5
29// 2 item of size 2.
30// After adding item 1 in the DP pass, the state is (2).
31// The lifted state is (3) that is (5) - (2) which is the maximal increase
32// of (2) that does not loose any state.
33// To limit time spent computing this, one can lift a state only if the
34// remaining number of item is below a threshold.
35// - Disable the backward pass (compress state towards the bin capacity).
36// Although this reduces the graph a lot, this simplication is not valid
37// when the cost is not the number of bins, but a function of the capacity
38// used (useful for fair allocation).
39
40#ifndef OR_TOOLS_PACKING_ARC_FLOW_BUILDER_H_
41#define OR_TOOLS_PACKING_ARC_FLOW_BUILDER_H_
42
43#include <cstdint>
44#include <set>
45#include <vector>
46
47#include "absl/container/flat_hash_map.h"
49
50namespace operations_research {
51namespace packing {
52
53// Arc flow gragh built from a vector bin packing problem.
54// The first node will always be the source. The last will always be the sink
55// of the arc-flow graph.
57 struct Arc {
58 int source;
61
62 // Needed for std::set.
63 bool operator<(const Arc& other) const;
64 };
65
66 std::vector<Arc> arcs;
67 // All the nodes explored during the DP phase.
68 // In the forward pass, these are the consumed capacity of the bin at this
69 // state. In the backward pass, this is pushed up towards the max capacity
70 // of the bin. In the final compression phase, this is pushed down towards
71 // the initial zero state.
72 std::vector<std::vector<int>> nodes;
73 // Debug info.
75};
76
77// Main method.
78
79// Arc flow builder. The input must enforce the following constraints:
80// - item_dimensions_by_type.size() == demand_by_type.size() == num types
81// - for each type t:
82// item_dimensions_by_type[t].size() == bin_dimensions.size() ==
83// num_dimensions
85 const std::vector<int>& bin_dimensions,
86 const std::vector<std::vector<int>>& item_dimensions_by_type,
87 const std::vector<int>& demand_by_type);
88
89} // namespace packing
90} // namespace operations_research
91
92#endif // OR_TOOLS_PACKING_ARC_FLOW_BUILDER_H_
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.
std::vector< std::vector< int > > nodes