Python 0-1ナップサック問題を解く
# -*- coding: utf-8 -*-
from pulp import LpProblem, LpVariable, LpMaximize, LpStatus,lpDot
cost = [700,500,400,800,600,200,300]
profit = [2700,1650,1200,2600,1800,1000,1200]
upCost = 2000
r = range(len(cost))
problem = LpProblem('sample',LpMaximize)
x = [LpVariable('x%d'%i, cat='Binary') for i in r]
#lpDot:内積
problem += (lpDot(profit, x), 'Objective function')
problem += (lpDot(cost, x) <= upCost, 'Constraint')
print(problem)
result = problem.solve()
print(LpStatus[result])
print('objective function value', problem.objective.value())
print([x[i].value() for i in r])