liveBook · Manning

lpSolve

As explained in Statistics Slam Dunk

lpSolve is an R package that provides functions for solving linear programming and related constrained optimization problems.

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

How Statistics Slam Dunk covers it

How deep it goes
Explains package loading and works through lp() arguments, constraint matrices, binary variables, and interpreted optimization results.
Examples
Demonstrates choosing five NBA free agents while maximizing win shares and limiting positions, salary, total age, and roster size.
What you can do after
Apply it via R's lp() function to optimize a constrained selection problem and inspect the chosen variables and aggregate results.
How it connects
Connects linear programming with constrained optimization, objective functions, constraint matrices, and integer or binary variables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.