我正在通过gurobi优化模块解决建模问题,但它是一个巨大的问题。但是,我将尝试根据一个简单的旅行推销员问题解释它,
A 是一个列表,其中包含从0到n访问任何城市的净利润值 优先级是 A 列表中任何 i 城市的优先级,因此它可能类似于precedences = [[1,3,5], [0,2,5] ....],因此对于城市0,在前往城市0之前必须访问的以前城市是city1,city3和city5。
必须解决问题,满足一些约束条件,例如时间或使用的时间段,每个时期受到最小城市访问量和最小收入的限制,以及可以解决问题的最大期限。
所以,我已经模仿了这样: 来自gurobipy import *
# data for problem
A=citiesvalue
Pc=precedences
Periods=range(7)
Disccount_rate=[0.9090909090909091, 0.8264462809917354, 0.7513148009015775, 0.6830134553650705, 0.6209213230591549, 0.5644739300537772, 0.5131581182307065]
m = Model()
n = len(A) # number of cities
# Indicator variable for each city
x = {}
for prd in Periods:
for i in range(n):
x[i,prd] = m.addVar(vtype=GRB.BINARY, name="x%d%d" %(i,prd))
m.update()
obj=(quicksum((quicksum(A[i]*x[i,prd] for i in range(n)))*Disccount_rate[prd] for prd in Periods))
# Set objective
m.setObjective(obj,GRB.MAXIMIZE)
# Add constraints
#this contrain prevents salesman go to city0 without going to its precedences cities at any period p
for e in range(0,len(Pc)):
for p in Periods:
for j in Pc[e]:
m.addConstr(x[u,p] <= x[v,p])
#this constrain prevents that a citie that was visited in a period 0 must be visited at periods>0 if it is at any precedence
for p in Periods:
for u in range(len(A)):
m.addConstr(x[u,p-1]<=x[u,p])
#this constrain prevent that in any period income be less than a certain number
m.addConstr(quicksum((A[i])*x[i,prd] for i in range(n) for prd in Periods))<=7500000)
你可以看到它是某种简单的问题,但是我的确非常庞大且 A 中每个项目的优先级也是如此,因此限制性添加需要花费很多时间才能完成,所以我让它超过24小时因内存不足而陷入困境。