69 : index(0), weight(0), value(0), chosen(n, 0)
88 : items_(items), capacity_(capacity)
94 bool is_complete(
const State &state)
const
96 return state.index == items_.size();
100 Objective objective_value(
const State &state)
const
106 Objective bound(
const State &state)
const
109 int remaining = capacity_ - state.weight;
111 for (
size_t i = state.index; i < items_.size()
and remaining > 0; ++i)
112 if (items_[i].weight <= remaining)
115 remaining -= items_[i].weight;
119 optimistic += items_[i].value*(
static_cast<Objective
>(remaining)/
120 static_cast<Objective
>(items_[i].weight));
128 void apply(State &state,
const Move &move)
const
132 state.weight += items_[state.index].weight;
133 state.value += items_[state.index].value;
134 state.chosen[state.index] = 1;
137 state.chosen[state.index] = 0;
143 void undo(State &state,
const Move &move)
const
148 state.weight -= items_[state.index].weight;
149 state.value -= items_[state.index].value;
152 state.chosen[state.index] = 0;
156 template <
typename Visitor>
157 bool for_each_successor(
const State &state,
Visitor visit)
const
159 if (state.index >= items_.size())
162 if (state.weight + items_[state.index].weight <= capacity_)
166 return visit(Move{
false});
176 std::cout <<
"Chosen items:";
177 for (
size_t i = 0; i < state.chosen.size(); ++i)
179 std::cout <<
' ' << i;
188 {2, 40.0}, {5, 30.0}, {10, 50.0}, {5, 10.0}
190 constexpr int capacity = 16;
194 policy.
strategy = Search::ExplorationPolicy::Strategy::Best_First;
196 auto result = Search::branch_and_bound_search(KnapsackBBDomain(items, capacity),
200 std::cout <<
"0/1 Knapsack reference example\n";
201 std::cout <<
"Capacity: " << capacity <<
'\n';
202 std::cout <<
"Strategy: best-first\n";
203 std::cout <<
"Optimal value: " << result.incumbent.best_value() <<
'\n';
204 std::cout <<
"Reference dynamic-programming value: "
206 std::cout <<
"Visited states: " << result.stats.visited_states <<
'\n';
207 std::cout <<
"Pruned by bound: " << result.stats.pruned_by_bound <<
'\n';
Classical knapsack problem variants (0/1, unbounded, bounded).
Umbrella header for the implicit state-space search framework.
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.
User-facing facade exported by the umbrella search header.
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.
An item for knapsack problems.
Dynamic array container with automatic resizing.