Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
arc_flow_builder.h
Go to the documentation of this file.
1// Copyright 2010-2024 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"
48#include "absl/types/span.h"
49#include "ortools/base/types.h"
50
51namespace operations_research {
52namespace packing {
53
54// Arc flow gragh built from a vector bin packing problem.
55// The first node will always be the source. The last will always be the sink
56// of the arc-flow graph.
58 struct Arc {
59 int source;
62
63 // Needed for std::set.
64 bool operator<(const Arc& other) const;
65 };
66
67 std::vector<Arc> arcs;
68 // All the nodes explored during the DP phase.
69 // In the forward pass, these are the consumed capacity of the bin at this
70 // state. In the backward pass, this is pushed up towards the max capacity
71 // of the bin. In the final compression phase, this is pushed down towards
72 // the initial zero state.
73 std::vector<std::vector<int>> nodes;
74 // Debug info.
76};
77
78// Main method.
79
80// Arc flow builder. The input must enforce the following constraints:
81// - item_dimensions_by_type.size() == demand_by_type.size() == num types
82// - for each type t:
83// item_dimensions_by_type[t].size() == bin_dimensions.size() ==
84// num_dimensions
86 const std::vector<int>& bin_dimensions,
87 absl::Span<const std::vector<int>> item_dimensions_by_type,
88 absl::Span<const int> demand_by_type);
89
90} // namespace packing
91} // namespace operations_research
92
93#endif // OR_TOOLS_PACKING_ARC_FLOW_BUILDER_H_
ArcFlowGraph BuildArcFlowGraph(const std::vector< int > &bin_dimensions, absl::Span< const std::vector< int > > item_dimensions_by_type, absl::Span< const int > demand_by_type)
Main method.
In SWIG mode, we don't want anything besides these top-level includes.
bool operator<(const Arc &other) const
Needed for std::set.
std::vector< std::vector< int > > nodes