Question: Can you fill out the following code : [ from pulp import * from math import sqrt def euclidean _ distance ( location _ coords,

Can you fill out the following code :[from pulp import *
from math import sqrt
def euclidean_distance(location_coords, i, j):
assert 0<= i and i < len(location_coords)
assert 0<= j and j < len(location_coords)
if i == j:
return 0.0
(xi, yi)= location_coords[i] # unpack coordinate
(xj, yj)= location_coords[j]
return sqrt((xj - xi)**2+(yj - yi)**2)
def solve_warehouse_location(location_coords, R):
assert R >0.0, 'radius must be positive'
n = len(location_coords)
prob = LpProblem('Warehouselocation', LpMinimize)
#1. Formulate the decision variables
#2. Add the constraints for each vertex and edge in the graph.
#3. Solve and interpret the status of the solution.
#4. Return the result in the required form to pass the tests below.
# your code here
raise NotImplementedError
from matplotlib import pyplot as plt
def check_solution(location_coords, R, warehouse_locs):
# for each location i, calculate all locations j within distance R of location i
# use list comprehension instead of accumulating using a nested for loop.
n = len(location_coords)
assert all(j >=0 and j < n for j in warehouse_locs), f'Warehouse locations must be between 0 and {n-1}'
neighborhoods =[[j for j in range(n) if euclidean_distance(location_coords, i, j)<= R] for i in range(n)]
W = set(warehouse_locs)
for (i, n_list) in enumerate(neighborhoods):
assert any(j in W for j in n_list), f'Location # {i} has no warehouse within distance {R}. The locations within distance {R} are {n_list}'
print('Your solution passed test')
def visualize_solution(location_coords, R, warehouse_locs):
n = len(location_coords)
(xCoords, yCoords)= zip(*location_coords)
warehouse_x, warehouse_y =[xCoords[j] for j in warehouse_locs],[yCoords[j] for j in warehouse_locs]
fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.scatter(xCoords, yCoords)
for j in warehouse_locs:
circ = plt.Circle(location_coords[j], R, alpha=0.5, color='g',ls='--',lw=2,ec='k')
ax.add_patch(circ)
for i in range(n):
(x,y)= location_coords[i]
ax.annotate(f'{i}', location_coords[i])
plt.scatter(warehouse_x, warehouse_y, marker='x',c='r', s=30)
location_coords =[(1,2),(3,5),(4,7),(5,1),(6,8),(7,9),(8,14),(13,6)]
R =5
locs = solve_warehouse_location(location_coords, R )
print(f'Your code returned warehouse locatitons: {locs}')
assert len(locs)<=4, f'Error: There is an solution involving just 4 locations whereas your code returns {len(locs)}'
visualize_solution(location_coords, R, locs)
check_solution(location_coords, R, locs)]
such that it passes the following test:from matplotlib import pyplot as plt
def check_solution(location_coords, R, warehouse_locs):
# for each location i, calculate all locations j within distance R of location i
# use list comprehension instead of accumulating using a nested for loop.
n = len(location_coords)
assert all(j >=0 and j < n for j in warehouse_locs), f'Warehouse locations must be between 0 and {n-1}'
neighborhoods =[[j for j in range(n) if euclidean_distance(location_coords, i, j)<= R] for i in range(n)]
W = set(warehouse_locs)
for (i, n_list) in enumerate(neighborhoods):
assert any(j in W for j in n_list), f'Location # {i} has no warehouse within distance {R}. The locations within distance {R} are {n_list}'
print('Your solution passed test')
def visualize_solution(location_coords, R, warehouse_locs):
n = len(location_coords)
(xCoords, yCoords)= zip(*location_coords)
warehouse_x, warehouse_y =[xCoords[j] for j in warehouse_locs],[yCoords[j] for j in warehouse_locs]
fig, ax = plt.subplots()
ax.set_aspect('equal')
plt.scatter(xCoords, yCoords)
for j in warehouse_locs:
circ = plt.Circle(location_coords[j], R, alpha=0.5, color='g',ls='--',lw=2,ec='k')
ax.add_patch(circ)
for i in range(n):
(x,y)= location_coords[i]
ax.annotate(f'{i}', location_coords[i])
plt.scatter(warehouse_x, warehouse_y, marker='x',c='r', s=30)
location_coords =[(1,1),(1,2),(2,3),(1,4),(5,1),(3,3),(4,4),(1,6),(0,3),(3,5),(2,4)]
## TEST 1
R =2
print("R =2 Test:")
locs = solve_warehouse_location(location_coords, R )
print(f'Your code returned warehouse locatitons: {locs}')
assert len(locs)<=4, f'Error: There is an solution involving just 4 locations whereas your code returns {len(locs)}'
visualize_solution(location_coords, R, locs)
check_solution(location_coords, R, locs)
print('Test with R=2 has passed')
## TEST 2
print("R=3 Test:")
R =3
locs3= solve_warehouse_location(location_coords, R )
print(f'Your code returned warehouse locatitons: {locs3}')
assert len(locs3)<=2, f'Error: There is an solution involving just 4 locations whereas your code returns {len(locs)}'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!