Logical constraints

Supply Chain Analytics in Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

Example problem

Maximum Weight 20,000 lbs

Product Weight (lbs) Profitability ($US)
A 12,800 77,878
B 10,900 82,713
C 11,400 82,728
D 2,100 68,423
E 11,300 84,119
F 2,300 77,765
  • Select most profitable product to ship without exceeding weight limit
  • Decision Variables:
    • X$_{i}$ = 1 if product _i_ is selected else 0
  • Objective:
    • Maximize z = $\sum$ Profitability$_{i}$X$_{i}$
  • Constraint:
    • $\sum$ Weight$_{i}$X$_{i}$ < 20,0000
Supply Chain Analytics in Python
prod = ['A', 'B', 'C', 'D', 'E', 'F']
weight = {'A':12800, 'B':10900, 'C':11400, 'D':2100, 'E':11300, 'F':2300}
prof = {'A':77878, 'B':82713, 'C':82728, 'D':68423, 'E':84119, 'F':77765}

# Initialize Class model = LpProblem("Loading Truck Problem", LpMaximize) # Define Decision Variables x = LpVariable.dicts('ship_', prod, cat='Binary')
# Define Objective model += lpSum([prof[i]*x[i] for i in prod]) # Define Constraint model += lpSum([weight[i]*x[i] for i in prod]) <= 20000
# Solve Model model.solve() for i in prod: print("{} status {}".format(i, x[i].varValue))
Supply Chain Analytics in Python

Example result

Maximum Weight 20,000 lbs

Product Ship or Not
A No
B No
C No
D Yes
E Yes
F Yes

Result

  • Profitability: $230,307
  • Weight of Products: 15,700 lbs
Supply Chain Analytics in Python

Logical constraint example 1

Either product E is selected or product D is selected, but not both.

  • X$_{E}$ = 1 if product _i_ is selected else 0
  • X$_{D}$ = 1 if product _i_ is selected else 0
  • Constraint
    • X$_{E}$ + X$_{D}$ ≤ 1
Supply Chain Analytics in Python

Code example - logical constraint example 1

model += x['E'] + x['D'] <= 1
prod = ['A', 'B', 'C', 'D', 'E', 'F']
weight = {'A':12800, 'B':10900, 'C':11400, 
          'D':2100, 'E':11300, 'F':2300}
prof = {'A':77878, 'B':82713, 'C':82728, 
        'D':68423, 'E':84119, 'F':77765}

# Initialize Class
model = LpProblem("Loading Truck Problem",
                   LpMaximize)

# Define Decision Variables
x = LpVariable.dicts('ship_', prod, 
                      cat='Binary')
# Define Objective
model += lpSum([prof[i]*x[i] for i in prod])

# Define Constraint
model += 
  lpSum([weight[i]*x[i] for i in prod]) <= 20000
model += x['E'] + x['D'] <= 1

# Solve Model
model.solve()
for i in prod:
  print("{} status {}".format(i, x[i].varValue))
Supply Chain Analytics in Python

Logical constraint 1 example result

Maximum Weight 20,000 lbs

Product Ship or Not
A No
B No
C Yes
D Yes
E No
F Yes

Result

  • Profitability: $228,916
  • Weight of Products: 15,800 lbs
Supply Chain Analytics in Python

Logical constraint example 2

If product D is selected then product B must also be selected.

  • X$_{D}$ = 1 if product _i_ is selected else 0
  • X$_{B}$ = 1 if product _i_ is selected else 0
  • Constraint
    • X$_{D}$ ≤ X$_{B}$
Supply Chain Analytics in Python

Code example - logical constraint example 2

model += x['D'] <= x['B']

prod = ['A', 'B', 'C', 'D', 'E', 'F'] weight = {'A':12800, 'B':10900, 'C':11400, 'D':2100, 'E':11300, 'F':2300} prof = {'A':77878, 'B':82713, 'C':82728, 'D':68423, 'E':84119, 'F':77765} # Initialize Class model = LpProblem("Loading Truck Problem", LpMaximize) # Define Decision Variables x = LpVariable.dicts('ship_', prod, cat='Binary')
# Define Objective
model += lpSum([prof[i]*x[i] for i in prod])

# Define Constraint
model += 
 lpSum([weight[i]*x[i] for i in prod]) <= 20000
model += x['D'] <= x['B']

# Solve Model
model.solve()
for i in prod:
  print("{} status {}".format(i, x[i].varValue))
Supply Chain Analytics in Python

Logical constraint 2 example result

Maximum Weight 20,000 lbs

Product Ship or Not
A No
B Yes
C No
D Yes
E No
F Yes

Result

  • Profitability: $228,901
  • Weight of Products: 15,300 lbs
Supply Chain Analytics in Python

Other logical constraints

Logical Constraint Constraint
If item _i_ is selected, then item _j_ is also selected. x$_{i}$ - x$_{j}$ ≤ 0
Either item _i_ is selected or item _j_ is selected, but not both. x$_{i}$ + x$_{j}$ = 1
If item _i_ is selected, then item _j_ is not selected. x$_{i}$ - x$_{j}$ ≤ 1
If item _i_ is not selected, then item _j_ is not selected. -x$_{i}$ + x$_{j}$ ≤ 0
At most one of items _i_, _j_, and _k_ are selected. x$_{i}$ + x$_{j}$ + x$_{k}$ ≤ 1
1 James Orlin, and Ebrahim Nasrabadi. 15.053 Optimization Methods in Management Science. Spring 2013. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Supply Chain Analytics in Python

Summary

  • Reviewed examples of logical constraints
  • Listed a table of other logical constraints
Supply Chain Analytics in Python

Your turn!

Supply Chain Analytics in Python

Preparing Video For Download...