2017-12-30 160 views
2

에러 함수 (f)가 ​​최소가되도록 (즉, 0 또는 0에 가까워 지도록) bo, ho, t1 및 t2와 같은 최적의 매개 변수를 찾고 싶습니다. 전체 코드는Scipy.optimize.minimize 함수로 여러 변수를 결정할 수 있습니다.

import numpy 
import scipy.optimize as optimize 

#OPTIMISATION USING SCIPY 
def Obj_func(x): 
    bo,ho,t1,t2=x 
    f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2))) 
    return f 

initial_guess=[2,2,0.2,0.2] 
cons = ({'type': 'eq', 'fun': lambda bo,ho,t1,t2: (bo*ho - (bo - 2*t2)*(ho - 2*t1)-7.55)}) 
bnds = ((0, None), (0, None),(0, None), (0, None)) #all four variables are positive and greater than zero 
#Always t1 and t2 should always be lesser than bo and ho 
#res=optimize.minimize(Obj_func, method='SLSQP',initial_guess, bounds=bnds,constraints=cons) 
res=optimize.minimize(Obj_func,initial_guess, bounds=bnds,constraints=cons) 
print ("Result",res) 

내가 여기에 몇 가지 문제에 직면하고, 다음과 같습니다 Q1을. 죄수를 위해 생성 된 람다 함수는``TypeError :()에 'ho, t1'및 't2'라는 3 개의 위치 인수가 누락되었습니다. 왜 이런 일이 일어나는거야? 비록 내가 구문을 올바르게 따라 갔지만, 나는 실종되었다. Q2. 나는 목적 함수, 즉 여기서 오류 함수 (f)를 0 또는 0에 최소화시키는 것을 최소화 할 필요가있다. 내가 최적화 방법의 유형을 언급하지 않고 optimize.minimize() 언급했다. 괜찮 니? 그것은 내가 찾고있는 정답을 줄 것인가?

답변

3

Q1 (구문) : 당신의 제약 조건이 하나의 벡터 인수를해야합니다

import numpy 
import scipy.optimize as optimize 

#OPTIMISATION USING SCIPY 
def Obj_func(x): 
    bo,ho,t1,t2=x 
    f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2))) 
    return f 

initial_guess=[2,2,0.2,0.2] 
cons = ({'type': 'eq', 'fun': lambda bhtt: (bhtt[0]*bhtt[1] - (bhtt[0] - 2*bhtt[3])*(bhtt[1] - 2*bhtt[2])-7.55)}) 
bnds = ((0, None), (0, None),(0, None), (0, None)) #all four variables are positive and greater than zero 
#Always t1 and t2 should always be lesser than bo and ho 
#res=optimize.minimize(Obj_func, method='SLSQP',initial_guess, bounds=bnds,constraints=cons) 
res=optimize.minimize(Obj_func,initial_guess, bounds=bnds,constraints=cons) 
print ("Result",res) 

출력 :

Result  fun: -15467.04696553346 
    jac: array([ 165915.125  , 147480.57421875, 1243506.8828125 , 
     -130354.05859375]) 
message: 'Positive directional derivative for linesearch' 
    nfev: 6 
    nit: 5 
    njev: 1 
    status: 8 
success: False 
     x: array([ 2. , 2. , 0.2, 0.2]) 

Q2 (수학은) 솔버는 문제를 같이하지 않는 것 그것은 제기된다. 나는 동등 제약과 하나의 변수를 제거함으로써 솔루션을 뱉어 냈다. 기본 솔버는 여전히 작동하지 않았지만 COBYLA는 않았다

import numpy 
import scipy.optimize as optimize 
from operator import itemgetter 

#OPTIMISATION USING SCIPY 
def Obj_func_orig(x): 
    bo,ho,t1,t2=x 
    f=-321226.4817 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(125 + (10400000*bo*ho**3 - 10400000*(bo - 2*t2)*(ho - 2*t1)**3)/(1563920*t1*(bo - 2*t2))) 
    return f 

def Obj_func(x): 
    bh, h, t = x 
    btht = bh - 7.55 
    D = bh*h*h - btht*(h-t)*(h-t) 
    f = -321226.4817 + D/(125/10400000 + D*(h-t)/(781960*t*btht)) 
    return f 

def cons(x): 
    bh, h, t = x 
    btht = bh - 7.55 
    return (btht * h - bh * (h-t)) * (t-h) 

def ge(i): 
    return {'type': 'ineq', 'fun': itemgetter(i)} 

initial_guess=[1,1,1] 
initial_guess=[64,8,0.5] 
cons = ({'type': 'ineq', 'fun': cons}, {'type': 'ineq', 'fun': Obj_func}, ge(0), ge(1), ge(2)) 
res=optimize.minimize(lambda x: Obj_func(x)**2,initial_guess,constraints=cons,method='COBYLA') 
print ("Result",res) 
bh, h, t = res.x 
bo, ho, t1, t2 = bh/h, h, t/2, (bh/h - (bh - 7.55)/(h-t))/2 
print('bo, ho, t1, t2, f', bo, ho, t1, t2, Obj_func_orig([bo,ho,t1,t2])) 

답변 OP의 초기 추측과 :

# Result  fun: 285138.38958324661 
# maxcv: 0.0 
# message: 'Optimization terminated successfully.' 
#  nfev: 47 
# status: 1 
# success: True 
#  x: array([ 6.40732657e+01, 8.29209901e+00, 6.03312595e-02]) 
# bo, ho, t1, t2, f 7.72702612177 8.29209901408 0.0301656297535 0.430273240999 533.983510591 

는 이러한 초기 추측에 따라 명확하게 지역 최소값 있음을 알려드립니다. 예를 들어 변환 된 변수에 을 사용하는 경우 :

# Result  fun: 5829.8452437661899 
# maxcv: 0.0 
# message: 'Optimization terminated successfully.' 
#  nfev: 48 
# status: 1 
# success: True 
#  x: array([ 4.39911932, 0.93395108, 0.89739524]) 
# bo, ho, t1, t2, f 4.71022456896 0.933951079923 0.44869761948 45.4519279253 76.3534232616 
+0

답장을 보내 주셔서 감사합니다. 여기서 목적 함수는 0 이하, 즉 재미 : -15467.04696553346 이하로 최소화됩니다. 그러나 목적 함수가 0 이하가 아닌 0으로 최소화되는 변수를 얻고 싶습니다. 내가 어떻게 할 수 있니? –

+0

@PaulThomas 이론적으로 목적 함수를 부등식 제약 조건으로 사용할 수 있어야합니다. 그러나 이것을 시도했을 때 해결책을 찾지 못했습니다. 때때로 더 나은 초기 추측이 도움이 될 수 있습니다. 적어도 음수가 아닌 함수 값을 가진 함수를 제공 할 수 있습니까? –

+0

정확히 0이 아니라면 목적 함수 (f) 값을 0에 가깝게 제한 할 수 있습니까? 또는 f (ab) (f)의 절대 값을 취하는 것이 가능하다.이 경우, 최소값은 0 또는 0에 가깝다. 사실 나는 긍정과 부정 모두 가질 수있는 이러한 종류의 기능에 대한 솔루션을 찾고 있습니다. 나는 더 나은 초기 추측이 될 것이라고 생각한다. [8, 8, 0.25, 0.25] –