or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

constants-enums.mdcore-modeling.mdfile-io.mdindex.mdmathematical-operations.mdsolver-interfaces.mdutility-functions.md

index.mddocs/

0

# PuLP

1

2

A comprehensive Python library for linear and mixed integer programming that provides an intuitive interface for creating, manipulating, and solving mathematical optimization problems. PuLP acts as a universal interface to multiple optimization solvers and can export problems to standard formats, making it highly reusable for operations research applications, resource allocation, scheduling problems, and mathematical optimization across various domains.

3

4

## Package Information

5

6

- **Package Name**: pulp

7

- **Language**: Python

8

- **Installation**: `pip install pulp`

9

- **Requires**: Python 3.9+

10

11

## Core Imports

12

13

```python

14

import pulp

15

```

16

17

Common for modeling problems:

18

19

```python

20

from pulp import *

21

```

22

23

This imports all PuLP classes, functions, constants, and solver interfaces.

24

25

## Basic Usage

26

27

```python

28

from pulp import *

29

30

# Create variables

31

x = LpVariable("x", 0, 3) # 0 <= x <= 3

32

y = LpVariable("y", cat="Binary") # Binary variable (0 or 1)

33

34

# Create problem

35

prob = LpProblem("MyProblem", LpMinimize)

36

37

# Add constraints

38

prob += x + y <= 2

39

40

# Set objective function

41

prob += -4*x + y

42

43

# Solve with default solver

44

status = prob.solve()

45

46

# Check solution

47

print(f"Status: {LpStatus[status]}")

48

print(f"x = {value(x)}")

49

print(f"y = {value(y)}")

50

print(f"Objective = {value(prob.objective)}")

51

```

52

53

## Architecture

54

55

PuLP's modular architecture enables flexible mathematical optimization modeling:

56

57

- **Core Modeling**: LpProblem containers manage variables, constraints, and objectives

58

- **Variables and Expressions**: LpVariable and LpAffineExpression handle decision variables and linear combinations

59

- **Constraint System**: LpConstraint manages mathematical relationships with various sense operators

60

- **Solver Interface**: Universal interface to 25+ optimization solvers including open-source (CBC, GLPK, HiGHS) and commercial (CPLEX, Gurobi, Xpress) options

61

- **File I/O**: Export capabilities to standard formats (MPS, LP files) for solver interoperability

62

63

This design allows PuLP to serve as the foundation for mathematical optimization in Python, providing solver-agnostic modeling with automatic solver selection and robust handling of both linear programming (LP) and mixed integer linear programming (MILP) problems.

64

65

## Capabilities

66

67

### Core Modeling Classes

68

69

Fundamental classes for creating and manipulating optimization problems including problem containers, decision variables, constraints, and linear expressions.

70

71

```python { .api }

72

class LpProblem:

73

def __init__(self, name="NoName", sense=LpMinimize): ...

74

def solve(self, solver=None, **kwargs): ...

75

def writeLP(self, filename): ...

76

def writeMPS(self, filename): ...

77

78

class LpVariable:

79

def __init__(self, name, lowBound=None, upBound=None, cat=LpContinuous, e=None): ...

80

@classmethod

81

def dicts(cls, name, indices, lowBound=None, upBound=None, cat=LpContinuous): ...

82

def bounds(self, low, up): ...

83

def value(self): ...

84

85

class LpConstraint:

86

def __init__(self, e=None, sense=LpConstraintEQ, name=None, rhs=None): ...

87

def changeRHS(self, RHS): ...

88

89

class LpAffineExpression:

90

def __init__(self, e=None, constant=0.0, name=None): ...

91

def addterm(self, var, coeff): ...

92

def value(self): ...

93

```

94

95

[Core Modeling](./core-modeling.md)

96

97

### Mathematical Operations

98

99

Essential functions for building linear expressions and extracting solution values from optimization problems.

100

101

```python { .api }

102

def lpSum(vector): ...

103

def lpDot(v1, v2): ...

104

def value(x): ...

105

```

106

107

[Mathematical Operations](./mathematical-operations.md)

108

109

### Solver Interfaces

110

111

Comprehensive collection of solver interfaces supporting open-source and commercial optimization solvers with automatic detection and configuration.

112

113

```python { .api }

114

class PULP_CBC_CMD: ... # Default CBC solver

115

class GLPK_CMD: ... # GLPK command-line

116

class HiGHS_CMD: ... # HiGHS solver

117

class CPLEX_CMD: ... # CPLEX command-line

118

class GUROBI: ... # Gurobi Python interface

119

class XPRESS: ... # Xpress solver

120

121

def getSolver(solver, *args, **kwargs): ...

122

def listSolvers(onlyAvailable=False): ...

123

```

124

125

[Solver Interfaces](./solver-interfaces.md)

126

127

### Constants and Enums

128

129

Essential constants for variable categories, problem senses, constraint types, and status codes used throughout PuLP's modeling system.

130

131

```python { .api }

132

# Variable categories

133

LpContinuous = "Continuous"

134

LpInteger = "Integer"

135

LpBinary = "Binary"

136

137

# Problem senses

138

LpMinimize = 1

139

LpMaximize = -1

140

141

# Status constants

142

LpStatusOptimal = 1

143

LpStatusInfeasible = -1

144

LpStatusUnbounded = -2

145

146

# Constraint senses

147

LpConstraintLE = -1 # <=

148

LpConstraintEQ = 0 # =

149

LpConstraintGE = 1 # >=

150

```

151

152

[Constants and Enums](./constants-enums.md)

153

154

### Utility Functions

155

156

Supporting functions for combinatorial operations, data structure manipulation, and system utilities that enhance PuLP's modeling capabilities.

157

158

```python { .api }

159

def makeDict(headers, array, default=None): ...

160

def allcombinations(orgset, k): ...

161

def allpermutations(orgset, k): ...

162

def read_table(data, coerce_type, transpose=False): ...

163

def splitDict(data): ...

164

```

165

166

[Utility Functions](./utility-functions.md)

167

168

### File I/O and Data Formats

169

170

Functions and classes for reading and writing optimization problems in standard formats (MPS, LP) and handling structured problem data.

171

172

```python { .api }

173

def readMPS(path, sense, dropConsNames=False): ...

174

def writeLP(prob, filename): ...

175

def writeMPS(prob, filename): ...

176

177

class Matrix: ... # Sparse matrix support

178

```

179

180

[File I/O](./file-io.md)

181

182

### Testing

183

184

Built-in testing functionality for validating PuLP installation and solver availability.

185

186

```python { .api }

187

def pulpTestAll(): ... # Run comprehensive PuLP test suite

188

```

189

190

## Types

191

192

```python { .api }

193

# Core exception classes

194

class PulpError(Exception):

195

"""

196

Base exception class for PuLP-related errors.

197

Raised for general PuLP operational errors including modeling issues,

198

data validation problems, and internal inconsistencies.

199

"""

200

201

class PulpSolverError(Exception):

202

"""

203

Exception class for solver-specific errors.

204

Raised when solver communication fails, solver returns unexpected results,

205

or solver configuration issues occur.

206

"""

207

208

# Status dictionaries

209

LpStatus: dict # Status code to string mappings

210

LpSolution: dict # Solution status mappings

211

LpConstraintSenses: dict # Constraint sense mappings

212

```