Bound-constrained optimization

Introduction to Optimization in Python

Jasmin Ludolf

Content Developer

Bound constrained optimization

 

  • Default solver: Broyden-Fletcher-Goldfarb-Shanno (BFGS)
  • The constraint is a range
  • Produce between 50 and 100 units per day

 

from scipy.optimize import Bounds

bounds = Bounds(50, 100)
Introduction to Optimization in Python

L-BFGS-B

from scipy.optimize import minimize, Bounds

def objective_function(b):
  return (2*b[0] - 1.5*b[1])

bounds = Bounds([5, 10], [25, 30])
x0 = [10, 5] result = minimize(objective_function, x0, method='L-BFGS-B', bounds=bounds) print(result.x)
[ 5. 30.]
Introduction to Optimization in Python

Linear constrained optimization

Objective function OR

  • Linear
  • $p = 2x + 1.5y$

Linear graph of the objective function

Constraint

  • Linear
  • $3x + 2y <= 100$

Linear graphs of the constrains

Introduction to Optimization in Python

Hard inequality

  • A condition
  • No more biscuits once flour is used!

Bag of flour

Introduction to Optimization in Python

Solve a linear constrained problem

from scipy.optimize import minimize


def objective_function(b): return (b[0]) ** 2 + (b[1]) ** 0.5
def constraint_function(x): return 2*x[0] + 3*x[1] - 6
constraint = {'type': 'ineq', 'fun': constraint_function}
x0 = [20, 20]
result = minimize(objective_function, x0, constraints=[constraint])
print(result)
     fun: 1.4000405652399073
     jac: array([0.24057153, 0.36086182])
 message: 'Optimization terminated successfully'
    nfev: 25
     nit: 8
    njev: 8
  status: 0
 success: True
       x: array([0.12028576, 1.9198095 ])
  • Types of contraints:
    • 'eq': equality
    • 'ineq': inequality
Introduction to Optimization in Python

Let's practice!

Introduction to Optimization in Python

Preparing Video For Download...