I have an optimization problem that consists of reducing the price/weight of the shipping of the products as much as possible. I modeled as below: MATHEMATICAL MODEL
Basically, I have a vector called order, which represents the weight of each order to be delivered. There is a vector w, which represents the weight of each truck, a vector p which represents the price that transport costs using each one of them. And a variable n, which represents a maximum number of customers on a delivery route.
I'm trying to use Gurobi as a solver but I'm getting the following error:
TypeError Traceback (most recent call last)
src/gurobipy/var.pxi in gurobipy.Var.__truediv__()
TypeError: float() argument must be a string or a number, not 'generator'
During handling of the above exception, another exception occurred:
GurobiError Traceback (most recent call last)
<ipython-input-5-18654dbac5a8> in <module>
---> 30 m.setObjective( gp.quicksum((z_n[i]/(gp.quicksum(x[i][j] *order[j]) for j in range(numItems))) for i in range(numTrucks)) , GRB.MINIMIZE)
src/gurobipy/gurobi.pxi in gurobipy.quicksum()
<ipython-input-5-18654dbac5a8> in <genexpr>(.0)
---> 30 m.setObjective( gp.quicksum((z_n[i]/(gp.quicksum(x[i][j] *order[j]) for j in range(numItems))) for i in range(numTrucks)) , GRB.MINIMIZE)
src/gurobipy/var.pxi in gurobipy.Var.__truediv__()
GurobiError: Divisor must be a constant
Follow the implemented code:
w = [650,1200,1200,1200,1200,1800,1800,1800,1800]
p = [250,330, 330, 330, 330, 400, 400, 400,400 ]
n = 7
order = [288,61,91,65,103,114,71,392,80,306,749,159,149,204,64,45,156,110,562,96,295,75,465,51,54,414,52,571,73,286,96,325,81,111,138,13,105,112,60,52,70,238,519,305,117,222,112,26,54,26,131,73,63,249,144,44,15,275,302]
numTrucks = len(w)
numItems = len(order)
import gurobipy as gp
from gurobipy import GRB
import itertools
m = gp.Model('trucks optimization')
x = m.addVars(numTrucks, numItems, vtype=gp.GRB.BINARY, name="x")
z_n = m.addVars(numTrucks, name="z_n")
m.addConstrs(gp.quicksum(x[i,j] for j in range(numItems)) <= n for i in range(numTrucks))
m.addConstrs(gp.quicksum(x[i,j] for i in range(numTrucks)) == 1 for j in range(numItems))
m.addConstrs(gp.quicksum(x[i,j] * order[j] for j in range(numItems)) <= w[i] for i in range(numTrucks))
m.addConstrs(z_n[i] == p[i] for i in range(numTrucks))
m.setObjective( gp.quicksum((z_n[i]/(gp.quicksum(x[i][j] *order[j]) for j in range(numItems))) for i in range(numTrucks)) , GRB.MINIMIZE)
m.optimize()