Skip to content

Instantly share code, notes, and snippets.

@smasson-bi4all
Created September 14, 2021 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smasson-bi4all/677d817c715a3b3f20f011ecca102dc8 to your computer and use it in GitHub Desktop.
Save smasson-bi4all/677d817c715a3b3f20f011ecca102dc8 to your computer and use it in GitHub Desktop.
set_up_constraints
def set_up_constraints(solver, data_model, x, w, num_products, num_locations):
# Sum of quantities of each product should be equal to its quantities
for i in range(num_products):
solver.Add(solver.Sum([x[i, j] for j in range(num_locations)]) == data_model['products'][i].quantity)
# Each location should not have more products than its capacity.
for j in range(num_locations):
solver.Add(solver.Sum([x[i, j] for i in range(num_products)]) <= data_model['locations'][j].freeCapacity)
for upr in data_model['unallocated_prod_relationship']:
i, k = data_model['products'].index(upr.product1), data_model['products'].index(upr.product2)
quantity_i, quantity_k = data_model['products'][i].quantity, data_model['products'][k].quantity
for j in range(num_locations):
freeCapacity_j = data_model['locations'][j].freeCapacity
min_freeCapJ_quantI = min(freeCapacity_j, quantity_i)
min_freeCapJ_quantK = min(freeCapacity_j, quantity_k)
#solver.Add(w[i, j, k, j] >= 0)
# w[i, j, k, j] >= max(x[i, j])*x[k, j] + x[i, j]*max(x[k, j]) - max(x[i, j])*max(x[k, j]))
# the max value of x[i,j] = min(freeCapacity_j, quantity_i)
solver.Add( w[i, j, k, j] >= min_freeCapJ_quantI * x[k, j] + x[i, j] * min_freeCapJ_quantK
- min_freeCapJ_quantI * min_freeCapJ_quantK )
# w[i, j, k, j] <= max(x[i, j])*x[k, j] + x[i, j]*min(x[k, j]) - max(x[i, j])*min(x[k, j])
# the min value of each x[i,j] and x[k,j] is 0. So,
# w[i, j, k, j] <= max(x[i, j])*x[k, j]
# the max value of x[i,j] = min(freeCapacity_j, quantity_i)
solver.Add( w[i, j, k, j] <= min_freeCapJ_quantI * x[k, j] )
# w[i, j, k, j] <= x[i, j]*max(x[k, j]) + min(x[i, j])*x[k, j] - min(x[i, j])*max(x[k, j])
# the min value of each x[i,j] and x[k,j] is 0. So,
# w[i, j, k, j] <= x[i, j]*max(x[k, j])
# the max value of x[k,j] = min(freeCapacity_j, quantity_k)
solver.Add( w[i, j, k, j] <= x[i, j] * min_freeCapJ_quantK )
# Constraints
set_up_constraints(solver, data_model, x, w, num_products, num_locations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment