This is the second post in my series about rebuilding the daily fantasy lineup optimizer I first wrote in Excel back in 2012 (the origin story is here). Before I get into the clever parts, I want to start with the simplest and most important idea: a fantasy lineup is just a knapsack problem wearing a baseball cap. Once you see that, everything the optimizer does makes sense.

The knapsack problem, in one sentence
The classic knapsack problem goes like this: you have a bag that can hold a fixed amount of weight, and a pile of items that each have a weight and a value. You want to fill the bag with the most total value without going over the weight limit. That’s it. It shows up everywhere once you start looking — budgeting, cargo loading, cutting stock. I once used the same idea to model the electoral college as a knapsack problem, where the “weight” was electoral votes and the “value” was the cost of campaigning in each state.
Now swap in a fantasy lineup
A daily fantasy contest hands you the exact same puzzle. The bag is your salary cap — on DraftKings MLB that’s $50,000. The items are the players, and each one has a price (their salary) and a value (their projected fantasy points). Your job is to pick the set of players that scores the most projected points without the salaries adding up past the cap. Weight becomes salary, value becomes points, and the bag becomes the cap. Same problem, different costume.
There’s one extra wrinkle that plain knapsack doesn’t have: you can’t just grab any ten players. You need a specific roster — a couple of pitchers, a catcher, infielders, outfielders. So on top of the budget, you have a handful of “you must take exactly this many of that type” rules. In optimization terms those are just more constraints, and they’re easy to add. The shape of the problem doesn’t change.
Why you can’t just pick the best values by hand
The obvious approach is to rank everyone by points-per-dollar and grab the best deals until the money runs out. That feels right, and it’s wrong often enough to cost you. The trouble is the roster slots and the cap interact. The best value at one position might force you into a bad value at another because of what’s left in the budget. Greedy picking can paint you into a corner where the last slot has to be filled by whoever’s cheap, not whoever’s good. The only way to be sure you’ve got the genuine best lineup is to consider the whole thing at once — which is exactly what an optimizer does and exactly what your gut can’t.
What the solver actually does
Under the hood, the tool writes this down as what’s called a mixed-integer linear program. Every player gets a yes/no switch — in the lineup or not. The “score” to maximize is the sum of projected points for the players switched on. The rules are written as math: the salaries of the switched-on players must total at most the cap; the number switched on at each position must match the roster. Then a solver flips through the combinations far more cleverly than brute force and hands back the highest-scoring legal lineup. In 2012 I did this with Excel’s built-in Solver. The version running today uses a proper open-source solver in Python, but the idea is identical to what I set up in a spreadsheet over a decade ago.
If that sounds like a lot of machinery for “pick a good lineup,” you’re right — and for a single lineup, it’s honestly overkill. Any decent free tool can spit out one optimal lineup. The interesting problems start when you want twenty lineups that are each strong but meaningfully different from one another, which is where stacking and diversity come in. That’s the subject of a later post in this series, and it’s the part that actually separates a good entry from a lucky one.
A quick example of greedy going wrong
Say you have $10 left and two slots to fill. The best “value” player available costs $9 and projects for 30 points. Grab him — he’s the best deal on the board — and you’ve got $1 for your last slot, which buys a player projecting 2 points. Total: 32. But if you’d passed on the $9 stud and taken two $5 players projecting 18 each, you’d have 36. The greedy pick felt obviously right at every step and still left points on the table, because it never looked ahead to what the last slot would cost you. Multiply that trap across ten slots and a $50,000 budget and you can see why eyeballing it loses to a solver that weighs every combination at once.
The part the optimizer can’t do for you
There’s one ingredient I’ve been quietly assuming: the projected points for each player. That number is the entire soul of the model. The optimizer takes those projections as gospel and finds the best lineup given them — but it has no idea whether they’re any good. Garbage projections in, a beautifully optimized garbage lineup out. This is the honest limit of any optimizer, mine included, and it’s why the expensive tools spend most of their effort on the projection and ownership data, not the math.
I took the opposite approach on purpose. The tool will use a baseline projection from the file you upload, but it also lets you paste in your own numbers and override everything. If you’ve done the homework — checked the matchups, the weather, who’s actually in the starting lineup — your projections will beat any generic feed, and the optimizer will faithfully build around them. The math is a commodity; your read on the players is the edge.
The takeaway
You don’t need to know the math to use the tool — that was the whole lesson from my Fiverr days. But it helps to know that there’s no magic and no secret data behind it. It’s a budget, a list of priced items with values, a few roster rules, and a solver that respects all of them at once. A knapsack in a jersey. If you want to try it on tonight’s slate, the MLB lineup optimizer is free and runs in your browser — bring your own projections or use the site’s, and it does the knapsack part for you.
Things that I use, like, and am affiliated with:
Mint Mobile offers great cell phone service for $15 flat, get $15 off using the link. Get discounted phones with service activation and no contract.
I never spend money before I check Mr Rebates or Rakuten to get cashbacks, rebates, discounts, coupons or cheaper gift cards.
