OpenOpt KSP未定义

时间:2014-08-27 23:14:31

标签: python knapsack-problem

我正在尝试使用Python中的OpenOpt包来解决一些多重构点的背包问题,但是现在我只是试图从OpenOpt站点运行该示例。

我相信我已经正确安装了所有东西,因为我可以进入Python并导入各个模块,如OpenOpt,FuncDesigner等等。

当我尝试运行KSP示例时,我收到一条错误消息,指出KSP未定义但正在导入OpenOpt模块。

#!/usr/bin/python
from openopt import *
from numpy import sin, cos
N = 150
items = [
    {
        'name': 'item %d' % i, # pay attention that Python indexation starts from zero
        'cost': 1.5*(cos(i)+1)**2,
        'volume': 2*sin(i) + 3,
        'mass': 4*cos(i)+5,
        'n':  1 if i < N/3 else 2 if i < 2*N/3 else 3 # number of elements
    }
    for i in range(N) # i = 0, ... , N-1
    ]

constraints = lambda values: (
                         values['volume'] < 10,
                         values['mass'] < 100,
                         values['nItems'] <= 10,
                         values['nItems'] >= 5
                         # we could use lambda-func, e,g.
                         # values['mass'] + 4*values['volume'] < 100
                         )
objective = 'cost'
p = KSP(objective, items, goal = 'max', constraints = constraints)
r = p.solve('glpk', iprint = 0)
print(r.xf)

确切错误:

File "test_knap.py", line 25, in <module>
p = KSP(objective, items, goal = 'max', constraints = constraints)
NameError: name 'KSP' is not defined

0 个答案:

没有答案