Unconstrained optimization

Introduction to Optimization in Python

Jasmin Ludolf

Content Developer

What is unconstrained optimization?

 

  • Finding the maxima or minima of a function that does not have any constraints on the input variables

 

  • SciPy

Graph with a positive trend and an arrow pointing up

Introduction to Optimization in Python

Univariate unconstrained optimization

from scipy.optimize import minimize_scalar


def objective_function(x): return x**2 - 12*x + 4
result = minimize_scalar(objective_function) print(result)
     fun: -32.0
 message: 'Optimization terminated successfully; The
 returned value satisfies the termination criteria 
 (using xtol = 1.48e-08 )'
    nfev: 10
     nit: 4
 success: True
       x: 6.000000000000001

Graph of 2 * x**2 - 3 * x + 1

Introduction to Optimization in Python

Univariate unconstrained optimization result

     fun: -32.0
 message: 'Optimization terminated successfully; The
 returned value satisfies the termination criteria 
 (using xtol = 1.48e-08 )'
    nfev: 10
     nit: 4
 success: True
       x: 6.000000000000001
result.x
6.000000000000001
  • fun: value of the function at the minimum
  • message: status
  • nfev: number of times the algorithm evaluated the function
  • nit: number of iterations it took to reach the solution
  • success: boolean on whether an optimum was found
  • x: the optimal value
Introduction to Optimization in Python

Finding the maxima

Maximization problem graphed.

def objective_function(x): 
  return 40 * q - 0.5 * q**2
-1*(40 * q - 0.5 * q**2)
-40 * q + 0.5 * q**2
def negated_function(x): 
  return -40 * q + 0.5 * q**2
result = minimize_scalar(negated_function) 
print(f"The maximum is {result.x:.2f} in two 
      decimals")
The maximum is 40.00 in two decimals
Introduction to Optimization in Python

Multivariate unconstrained optimization

from scipy.optimize import minimize


def objective_function(a): return (a[0] - 2)**2 + (a[1] - 3)**2 + 1.5
x0 = [1, 2] result = minimize(objective_function, x0)
  • a[0] represents x and a[1] represents y
Introduction to Optimization in Python

Multivariate unconstrained optimization result

print(result)
print(f"minimum is (x, y) = ({result.x[0]:.2f}, {result.x[1]:.2f}) in two decimals.")
      fun: 1.5000000000000002

hess_inv: array([[ 0.75, -0.25], [-0.25, 0.75]])
jac: array([0., 0.])
message: 'Optimization terminated successfully.' nfev: 9 nit: 2 njev: 3
status: 0
success: True x: array([1.99999999, 2.99999999]) minimum is (x, y) = (2.00, 3.00) in two decimals.
Introduction to Optimization in Python

Let's practice!

Introduction to Optimization in Python

Preparing Video For Download...