23#ifndef __PBeFS_Knapsack
24 #define __PBeFS_Knapsack
77template<
class V =
double >
86 std::vector< char >
x;
91 std::vector< long >
p;
92 std::vector< long >
w;
98 n.
x.assign(
p.size() , 0 );
105 for( std::size_t i = n.
k ; i <
p.size() ; ++i )
120 double b = double( n.
profit );
122 for( ; ( j <
p.size() ) && (
w[ j ] <= r ) ; ++j ) {
124 b += double(
p[ j ] );
127 b += double(
p[ j ] ) * double( r ) / double(
w[ j ] );
128 if constexpr( std::is_integral_v< V > )
129 e.
bound = V( std::floor( b ) );
138 if( n.
k ==
p.size() )
143 const auto bound = [ & ](
node_type & x ) {
153 const V bc = bound( c );
154 f( std::move( c ) , bc );
159 const V bt = bound( t );
160 f( std::move( t ) , bt );
165 return( ADS::splitmix64( ( std::uint64_t( n.
k ) << 40 ) ^
166 ( std::uint64_t( n.
weight ) << 20 ) ^
167 std::uint64_t( n.
profit ) ) );
171 return( V(
greedy( n ) ) );
176 const auto put = [ & ](
const auto & v ) {
177 const auto * b =
reinterpret_cast< const char *
>( & v );
178 out.insert( out.end() , b , b +
sizeof( v ) );
183 out.insert( out.end() , n.
x.begin() , n.
x.end() );
188 std::memcpy( & n.
k , in ,
sizeof( n.
k ) );
194 n.
x.assign( in , in +
p.size() );
201 std::vector< long > best( std::size_t(
cap ) + 1 , 0 );
202 for( std::size_t i = 0 ; i <
p.size() ; ++i )
203 for(
long c =
cap ; c >=
w[ i ] ; --c )
204 best[ c ] = std::max( best[ c ] , best[ c -
w[ i ] ] +
p[ i ] );
205 return( best[
cap ] );
212 std::uint64_t seed ) {
213 std::mt19937_64 gen( seed );
214 std::vector< std::pair< long , long > > it( n );
216 for(
auto & [ pi , wi ] : it ) {
217 wi = 1 + long(
random_below( gen , std::uint64_t( r ) ) );
219 pi = 1 + long(
random_below( gen , std::uint64_t( r ) ) );
223 gen , std::uint64_t( r / 5 + 1 ) ) ) );
228 std::sort( it.begin() , it.end() , [](
const auto & a ,
const auto & b ) {
229 return( a.first * b.second > b.first * a.second );
232 for(
const auto & [ pi , wi ] : it ) {
233 kp.
p.push_back( pi );
234 kp.
w.push_back( wi );
242 long wt = 0 , pt = 0;
243 for( std::size_t i = 0 ; i < n.
k ; ++i )
249 ( V(
greedy( n ) ) == v ) );
Header file for the best-first search with an incumbent, i.e., for Branch-and-Bound (B&B): the concep...
namespace for the Parallel Best-First Search library
Definition AStar.h:83
std::uint64_t random_below(Gen &gen, std::uint64_t n)
an integer in [ 0 , n ) from the next word of a 64-bit generator, n > 0
Definition SearchUtils.h:165
BasicKnapsack<> Knapsack
the 0-1 knapsack with values of type double
Definition Knapsack.h:257
Sense
the sense of the optimization
Definition BranchAndBound.h:131
@ maximize
the largest value is sought
Definition BranchAndBound.h:133
static constexpr V infinity(void) noexcept
the infinite value
Definition BranchAndBound.h:212
the outcome of the evaluation of a node, with values of type V
Definition BranchAndBound.h:155
V bound
a bound on the values of the node
Definition BranchAndBound.h:156
std::optional< V > feasible
a feasible value, n being its solution, if there is one
Definition BranchAndBound.h:158
bool infeasible
true if the node has no solution
Definition BranchAndBound.h:157
long weight
the weight of those taken
Definition Knapsack.h:84
std::vector< char > x
the items taken
Definition Knapsack.h:86
long profit
their profit
Definition Knapsack.h:85
std::size_t k
the items fixed
Definition Knapsack.h:83
a 0-1 knapsack, its items sorted by decreasing ratio of profit to weight
Definition Knapsack.h:78
std::uint64_t hash(const node_type &n) const
Definition Knapsack.h:164
V value_type
the type of the values
Definition Knapsack.h:80
static constexpr Sense sense
Definition Knapsack.h:89
static BasicKnapsack random(int cls, std::size_t n, long r, std::uint64_t seed)
Definition Knapsack.h:211
void serialize(const node_type &n, std::vector< char > &out) const
Definition Knapsack.h:175
BasicEvaluation< V > evaluate(node_type &n) const
Definition Knapsack.h:113
long cap
the capacity
Definition Knapsack.h:93
long optimum(void) const
Definition Knapsack.h:200
node_type deserialize(const char *&in) const
Definition Knapsack.h:186
void branch(const node_type &n, F &&f) const
Definition Knapsack.h:137
std::vector< long > p
the profits
Definition Knapsack.h:91
long greedy(const node_type &n) const
Definition Knapsack.h:103
bool explicit_children
own bounds, infeasible children
Definition Knapsack.h:94
node_type root(void) const
Definition Knapsack.h:96
std::vector< long > w
the weights
Definition Knapsack.h:92
bool check(const node_type &n, V v) const
Definition Knapsack.h:241
V estimate(const node_type &n) const
Definition Knapsack.h:170