modelling error
AnsweredHi,
I have an error for the below code which is written in python gurobi.I tried to add Constrs or different method but couldn't find the correction method. What do you suggest to get rid of this error?
Thank you in advance
F = m.addConstr( (R[month]/2 for month in months) == gp.quicksum(y[skill, month]*b[skill, month] for skill in skills for month in months), name ="R")
TypeError: unsupported operand type(s) for -: 'gurobipy.QuadExpr' and 'generator'
-
Hi,
Are you trying to model
\[\sum_{m \in months} R_m/2 = \sum_{s \in skills, m \in months}y_{s,m}b_{s,m}\]
or
\[R_m/2 = \sum_{s \in skills}y_{s,m}b_{s,m} \qquad \forall \ m \in months \ ?\]Option 1 can be modelled as:
F = m.addConstr( gp.quicksum(R[month]/2 for month in months) == gp.quicksum(y[skill, month]*b[skill, month] for skill in skills for month in months), name ="R")Option 2 can be modelled as:
F = m.addConstrs( (R[month]/2 == gp.quicksum(y[skill, month]*b[skill, month] for skill in skills) for month in months), name="R")
I hope this helps.Best regards,Simran0 -
I was trying to write 2nd funtion, now it works, thank you for your support Simranjit :)
0 -
Hi Simranjit,
I would be very happy if you make some comment s about below error too:
Total_rev = m.addVars(months, vtype=GRB.CONTINUOUS, name="totalrevenue")
Total_revenue= m.addConstr( (Total_rev[month] for month in months) == gp.quicksum( p[skill,product,month]*r[product]*x[skill, month] + k[skill,product,month]*r[product]*y[skill, month] for skill in skills for month in months for product in products), name= "rev")KeyError: (1, 1, 1)
0 -
Hi Ege,
A KeyError is raised when you try to access an element before it is created. Please check if p[1,1,1] and k[1,1,1] exist.
Also, please consider re-writing the Total_revenue constraint. In its current form, it will throw the same error as you had in the first constraint we discussed in this post.
Best regards,
Simran0
Please sign in to leave a comment.
Comments
4 comments