0

I'm trying to implement a problem a basis problem os Set covering with python, but pulp gives me all the time the error: AttributeError: 'LpAffineExpression' object has no attribute 'solve'

This is what I'm trying to solve

enter image description here

And this is my code:

import pulp as lp 

I = ['A','B','C','D'] 
J = ['1','2','3'] 
h = dict(zip(I, [100,400,230,190]))
 
m_dist = [[9,12,3], [5,7,3], [2,5,8], [12,9,6]]
 
d_m_dist = dict(zip(I, [dict(zip(J, m_dist [i])) for i in range(len(I))]))
Dc = 6
 
modelo = lp.LpProblem ('Cobertura', lp.LpMinimize)
 
x = lp.LpVariable.dicts('Se utiliza la instalación ', J, 0, cat = 'Binary')

modelo = lp.lpSum(x[j] for j in J)
for i in I:
     modelo += lp.lpSum(x[j] for j in J) >= 1
modelo.writeLP('Cobertura.lp') 
modelo.solve() 
print('Status', lp.LpStatus[modelo.status])
joni
  • 6,840
  • 2
  • 13
  • 20

1 Answers1

0

You are overwriting the modelo variable when defining the objective, so it's no longer a pulp.LpProblem. Use

modelo += lp.lpSum(x[j] for j in J) 

instead. Note the += instead of the =. Alternatively, you can use the addConstraint() and setObjective() methods instead of relying on the overload of the += operator, which is much cleaner IMHO:

modelo.setObjective(sum(x[j] for j in J))

for i in I:
     modelo.addConstraint(sum(x[j] for j in J) >= 1)
modelo.writeLP('Cobertura.lp') 
modelo.solve() 
joni
  • 6,840
  • 2
  • 13
  • 20