我正在解决一个需要多次解决相同优化问题的问题,尽管每次都有不同的变量集。我使用for循环来迭代优化,并且出于某种原因,它只在第一次起作用(产生了正确的结果;我手动检查)。但是,每次第一次之后,优化都会提供次优问题。我得到这样的声明:warnings.warn("覆盖以前设定的目标。")
通过检查,计算出的解决方案是不正确的(我试图最小化对象之间的距离,并且它显然是错误的)。此外,目标函数为0,这没有意义,因为我将目标定义为对的总距离,因此目标函数的值应始终大于0.代码很长,但也许它会显示我做错了什么。这是我正在迭代的内容:
model = pulp.LpProblem('R_lessthan_V_' + str(t), LpMinimize)
#Set variables to optimize
x = pulp.LpVariable.dicts('single_ride_' + str(t), ((i.num, j.num) for i in R_prime for j in V_prime), cat = 'Binary')
x_prime = pulp.LpVariable.dicts('shared_ride_' + str(t), ((i.num, j.num) for i in R_prime for j in V_prime), cat = 'Binary')
#Set objective function
model += (pulp.lpSum([pulp.lpSum([[x[(i.num, j.num)] * (d[i.num][j.num] + phi * p[j.num])]
+ [x_prime[(i.num, j.num)] * rideshare_pen[i.num][j.num][0]]
+ [delta * q[j.num] * (x[(i.num, j.num)] + x_prime[(i.num, j.num)]) * (1 - y[i.num][j.num])]
for i in R_prime]) for j in V_prime])), 'wait_cost_' + str(t)
#Set constraints
for j in V_prime:
label = 'rideshare_pass_constraint_time%d_v%d' % (t, j.num)
condition = p[j.num] >= pulp.lpSum([[x_prime[(i.num, j.num)]] for i in R_prime]) #passenger 1 before 2 ride-share
model += condition, label
label_single = 'one_max_single_time%d_v%d' % (t, j.num)
label_shared = 'one_max_shared_time%d_v%d' % (t, j.num)
condition_single = pulp.lpSum([[x[(i.num, j.num)]] for i in R_prime]) <= 1 #cap the number of single rides for each vehicle
condition_shared = pulp.lpSum([[x_prime[(i.num, j.num)]] for i in R_prime]) <= 1 #cap the number of ride shares for each vehicle
model += condition_single, label_single
model += condition_shared, label_shared
for i in R_prime:
label = 'all_assigned_time%d_p%d' % (t, i.num)
condition = pulp.lpSum([[x[(i.num, j.num)]] + [x_prime[(i.num, j.num)]] for j in V_prime]) == 1 #every passenger is assigned
model += condition, label
for i in R_prime:
for j in V_prime:
label_single = 'nonneg_single_time%d_p%d_i%d' % (t, i.num, j.num)
label_shared = 'nonneg_shared_time%d_p%d_i%d' % (t, i.num, j.num)
condition_single = x[(i.num, j.num)] >= 0 #nonnegative single rides
condition_shared = x_prime[(i.num, j.num)] >= 0 #nonnegative ride shares
model += condition_single, label_single
model += condition_shared, label_shared
for i in R_A:
label = 'stay_assigned_time%d_p%d' % (t, i.num)
condition = pulp.lpSum([x[(i.num, j.num)]] + [x_prime[(i.num, j.num)]]) #prevents assigned -> unassigned
model += condition, label
for j in V_P:
label_single = 'reassign_single_time%d_p%d_i%d' % (t, i.num, j.num)
label_shared = 'reassign_shared_time%d_p%d_i%d' % (t, i.num, j.num)
condition_single = b[i.num] * (y[i.num][j.num] - x[(i.num,j.num)]) <= 0 #prevents more than 1 reassignment
condition_shared = b[i.num] * (y[i.num][j.num] - x_prime[(i.num,j.num)]) <= 0 #prevents more than 1 reassignment
model += condition_single, label_single
model += condition_shared, label_shared
提前谢谢!
答案 0 :(得分:0)
迭代在哪里?我只看到一个模型,但在代码中没有解决
也
model += pulp.lpSum(x[(i.num, j.num)] * (d[i.num][j.num] + phi * p[j.num])
+ x_prime[(i.num, j.num)] * rideshare_pen[i.num][j.num][0]
+ delta * q[j.num] * (x[(i.num, j.num)] + x_prime[(i.num, j.num)]) * (1 - y[i.num][j.num])
for i in R_prime for j in V_prime), 'wait_cost_' + str(t)
更具惯用性