91template <
typename W,
typename V>
112namespace knapsack_detail {
113template <std::
integral W>
116 if constexpr (std::is_signed_v<W>)
119 using UW = std::make_unsigned_t<W>;
120 const UW uvalue =
static_cast<UW
>(
value);
121 if constexpr (
sizeof(UW) >
sizeof(
size_t))
123 << fn_name <<
": " << field_name <<
" is too large for size_t";
125 return static_cast<size_t>(uvalue);
128template <std::
integral W,
typename V>
133 for (
size_t i = 0; i < items.size(); ++i)
142 if constexpr (std::is_integral_v<V>)
143 if (b >
V{}
and a > std::numeric_limits<V>::max() - b)
144 return std::numeric_limits<V>::max();
165template <
typename V, std::
integral W>
168 const size_t n = items.size();
178 for (
size_t i = 0; i <= n; ++i)
181 for (
size_t w = 0;
w <= C; ++
w)
186 for (
size_t i = 1; i <= n; ++i)
188 const size_t wi = weights[i - 1];
189 const V vi = items[i - 1].value;
190 for (
size_t w = 0;
w <= C; ++
w)
192 dp[i][
w] = dp[i - 1][
w];
196 if (candidate > dp[i - 1][
w])
197 dp[i][
w] = candidate;
205 for (
size_t i = n; i > 0; --i)
206 if (dp[i][
w] != dp[i - 1][
w])
215 for (
size_t k = sel.
size();
k > 0; --
k)
236template <
typename V, std::
integral W>
239 const size_t n = items.
size();
247 for (
size_t w = 0;
w <= C; ++
w)
250 for (
size_t i = 0; i < n; ++i)
252 const size_t wi = weights[i];
253 const V vi = items[i].value;
255 for (
size_t w = C;
w >= wi
and w !=
static_cast<size_t>(-1); --
w)
258 if (candidate > dp[
w])
285template <
typename V, std::
integral W>
289 const size_t n = items.size();
297 for (
size_t i = 0; i < n; ++i)
299 <<
"knapsack_unbounded: zero-weight item with positive value " <<
"leads to unbounded optimum";
304 constexpr size_t NONE = std::numeric_limits<size_t>::max();
305 for (
size_t w = 0;
w <= C; ++
w)
311 for (
size_t w = 1;
w <= C; ++
w)
312 for (
size_t i = 0; i < n; ++i)
313 if (
const size_t wi = weights[i]; wi <=
w)
316 if (candidate > dp[
w])
326 while (
w > 0
and choice[
w] != NONE)
328 const size_t idx = choice[
w];
356template <
typename V, std::
integral W>
362 <<
"knapsack_bounded: items and counts must have same size";
364 const size_t n = items.size();
377 for (
size_t i = 0; i < n; ++i)
379 const size_t wi = weights[i];
380 size_t rem = counts[i];
384 const size_t take = std::min(
k, rem);
387 if constexpr (std::is_integral_v<V>)
389 if (items[i].
value ==
V{0})
391 else if (items[i].
value >
V{0})
395 using UV = std::make_unsigned_t<V>;
396 const UV value_uv =
static_cast<UV
>(items[i].value);
397 const UV maxV_uv =
static_cast<UV
>(std::numeric_limits<V>::max());
398 value_ok = (
static_cast<UV
>(take) <= maxV_uv / value_uv);
403 using UV = std::make_unsigned_t<V>;
404 const UV abs_value =
static_cast<UV
>(-(items[i].value +
V{1})) + UV{1};
406 const UV abs_min =
static_cast<UV
>(std::numeric_limits<V>::min());
407 value_ok = (
static_cast<UV
>(take) <= abs_min / abs_value);
410 else if constexpr (std::numeric_limits<V>::is_specialized)
412 const V maxV = std::numeric_limits<V>::max();
413 if (items[i].
value ==
V{0})
418 const V absV = items[i].value >
V{0} ? items[i].value : -items[i].value;
419 value_ok = (
static_cast<V>(take) <= maxV / absV);
425 if ((wi == 0 or take <= C / wi)
and value_ok)
427 const V val =
static_cast<V>(take) * items[i].
value;
437 if (
k > std::numeric_limits<size_t>::max() / 2)
449 for (
size_t k = 0;
k < result.selected_items.size(); ++
k)
451 const size_t ei = result.selected_items[
k];
452 const size_t orig = origin[ei];
453 const size_t mult = multiplier[ei];
454 for (
size_t j = 0; j < mult; ++j)
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
V dp_add(V a, V b) noexcept
Array< size_t > extract_weights_checked(const Array< Knapsack_Item< W, V > > &items, const char *fn_name)
size_t to_size_checked(const W value, const char *fn_name, const char *field_name)
Main namespace for Aleph-w library functions.
V knapsack_01_value(const Array< Knapsack_Item< W, V > > &items, W capacity)
Solve the 0/1 Knapsack problem (value only, space-optimized).
Knapsack_Result< V > knapsack_01(const Array< Knapsack_Item< W, V > > &items, W capacity)
Solve the 0/1 Knapsack problem with item reconstruction.
and
Check uniqueness with explicit hash + equality functors.
Knapsack_Result< V > knapsack_bounded(const Array< Knapsack_Item< W, V > > &items, const Array< size_t > &counts, W capacity)
Solve the Bounded Knapsack problem with reconstruction.
Knapsack_Result< V > knapsack_unbounded(const Array< Knapsack_Item< W, V > > &items, W capacity)
Solve the Unbounded Knapsack problem with reconstruction.
An item for knapsack problems.
V value
Value (or profit) of the item.
W weight
Weight (or cost) of the item.
Result of a knapsack computation.
Array< size_t > selected_items
Indices (0-based) of items selected for the optimum.
V optimal_value
Maximum total value achieved.
Dynamic array container with automatic resizing.