PuLP is a linear and mixed integer programming modeler that provides an intuitive Python interface for creating, manipulating, and solving mathematical optimization problems.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Essential functions for building linear expressions and extracting solution values from optimization problems. These operations form the mathematical foundation for PuLP's modeling capabilities.
Functions for constructing complex linear expressions from lists of variables and coefficients, enabling efficient formulation of optimization problems.
def lpSum(vector):
"""
Calculate the sum of a list of linear expressions or variables.
Parameters:
- vector (list): List of LpVariable, LpAffineExpression, or numeric values
Returns:
LpAffineExpression: Sum of all elements in the vector
Examples:
lpSum([x, y, z]) # x + y + z
lpSum([2*x, 3*y, 5]) # 2*x + 3*y + 5
lpSum(vars[i] for i in range(n)) # Sum over generated variables
"""
def lpDot(v1, v2):
"""
Calculate the dot product of two lists of linear expressions.
Parameters:
- v1 (list): First vector of LpVariable, LpAffineExpression, or numeric values
- v2 (list): Second vector of LpVariable, LpAffineExpression, or numeric values
Returns:
LpAffineExpression: Dot product v1[0]*v2[0] + v1[1]*v2[1] + ...
Examples:
lpDot([x, y], [2, 3]) # 2*x + 3*y
lpDot(costs, production) # Cost calculation
"""Usage examples:
# Sum multiple variables
x = [LpVariable(f"x_{i}", 0, 10) for i in range(5)]
total_x = lpSum(x) # x_0 + x_1 + x_2 + x_3 + x_4
# Sum with coefficients
costs = [10, 15, 20]
production = [LpVariable(f"prod_{i}", 0) for i in range(3)]
total_cost = lpSum([costs[i] * production[i] for i in range(3)])
# Dot product for cost calculations
unit_costs = [5.5, 7.2, 3.8, 9.1]
quantities = [LpVariable(f"qty_{i}", 0) for i in range(4)]
total_cost = lpDot(unit_costs, quantities)
# Complex expressions in constraints
prob += lpSum(production) <= 1000 # Total production constraint
prob += lpDot(weights, items) <= max_weight # Weight constraintFunctions for retrieving and validating solution values from solved optimization problems with robust handling of unsolved states.
def value(x):
"""
Get the value of a variable, expression, or number.
Parameters:
- x (LpVariable, LpAffineExpression, or number): Object to evaluate
Returns:
float: Value of x in the current solution, or x itself if it's a number
None: If x is a variable/expression and the problem hasn't been solved
Examples:
value(x) # Get variable value
value(2*x + 3*y) # Get expression value
value(5) # Returns 5 (unchanged)
"""
def valueOrDefault(x):
"""
Get the value of a variable/expression or return a default within bounds.
Parameters:
- x (LpVariable or LpAffineExpression): Object to evaluate
Returns:
float: Value of x, or a default value if not solved
Note: For variables, returns a value within the variable's bounds.
For expressions, returns the constant term or calculated default.
"""
def isNumber(x):
"""
Check if x is a numeric value (int or float).
Parameters:
- x: Object to check
Returns:
bool: True if x is int or float, False otherwise
"""Usage examples:
# After solving a problem
status = prob.solve()
if status == LpStatusOptimal:
# Get individual variable values
x_val = value(x)
y_val = value(y)
# Get expression values
obj_val = value(prob.objective)
constraint_lhs = value(2*x + 3*y)
# Check if values are available
if x_val is not None:
print(f"Optimal x = {x_val}")
# Use in calculations
total_profit = value(lpDot(profit_margins, production_vars))
# Handle mixed types
mixed_expr = 100 + 2*x # Constant + variable
mixed_val = value(mixed_expr) # Handles both parts correctly
# Robust value checking
for var in prob.variables():
var_value = value(var)
if var_value is not None:
print(f"{var.name} = {var_value}")
else:
print(f"{var.name} = Not solved")Mathematical operations are directly supported on PuLP objects through operator overloading, enabling natural mathematical syntax.
# Arithmetic operations (built into LpVariable and LpAffineExpression)
# Addition
result = x + y # Variable + Variable -> LpAffineExpression
result = x + 5 # Variable + Number -> LpAffineExpression
result = expr1 + expr2 # Expression + Expression -> LpAffineExpression
# Subtraction
result = x - y # Variable - Variable -> LpAffineExpression
result = x - 10 # Variable - Number -> LpAffineExpression
# Multiplication (by scalars only - linear programming)
result = 3 * x # Number * Variable -> LpAffineExpression
result = 2.5 * expr # Number * Expression -> LpAffineExpression
# Division (by scalars only)
result = x / 2 # Variable / Number -> LpAffineExpression
# Negation
result = -x # Negate variable -> LpAffineExpression
# Comparison operations (create constraints)
constraint = x <= 10 # Creates LpConstraint with LpConstraintLE sense
constraint = x == 5 # Creates LpConstraint with LpConstraintEQ sense
constraint = x >= 0 # Creates LpConstraint with LpConstraintGE senseAdditional mathematical functions for handling numeric precision and type validation in optimization contexts.
def resource_clock():
"""
Return the current resource usage time for performance monitoring.
Returns:
float: Resource time in seconds
Note: Used internally for solver performance tracking.
"""Usage examples:
# Performance monitoring
start_time = resource_clock()
status = prob.solve()
solve_time = resource_clock() - start_time
print(f"Solve time: {solve_time:.3f} seconds")
# Type checking for robust code
def build_constraint(variables, coefficients):
if all(isNumber(c) for c in coefficients):
return lpDot(variables, coefficients) <= 100
else:
raise ValueError("All coefficients must be numeric")
# Complex mathematical expressions
# PuLP handles operator precedence correctly
complex_expr = 2*x + 3*(y - z) + 5*w/2
prob += complex_expr <= value_limit
# Multi-dimensional problem formulation
facilities = ["F1", "F2", "F3"]
customers = ["C1", "C2", "C3", "C4"]
flow = LpVariable.matrix("flow", facilities, customers, 0)
# Total flow constraint using lpSum
for f in facilities:
prob += lpSum([flow[f][c] for c in customers]) <= capacity[f]
# Demand satisfaction using lpSum
for c in customers:
prob += lpSum([flow[f][c] for f in facilities]) >= demand[c]
# Transportation cost objective using nested lpSum
transport_cost = lpSum([
lpSum([cost[f][c] * flow[f][c] for c in customers])
for f in facilities
])
prob += transport_costInstall with Tessl CLI
npx tessl i tessl/pypi-pulp