99 for (
size_t i = 0; i + 1 < n; ++i)
100 for (
size_t j = i + 1; j < n; ++j)
112 return s.next_item ==
static_cast<size_t>(
items_.
size());
118 return s.value_so_far;
128 double remaining =
capacity_ - s.weight_so_far;
129 double ub = s.value_so_far;
130 size_t i = s.next_item;
131 while (i <
static_cast<size_t>(
items_.
size())
and remaining > 0.0)
134 if (item.
weight <= remaining)
155 s.weight_so_far += item.
weight;
156 s.value_so_far += item.
value;
167 s.weight_so_far -= item.
weight;
168 s.value_so_far -= item.
value;
177 template <
typename Visitor>
206 const std::string &label)
215 std::cout << label <<
":\n";
216 if (result.found_solution())
218 std::cout <<
" Optimum value : " << result.incumbent.best_value() <<
"\n";
219 std::cout <<
" Expanded states: " << result.stats.expanded_states <<
"\n";
220 std::cout <<
" Pruned by bound: " << result.stats.pruned_by_bound <<
"\n";
224 std::cout <<
" No solution found.\n";
239 {
"camera", 3.0, 12.0},
240 {
"tablet", 4.0, 13.0},
241 {
"laptop", 5.0, 16.0},
244 const double capacity = 7.0;
246 run_strategy(items, capacity, ExplorationPolicy::Strategy::Depth_First,
"Depth-First B&B");
248 run_strategy(items, capacity, ExplorationPolicy::Strategy::Best_First,
"Best-First B&B");
Reusable branch-and-bound engine for implicit optimization spaces.
static void run_strategy(const Array< Item > &items, double capacity, ExplorationPolicy::Strategy strategy, const std::string &label)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Reusable branch-and-bound engine over implicit state spaces.
void undo(State &s, const Move &m) const noexcept
KnapsackDomain(const Array< Item > &items, double capacity)
Objective bound(const State &s) const noexcept
Greedy fractional upper bound on the remaining items.
bool for_each_successor(const State &s, Visitor visit) const
bool is_feasible(const State &s) const noexcept
void apply(State &s, const Move &m) const noexcept
bool is_complete(const State &s) const noexcept
True when all items have been decided.
Objective objective_value(const State &s) const noexcept
Objective value for a complete assignment.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Exploration controls shared across engines.
Strategy strategy
Traversal strategy.
Strategy
Traversal strategy supported by the engine.
size_t next_item
Index of the next item to decide.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.