AttributeError: 'generator' object has no attribute 'GetCoeffs'

1,654 views
Skip to first unread message

mlgolden07

unread,
Jan 16, 2017, 8:50:11 PM1/16/17
to or-tools-discuss
I am getting this error:

<-- My error -->

Traceback (most recent call last):

  File "my_projects/MyKnapsack.py", line 40, in <module>

    main()

  File "my_projects/MyKnapsac.py", line 27, in main

    solver.Maximize(solver.Sum(values[i] * x[i]) for i in range(num_items))

  File "/Library/Python/2.7/site-packages/ortools-5.0.3919-py2.7-macosx-10.6-intel.egg/ortools/linear_solver/pywraplp.py", line 326, in Maximize

    coeffs = expr.GetCoeffs()

AttributeError: 'generator' object has no attribute 'GetCoeffs'

make: *** [rpy] Error 1

<-- End Error-->

I have no clue what is going on. I am new to OR tools and python.


I am trying to build a simple program to solve Integer programming (decision variable is either 0 or 1).


Please let me know what went wrong with my code (this is a simple knapsack problem). I appreciate in advance:



from __future__ import print_function

from ortools.linear_solver import pywraplp


def main():

  # Instantiate a mixed-integer solver, naming it SolveIntegerProblem.

  solver = pywraplp.Solver('SolveIntegerProblem',

                           pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)

  

  #value data

  values = [100,20,50,25,50,75]

  weight = [200,70,20,25,30,150]

  maxweight = 350


  #create variables

  x = {}

  num_items = len(values)

  

  for i in range(num_items):

    x[i] = solver.IntVar(0,1, 'x[%i]' % (i))


  #constraints

  solver.Add(solver.Sum([weight[i] * x[i] for i in range(num_items)]) <= maxweight)


  #objective

  solver.Maximize(solver.Sum(values[i] * x[i]) for i in range(num_items))


  sol = solver.Solve()


  print('Total Cost = ', solver.Objective().value())

  print()

  for i in range(num_items):

    if x[i].solution_value() > 0:

      print('Weight', i, ' Cost = ', values[i])

  print()

  print("Time = ", solver.WallTime(), "milliseconds")


if __name__ == '__main__':

  main()


Driss Lahlou

unread,
Jan 17, 2017, 6:14:48 AM1/17/17
to or-tools-discuss
Hello,

I was looking at linear_programming.py and figured out that you didn't set the coefficients correctly in your example.

from __future__ import print_function
from ortools.linear_solver import pywraplp

def main():

# Instantiate a mixed-integer solver, naming it SolveIntegerProblem.
solver = pywraplp.Solver('SolveIntegerProblem',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
  #value data
values = [100,20,50,25,50,75]
weight = [200,70,20,25,30,150]
maxweight = 350
#create variables

x = {}

num_items = len(values)
for i in range(num_items):
x[i] = solver.IntVar(0,1, 'x[%i]' % (i))

#constraints
c = solver.Constraint(-solver.infinity(), maxweight, 'c')
for i in range(num_items):
c.SetCoefficient(x[i], weight[i])
#solver.Add(solver.Sum([weight[i] * x[i] for i in range(num_items)]) <= maxweight)

#objective
objective = solver.Objective()
objective.SetCoefficient(x[i], values[i])
objective.SetMaximization()
#solver.Maximize(solver.Sum(values[i] * x[i]) for i in range(num_items))

sol = solver.Solve()

print('Total Cost = ', solver.Objective().Value())
print()
for i in range(num_items):
if x[i].solution_value() > 0:
print('Weight', i, ' Cost = ', values[i])
print()
print("Time = ", solver.WallTime(), "milliseconds")

if __name__ == '__main__':
main()

I hope this helps.

Driss

mlgolden07

unread,
Jan 17, 2017, 6:41:51 PM1/17/17
to or-tools-discuss
Thanks Driss,
It doesn't give me an error and found a solution. However, the solver only picks an item that is the heaviest, instead of selecting multiple items to reach the maxweight of 350.

That is why in my constraint I tried to sum all the weight to be less than or equal to maxweight.

I followed the MIP example from this page: Comparing MIP and CP, but I don't know how to change my syntax to solve my problem.

mlgolden07

unread,
Jan 17, 2017, 8:06:44 PM1/17/17
to or-tools-discuss
I have figured out. It is actually the objective functions that needs to be changed.

From 
  solver.Maximize(solver.Sum(values[i] * x[i]) for i in range(num_items))

To
  solver.Maximize(solver.Sum([values[i] * x[i] for i in range(num_items)]))

it is the location of the closing bracket ']' that was incorrect.



On Monday, January 16, 2017 at 8:50:11 PM UTC-5, mlgolden07 wrote:
Reply all
Reply to author
Forward
0 new messages