非制約最適化

Pythonで学ぶOptimization入門

Jasmin Ludolf

Content Developer

非制約最適化とは?

 

  • 入力変数に制約がない関数の最大値・最小値を求めること

 

  • SciPy

右上がりの傾向と上向き矢印のグラフ

Pythonで学ぶ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

2 * x**2 - 3 * x + 1 のグラフ

Pythonで学ぶOptimization入門

単変量の非制約最適化:結果

     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: 最小値における関数値
  • message: ステータス
  • nfev: 関数を評価した回数
  • nit: 収束までの反復回数
  • success: 最適解が見つかったかの真偽値
  • x: 最適な値
Pythonで学ぶOptimization入門

最大値の求め方

最大化問題のグラフ。

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
Pythonで学ぶ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]xa[1]y を表す
Pythonで学ぶOptimization入門

多変量の非制約最適化:結果

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.
Pythonで学ぶOptimization入門

Ayo berlatih!

Pythonで学ぶOptimization入門

Preparing Video For Download...