Tối ưu không ràng buộc

Nhập môn Tối ưu hóa với Python

Jasmin Ludolf

Content Developer

Tối ưu không ràng buộc là gì?

 

  • Tìm cực đại/cực tiểu của hàm không có ràng buộc với biến đầu vào

 

  • SciPy

Đồ thị xu hướng dương với mũi tên hướng lên

Nhập môn Tối ưu hóa với Python

Tối ưu đơn biến không ràng buộc

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

Đồ thị của 2 * x**2 - 3 * x + 1

Nhập môn Tối ưu hóa với Python

Kết quả tối ưu đơn biến không ràng buộc

     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: giá trị hàm tại điểm cực tiểu
  • message: trạng thái
  • nfev: số lần thuật toán đánh giá hàm
  • nit: số vòng lặp để hội tụ
  • success: boolean cho biết có tìm được optimum không
  • x: giá trị tối ưu
Nhập môn Tối ưu hóa với Python

Tìm điểm cực đại

Bài toán cực đại được vẽ đồ thị.

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
Nhập môn Tối ưu hóa với Python

Tối ưu đa biến không ràng buộc

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] là x, a[1] là y
Nhập môn Tối ưu hóa với Python

Kết quả tối ưu đa biến không ràng buộc

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.
Nhập môn Tối ưu hóa với Python

Ayo berlatih!

Nhập môn Tối ưu hóa với Python

Preparing Video For Download...