AttributeError: 'ortools.algorithms.python.knapsack_solver.KnapsackSolver'没有属性'KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
我正在尝试使用ortools
包里的knapsack
算法,但遇到了一些问题。这里是我用来测试这个算法的脚本,我会把完整的脚本都给你。根据我在GitHub上找到的信息,大部分的包使用方法都是基于之前的版本,我找不到合适的、能正常工作的用法。
# A Dynamic Programming based Python Program for 0-1 Knapsack problem
# Returns the maximum value that can be put in a knapsack of capacity W
import numpy as np
from ortools.algorithms.python import knapsack_solver
def knapsack(W, wt, val, n):
K = [[0 for x in range(W+1)] for x in range(n+1)]
# Build table K[][] in bottom up manner
for i in range(n+1):
for w in range(W+1):
if i==0 or w==0:
K[i][w] = 0
elif wt[i-1] <= w:
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w])
else:
K[i][w] = K[i-1][w]
best = K[n][W]
amount = np.zeros(n)
a = best
j = n
Y = W
# j = j + 1;
#
# amount(j) = 1;
# Y = Y - weights(j);
# j = j - 1;
# a = A(j + 1, Y + 1);
while a > 0:
while K[j][Y] == a:
j = j - 1
j = j + 1
amount[j-1] = 1
Y = Y - wt[j-1]
j = j - 1
a = K[j][Y]
return amount
def test_knapsack():
weights = [1 ,1 ,1, 1 ,2 ,2 ,3]
values = [1 ,1 ,2 ,3, 1, 3 ,5]
best = 13
print(knapsack(7, weights, values, 7))
#===========================================
'''
------------------------------------------------
Use dynamic programming (DP) to solve 0/1 knapsack problem
Time complexity: O(nW), where n is number of items and W is capacity
Author: Kaiyang Zhou
Website: https://kaiyangzhou.github.io/
------------------------------------------------
knapsack_dp(values,weights,n_items,capacity,return_all=False)
Input arguments:
1. values: a list of numbers in either int or float, specifying the values of items
2. weights: a list of int numbers specifying weights of items
3. n_items: an int number indicating number of items
4. capacity: an int number indicating the knapsack capacity
5. return_all: whether return all info, defaulty is False (optional)
Return:
1. picks: a list of numbers storing the positions of selected items
2. max_val: maximum value (optional)
------------------------------------------------
'''
def knapsack_dp(values,weights,n_items,capacity,return_all=False):
check_inputs(values,weights,n_items,capacity)
table = np.zeros((n_items+1,capacity+1),dtype=np.float32)
keep = np.zeros((n_items+1,capacity+1),dtype=np.float32)
for i in range(1,n_items+1):
for w in range(0,capacity+1):
wi = weights[i-1] # weight of current item
vi = values[i-1] # value of current item
if (wi <= w) and (vi + table[i-1,w-wi] > table[i-1,w]):
table[i,w] = vi + table[i-1,w-wi]
keep[i,w] = 1
else:
table[i,w] = table[i-1,w]
picks = []
K = capacity
for i in range(n_items,0,-1):
if keep[i,K] == 1:
picks.append(i)
K -= weights[i-1]
picks.sort()
picks = [x-1 for x in picks] # change to 0-index
if return_all:
max_val = table[n_items,capacity]
return picks,max_val
return picks
def check_inputs(values,weights,n_items,capacity):
# check variable type
assert(isinstance(values,list))
assert(isinstance(weights,list))
assert(isinstance(n_items,int))
assert(isinstance(capacity,int))
# check value type
assert(all(isinstance(val,int) or isinstance(val,float) for val in values))
assert(all(isinstance(val,int) for val in weights))
# check validity of value
assert(all(val >= 0 for val in weights))
assert(n_items > 0)
assert(capacity > 0)
def test_knapsack_dp():
values = [2,3,4]
weights = [1,2,3]
n_items = 3
capacity = 3
picks = knapsack_dp(values,weights,n_items,capacity)
print (picks)
osolver = knapsack_solver.KnapsackSolver(
# knapsack_solver.KnapsackSolver.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
knapsack_solver.KnapsackSolver.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER,
'test')
def knapsack_ortools(values, weights, items, capacity ):
scale = 1000
values = np.array(values)
weights = np.array(weights)
values = (values * scale).astype(np.int)
weights = (weights).astype(np.int)
capacity = capacity
osolver.Init(values.tolist(), [weights.tolist()], [capacity])
computed_value = osolver.Solve()
packed_items = [x for x in range(0, len(weights))
if osolver.BestSolutionContains(x)]
return packed_items
if __name__ == "__main__":
test_knapsack_dp()
test_knapsack()
这是我遇到的详细错误信息
File "knapsack.py", line 135, in <module>
knapsack_solver.KnapsackSolver.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'ortools.algorithms.python.knapsack_solver.KnapsackSolver' has no attribute 'KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER'
我使用的环境是:
Python 3.12.2
ortools 9.8.3296
numpy 1.26.4
1 个回答
2
当我们从swig切换到pybind11时,我们需要引入类枚举。
knapsack_solver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER
请查看 这个链接