Optimization — The Knapsack Problem
Learning objective: Solve NP-hard problems approximately via mass-action kinetics.
The previous chapter tested whether an ODE relaxation could rank moves in a constraint problem, and found that on the Sudoku net as built it cannot yet. This chapter applies the same continuous relaxation to a combinatorial optimization problem small enough that the dynamics can be solved by hand, so we can say exactly what the relaxation sees and what it misses.
The 0/1 knapsack problem asks: given items with weights and values, select a subset that maximizes total value within a weight budget. It’s NP-hard. Traditional approaches use branch-and-bound — systematic enumeration with pruning. The Petri net approach uses mass-action kinetics — items compete for capacity through continuous dynamics. Repeating the simulation with one item removed at a time shows which removal raises the value the dynamics settle at.
The 0/1 Knapsack as a Petri Net
Problem Definition
Four items with different weights and values, capacity budget of 15:
| Item | Weight | Value | Efficiency (v/w) |
|---|---|---|---|
| item0 | 2 | 10 | 5.0 |
| item1 | 4 | 10 | 2.5 |
| item2 | 6 | 12 | 2.0 |
| item3 | 9 | 18 | 2.0 |
These are the values in go-pflow’s examples/knapsack, which produced every simulation number in this chapter; the example’s README records its output. The numbers were not re-run for this chapter, and no test pins them (the example only prints them), but each one also follows from the closed form derived below.
The optimal solution takes items 0, 1, and 3 — total weight 15, total value 38. The example prints that as a constant rather than computing it; it is checked by enumeration under Comparison with Branch-and-Bound. Item2 and item3 tie on efficiency at 2.0, so the ratio can’t choose between them; capacity does. After items 0 and 1 take 6 units, item3 fills the remaining 9 exactly, while item2 would leave 3 idle (items 0, 1, 2 weigh 12 and are worth 32).
Net Structure
The Petri net encodes the knapsack as a dynamic system:
- Item places: Each holds 1 token (item available) or 0 (item taken). Under the discrete firing rule this enforces the 0/1 constraint: an item is taken once or not at all. The ODE relaxes it, so an item place can settle anywhere between 0 and 1.
- Capacity place: Holds tokens representing available weight budget. Initial marking = 15 (the weight limit).
- Take transitions: One per item. Consumes the item token plus capacity tokens equal to the item’s weight. Produces tokens into value and weight tracking places.
For item0 (weight 2, value 10), with arc weights in parentheses:
item0 ----(1)---> take_item0 ---(10)--> value
^ \
capacity ---(2)----+ `--(2)---> weight
createKnapsackNet in main.go builds these places and arcs, and the same net in the editor’s JSON format is checked into go-pflow as a parse golden, where the transitions are named txn0–txn3.
The arc from capacity to take_item0 has weight 2 — taking item0 costs 2 units of capacity. The enabling condition requires AND . If the remaining capacity is less than 2, this item can’t be taken.
The structural point is that all items compete for the same capacity pool. Taking item2 (weight 6) leaves less capacity for item3 (weight 9). This competition is encoded in the shared input place, not in any explicit exclusion logic.
Why Every Item Is Taken Equally
In the basic model, all transitions have rate 1.0. go-pflow’s mass-action kinetics are first-order in every input place (solver/ode.go):
The arc weight scales how much capacity each firing consumes, not the rate: it does not appear as an exponent. (Chemical mass action would raise to the power ; go-pflow does not.)
This makes the dynamics solvable by hand. Let . Each item place obeys , so
— the same curve for every item, whatever its weight or value. Values never enter the dynamics at all; they sit on output arcs into a place nothing reads. All active items are therefore taken in one common fraction , and the remaining capacity is , where is the total weight of the active items. If , capacity runs out at and the net settles at value , with the total value of the active items. If , and the net eventually collects all of . Every number in the rest of this chapter follows from that formula.
Continuous Relaxation and Rounding
Running the ODE simulation with uniform rates:
Final state (continuous approximation):
Value accumulated: 35.71
Weight used: 15.00
Capacity remaining: 0.00
Item consumption (fraction taken):
item0: 71.4% taken
item1: 71.4% taken
item2: 71.4% taken
item3: 71.4% taken
The continuous relaxation takes fractional amounts of each item — all at exactly , as the formula predicts (, , value ). The total value is 35.71, compared to the discrete optimum of 38.
The fractional solution looks like the LP (linear programming) relaxation of the knapsack, but it is not one: an LP relaxation is an upper bound on the integer optimum (here the LP value is 38 — items 0 and 1 whole, then 9 units at ratio 2.0), while the ODE settles at 35.71, below the integer optimum of 38. Mass-action kinetics spreads consumption across items proportionally; it is a smooth dynamics, not a maximiser. The gap between 35.71 and 38 tells us that simple rounding won’t find the optimum. We need more information about the solution structure.
Exclusion Analysis for Sensitivity
Exclusion analysis disables each item’s transition one at a time (main.go sets the item’s token and its rate to 0) and re-runs the ODE to :
| Excluded | Final Value | Relative to Baseline |
|---|---|---|
| none | 35.71 | 100.0% |
| item0 | 31.58 | 88.4% |
| item1 | 35.29 | 98.8% |
| item2 | 37.75 | 105.7% |
| item3 | 32.00 | 89.6% |
Three of the four items behave as expected — excluding them reduces the total value. But item2 is anomalous: excluding it increases the value from 35.71 to 37.75.
The formula explains the whole table. While the active items overweigh the capacity, the value is — capacity times the aggregate value density of the active set. Excluding item0 costs the most because it is the densest item: falls from to , giving 31.58. Excluding item3 leaves items weighing 12, which fit, so the net collects their full value of 32. Item2 is the only exclusion that raises the value, because without it the remaining weights sum to exactly 15 and the net can take all of items 0, 1 and 3.
So item2 is “hurting” the solution in a precise sense: its exclusion is the one that makes the rest fit. On this instance one sensitivity pass — five ODE runs — points at the optimum. Single exclusions only examine subsets of size , though, and on a larger instance the optimum can drop several items; that this heuristic finds the optimum in general is not shown here.
Convergence After Exclusion
With item2’s transition disabled, the ODE converges toward the discrete optimum:
| Time | Value | Gap to Optimal |
|---|---|---|
| t=10 | 37.75 | 0.2517 |
| t=100 | 37.97 | 0.0253 |
| t=1000 | 38.00 | 0.0025 |
Given enough simulation time, the continuous relaxation without item2 converges to the exact discrete optimum of 38. The remaining items (0, 1, 3) weigh exactly the capacity, so capacity and items drain together: integrates to , and the gap to 38 is (0.2517, 0.0253 and 0.0025 at the three times, matching the table) — algebraic rather than exponential, which is why it takes until to close.
The exclusion step removes the item whose presence keeps the rest from fitting; the ODE then fills the set that remains. It reaches the integer optimum here because , not because the dynamics select it.
Comparison with Branch-and-Bound
The two approaches work through different mechanisms, and only one of them solves the problem.
Branch-and-bound treats optimization as search. It builds a decision tree where each node represents a binary choice: take this item or skip it. The algorithm explores branches, computing upper bounds to prune paths that can’t improve on the best solution found so far. It’s systematic enumeration with smart pruning — still exponential in the worst case, but practical for moderate problem sizes.
The ODE treats optimization as simulation. Instead of explicit decisions, items compete for capacity through continuous dynamics. The mass-action kinetics means all enabled transitions fire simultaneously at rates proportional to available resources. There’s no decision tree — just differential equations evolving toward equilibrium.
| Aspect | Branch-and-Bound | ODE |
|---|---|---|
| Core operation | Decision-tree search with bounds | Continuous dynamics |
| Decisions | Explicit (take/skip) | None — every active item is taken in the same fraction |
| Solution type | Exact integer | Fractional approximation |
| Cost | Exponential worst case (pruned) | One ODE solve per run; does not solve the integer problem |
| Primary output | The optimal subset | A relaxed value, and its sensitivity to each item |
For the exact answer, search wins. No branch-and-bound was run on this instance, and go-pflow has no implementation, but on four items plain enumeration settles it: of the 16 subsets, three are over capacity ({0, 2, 3} at 17, {1, 2, 3} at 19, all four at 21), and of the thirteen that fit, {0, 1, 3} is the best at 38, then {0, 1, 2} at 32. Any exact method has to return that subset. The ODE reaches 35.71 by taking the same fraction of everything.
What the ODE adds is the sensitivity table. The exclusion pass singles out item2 as the one item whose removal raises the relaxed value; branch-and-bound reaches the same subset by pruning, without reporting why. The closed form says how much that table can carry: the dynamics never read the values, so on this instance the signal is the weight fit — without item2 the remaining items fill capacity exactly.
When to Use Each
The two can be chained: run the exclusion pass to find items whose removal helps, then give the reduced instance to branch-and-bound or dynamic programming for the exact answer. This chapter shows the first half on one four-item instance; no example in go-pflow runs the combined workflow.
Beyond Four Items
The 4-item example is intentionally small. For larger instances:
-
100 items: Each ODE run stays cheap — the number of transitions scales linearly — and single-item exclusion analysis is 101 runs. But single exclusions only look at 99-item subsets, and the optimum can drop many items, so this does not replace branch-and-bound’s exponential worst case; a benchmark on larger instances has not been done.
-
Items with equal efficiency: item2 and item3 tie at 2.0 here, and the exclusion pass separates them only by how their weights fit the capacity, since the dynamics never read values. Whether it tells interchangeable items apart on other instances has not been demonstrated.
-
Multiple constraints: A second constraint (volume, fragility) makes this the multidimensional knapsack problem, which is harder for any method. In the net it is a second capacity place with its own arc weights. Each take transition then has three input places, so every item still drains along one shared curve, now with the integral of , and the net stops at the fraction where the first capacity runs out. That follows from the same derivation;
examples/knapsackhas no such variant to run.
The Pattern
The knapsack model demonstrates a fourth application pattern, distinct from resources (Chapter 5), games (Chapter 6), and constraints (Chapter 7):
- Decision variables become places (items available or taken)
- Resource constraints become shared capacity places with weighted arcs
- Competition comes from the shared input place: under uniform rates every item is drawn down by the same fraction, and capacity in proportion to weight
- Exclusion analysis shows which single removal raises the relaxed value
- Continuous relaxation settles below the optimum here; the exclusion pass leads to the exact solution on this instance
The ODE does not find the optimum. It produces a relaxed value and a sensitivity table, and on this instance the table leads straight to the optimum. On larger instances it can at best guide a search, and that has not been benchmarked.
Try it live: This chapter’s net is small enough to write as model JSON (Appendix C). Store it with
POST https://sim.pflow.xyz/api/models, no account needed, and open the returned id in the what-if console at/whatif/?id={id}.