Skip to content

Instantly share code, notes, and snippets.

@smasson-bi4all
Created September 14, 2021 14:55
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/8c45da4d930a541eb5ba2069ad4965ac to your computer and use it in GitHub Desktop.
Save smasson-bi4all/8c45da4d930a541eb5ba2069ad4965ac to your computer and use it in GitHub Desktop.
print_solution
# Solve
status = solver.Solve()
print('==== Assignment Result: ====')
if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:
print('Total cost = ', solver.Objective().Value(), '\n')
prods = generate_items_dict(data_model['products'])
locations = generate_items_dict(data_model['locations'])
for i in range(num_products):
product = data_model['products'][i]
for j in range(num_locations):
location = data_model['locations'][j]
if x[i, j].solution_value() > 0.5:
prods[product] += x[i, j].solution_value()
locations[location] += x[i, j].solution_value()
cost_product = data_model['costs'][i][j].product
cost_product_name = data_model['costs'][i][j].product.name
cost_location_name = data_model['costs'][i][j].location.name
quantity = x[i, j].solution_value()
res = 'Product %s assigned to Shelf %s. Quantity = %d Cost = %f '
print(res % ( cost_product_name, cost_location_name , quantity,
quantity * (data_model['costs'][i][j].value -
data_model['allocated_unallocated_prod_relationship'][i][j].weight) ))
print()
print('==== Weights w[i, j, k, j] ====')
total_weight = 0
for upr in data_model['unallocated_prod_relationship']:
i, k = data_model['products'].index(upr.product1), data_model['products'].index(upr.product2)
for j in range(num_locations):
res = 'Product %s and Product %s at Location %s: Weight = %d'
print(res % (upr.product1.name, upr.product2.name, data_model['locations'][j].name, w[i, j, k, j].solution_value() ))
total_weight += w[i, j, k, j].solution_value()
print('Total Weight = %d' % (total_weight))
if status == pywraplp.Solver.OPTIMAL:
print('OPTIMAL')
elif status == pywraplp.Solver.FEASIBLE:
print('FEASIBLE')
else:
print('No Solution Found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment