Transforming non-linear problems

Introduction to Optimization in Python

Jasmin Ludolf

Content Developer

Maximize a non-linear function

  • Artist produces up to 16 paintings at a cost $C(q) = \sqrt q$ where $q$ is the quantity
  • Inverse demand is $p=\frac{3}{\sqrt q}$ where $p$ is the price
  • $\max \Pi = pq - C=\frac{3}{\sqrt q}q-\sqrt q = 2\sqrt q$

$$\max 2\sqrt q$$

$$s.t. \ \ \ \ q\leq 16 $$

Woman painting on a canvas

Introduction to Optimization in Python

SciPy or PuLP?

  • SciPy: milp expects a linear objective
  • PuLP:
model = LpProblem('Artist', LpMaximize)
q = LpVariable('q', lowBound=0, upBound=16)
model += 2 * q**(1/2)
--> 3 model += 2 * q**(1/2)

TypeError: unsupported operand type(s) for ** or pow(): 'LpVariable' and 'float'
Introduction to Optimization in Python

Substitute to linearize

  • Substitute $z = \sqrt q$
  • $\rightarrow$ $\Pi=2\sqrt q =2z$
  • $\rightarrow$ capacity constraint $z^2\leq 16\Leftrightarrow z\leq 4$
model = LpProblem('Artist', LpMaximize)
z = LpVariable('z', lowBound=0, upBound=4, cat='Integer')
model += 2 * z

model.solve() print(f"Solution is {LpStatus[model.status]}.") print(f"The optimal number of paintings is {round(z.varValue**2)}.")
Solution is Optimal. 
The optimal number of paintings is 16.
Introduction to Optimization in Python

Capital budgeting with dependent projects

Problem statement

  • Projects $A$, $B$, $C$; $A$ is a prerequisite for $B$
  • Profits are respectively $V$ = [250, 200, 300]
  • Required investment is $I$ = [2000, 1900, 2500] and only $4600 is available

Modeling

  • $o_A$, $o_B$, $o_C$ binary variables; indicate project selection

$\max\ \ o_AV_A + o_Ao_BV_B + o_CV_C$

$s.t.\ \ \ \ o_AI_A + o_Ao_BI_B + o_CI_C\leq 4600$

Budget management icon

Introduction to Optimization in Python

Linearization: product of binaries

  • Replace $o_Ao_B=o_{AB}$
  • Add constraints
    • $o_{AB}\leq o_{A}$
    • $o_{AB}\leq o_{B}$
    • $o_{AB}\geq o_{A} + o_{B} -1$
  • Problem reduces to

$$\max\ \ o_AV_A + o_{AB}V_B + o_CV_C$$

$$s.t.\ \ \ \ o_AI_A + o_{AB}I_B + o_CI_C\leq 4600$$

$$ o_{A} + o_{B} -1 \leq o_{AB}\leq o_{A}, o_{B}$$

Introduction to Optimization in Python

Resource allocation with training cost

  • Allocate 120 tasks to minimize cost
  • Senior ($S$), junior ($J$) and intern ($I$)
  • Intern training cost $500
  • Cost to resolve a task $c$ = [30, 40, 5]
  • Vector $x$: optimally assigned tasks
  • Binary $o$: whether intern gets training
  • $TC = 30x_S+40x_J+(5x_I+500)o$

Person with pencil stands next to large to do list

Introduction to Optimization in Python

Linearization: product of binary and continuous

  • BigM method introduces a large number M
  • Replaces the product with $z = (5x_I+500)o$
  • Imposes
    • $-oM\leq z \leq oM$
    • $-(1-o)M \leq z- (5x_I+500)o \leq (1-o)M$
Introduction to Optimization in Python

Let's practice!

Introduction to Optimization in Python

Preparing Video For Download...