Onbeperkte optimalisatie

Introductie tot optimalisatie in Python

Jasmin Ludolf

Content Developer

Wat is onbeperkte optimalisatie?

 

  • Maxima of minima vinden van een functie zonder beperkingen op de invoervariabelen

 

  • SciPy

Grafiek met opwaartse trend en een pijl omhoog

Introductie tot optimalisatie in Python

Eendimensionale onbeperkte optimalisatie

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

Grafiek van 2 * x**2 - 3 * x + 1

Introductie tot optimalisatie in Python

Resultaat: eendimensionale onbeperkte optimalisatie

     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: functiewaarde op het minimum
  • message: status
  • nfev: aantal functie-evaluaties
  • nit: aantal iteraties tot de oplossing
  • success: booleaan of een optimum is gevonden
  • x: de optimale waarde
Introductie tot optimalisatie in Python

Maxima vinden

Maximalisatieprobleem geplot.

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
Introductie tot optimalisatie in Python

Multivariabele onbeperkte optimalisatie

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] staat voor x en a[1] staat voor y
Introductie tot optimalisatie in Python

Resultaat: multivariabele onbeperkte optimalisatie

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.
Introductie tot optimalisatie in Python

Laten we oefenen!

Introductie tot optimalisatie in Python

Preparing Video For Download...