I'm working on a problem that requires me to solve the same optimization multiple times, although with a different set of variables every time. I'm using a for loop to iterate through the optimization, and for some reason, it only works the first time (the correct outcome is produced; I checked by hand). However, every time after the first time, the optimization provides a suboptimal problem. I get this statement: warnings.warn("Overwriting previously set objective.")
By inspection, the solution that is calculated is incorrect (I'm trying to minimize the distance between pairs of objects, and it's just clearly wrong). Furthermore, the objective function is 0, which doesn't make sense, because I defined the objective to be the summed distances of pairs, so the value of the objective function should always be greater than 0. The code is pretty long, but maybe it'll show what I'm doing wrong. This is what I'm iterating over:
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
Thank you in advance!