此程序:

from __future__ import division
import numpy as np
from scipy.optimize import minimize
from collections import deque

def buy_captial():
    """ buys the profit maximizing amount of captial """
    offers = {'W': [{'quantity':50, 'price': 1}],
              'K': [{'quantity':200, 'price': 0.5}]}
    key_order = ('W', 'K')
    exponents = {'W': 0.6, 'K': 0.4}

    prices = {}
    quantities = {}
    for key in key_order:
        if key in offers:
            prices[key] = deque([offers[key][i]['price'] for i in range(len(offers[key]))])
            quantities[key] = deque([offers[key][i]['quantity'] for i in range(len(offers[key]))])

    print quantities
    budget = [{'type': 'ineq', 'fun': lambda x:
                                        200
                                        - sum([x[i] * prices[key_order[i]][0] for i in rng])}]
    rng = range(len(key_order))
    x0 = np.zeros(len(key_order))
    bounds =  ((0, 50), (0, 200))

    def objective(x):
        return \
            - 5  * np.prod([(x[i]) ** exponents[key_order[i]] for i in rng]) \
            + sum([x[i] * prices[key_order[i]][0] for i in rng])


    res = minimize(objective, x0=x0, method='SLSQP', constraints=budget, bounds=bounds)
    print 'buy', res,

buy_captial()

导致此错误:
python test_buy_captial.py
{'K': deque([200]), 'W': deque([50])}
Traceback (most recent call last):
  File "test_buy_captial.py", line 37, in <module>
    buy_captial()
  File "test_buy_captial.py", line 34, in buy_captial
    res = minimize(objective, x0=x0, method='SLSQP', constraints=budget, bounds=bounds)
  File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/_minimize.py", line 358, in minimize
    constraints, **options)
  File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/slsqp.py", line 333, in _minimize_slsqp
    xl[infbnd[:, 0]] = -1.0E12
OverflowError: Python int too large to convert to C long

最佳答案

问题是边界被指定为整数。Fortran主干网无法转换这一点。
解决方案是将边界指定为float。或者是'float()'参数,或者是@pv指出的速度更好的参数。通过bounds = array([[0, 50], [0, 200]], dtype=float)

关于python - scipy最小化slsqp,导致OverflowError:Python int太大,无法转换为C long,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13461296/

10-11 19:37