lpSolve is an R package for solving linear programming and related constrained optimization problems. Its functions help turn a practical decision—such as choosing an NBA roster—into an optimization model. section 4.2
Use lpSolve by preparing an objective, constraints, and variable rules:
- Represent each candidate or decision as a variable and place constraint coefficients in a matrix, with one row per constraint and one column per variable. section 4.8
- Pass the objective to lp(), choose direction = "max" or "min", define right-hand sides and comparison directions, and identify integer or binary variables. section 4.8
- Load the package with the exact case-sensitive name library(lpSolve); comments beginning with # can document the model. section 4.2
Selecting five NBA free agents
Maximize expected win shares with lp(const.mat = constraint_matrix, objective = free_agents_sort$win_shares, direction = "max"), while enforcing position, roster-size, salary, age, and binary-selection constraints. Filtering the solution returns five players with 25.5 total win shares, $89.5 million in salaries, and an average age of 28.4. section 4.8
co_object <-
lp(const.mat = constraint_matrix,
objective = free_agents_sort$win_shares,
direction = "max",
const.rhs = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 90000000, 150),
const.dir = c("<=", ">=","<=", ">=", "<=", ">=",
"<=", ">=", "<=", ">=", "<=", "<=", "<="),
int.vec = 1:24, all.bin = TRUE)
Browse the complete lp() call for the NBA roster model, including constraints, optimization direction, integer variables, and binary selection. section 4.8
