import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import fmin_slsqp
"""
- 직접 그림을 그려보지는 않았고, 그냥 참고한 포스트에 있는 내용을 그대로 사용함.
- equality constraint, inequality constraint 상관없이, 모두 넘기면 되는데, 단 ineqaulity는 항상 positive하도록 넘겨야 함
"""
def obj_fun(x):
return x[0] ** 2 + x[1] ** 2
def eq_const1(x):
return x[0] + x[1] - 1
def ieq_const1(x): # returned value should be positive constraint >=0
return np.atleast_1d(1 - np.sum(np.abs(x)))
x_opt = fmin_slsqp(obj_fun, np.array([0, 0]),
eqcons=[eq_const1],
ieqcons=[ieq_constraint])
print("-----")
print(x_opt)
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.5
Iterations: 3
Function evaluations: 13
Gradient evaluations: 3
-----
[ 0.5 0.5]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import fmin_slsqp
def obj_fun(x):
return x[0] ** 2 + x[1] ** 2
def eq_const1(x):
return x[0] + x[1] - 1
def eq_const2(x):
return x[0] + x[1] - 2
def ieq_const1(x): # returned value should be positive constraint >=0
return x[0]
x_opt = fmin_slsqp(obj_fun, np.array([0, 0]),
eqcons=[eq_const1, eq_const2], ieqcons=[ieq_const1])
print("-----")
print(x_opt)
Inequality constraints incompatible (Exit mode 4)
Current function value: 0.0
Iterations: 1
Function evaluations: 4
Gradient evaluations: 1
-----
[ 0. 0.]
댓글남기기