최적화된 제한

0

질문

하고 싶을 해결 제한 최적화 문제입니다.

max{ln(c1)+ln(c2)}

s.t. 4(c1)+6(c2)≤40

이 코드:

import numpy as np
from scipy import optimize

def main():
    """
    solving a regular constrained optimization problem    
    max ln(cons[0]) + ln(cons[1]) 
    st. prices[0]*cons[0] + prices[1]*cons[1] <= I         
    """    
   
    prices = np.array([4.0, 6.0])
    I = 40.0
   
    util = lambda cons: np.dot( np.log(cons))  #define utility function
    budget = lambda cons: I - np.dot(prices, cons)   #define the budget constraint
    
    initval = 40.0*np.ones(2)    #set the initial guess for the algorithm
    
    res = optimize.minimize(lambda x: -util(x), initval, method='slsqp', 
                            constraints={'type':'ineq', 'fun':budget}, 
                            tol=1e-9)
    assert res['success'] == True
           
    
    print(res)

불행하게도,나의 코드를 인쇄하지 않는 모든 솔루션입니다. 당신이 나를 도울 수 있습니 그 이유?

mathematical-optimization python scipy
2021-11-17 08:52:35
1

최고의 응답

1

귀하의 코드를 생성 TypeError 이후 np.dot 예수의 정의를 참조하십시오 h utils 기능입니다. 따라서,사용

# is the same as np.dot(np.ones(2), np.log(cons))
utils = lambda cons: np.sum(np.log(cons))

대신 합니다.

2021-11-17 21:09:35

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................