Skip to content
Snippets Groups Projects
Commit 9316ffd6 authored by Adam Sitabkhan's avatar Adam Sitabkhan
Browse files

Enforces unique grid indices

parent 313ef6b5
No related branches found
No related tags found
1 merge request!1Updated place grid
...@@ -42,40 +42,38 @@ def place_grid(robot_locations, cell_size=1, grid_shape=(5, 5)): ...@@ -42,40 +42,38 @@ def place_grid(robot_locations, cell_size=1, grid_shape=(5, 5)):
constraints.append(grid_indices[:,1] <= grid_shape[0] - 1) constraints.append(grid_indices[:,1] <= grid_shape[0] - 1)
# No two robots can share a cell # No two robots can share a cell
# Reformulation of the constraints # Use Big M method to ensure separation in at least one direction
# abs(grid_indices[i, 0] - grid_indices[j, 0]) >= 1 and M = max(grid_shape) * 10
# abs(grid_indices[i, 1] - grid_indices[j, 1]) >= 1
# to be compatible with solver
for i in range(N): for i in range(N):
for j in range(i+1, N): for j in range(i+1, N):
# Auxiliary variable for the distance between cell centers i and j in the x direction # At least one of the two constraints below must be true
xdist = cp.Variable(name=f"xdist_{i}_{j}") y1 = cp.Variable(boolean=True)
constraints.append(xdist >= grid_indices[i, 0] - grid_indices[j, 0]) y2 = cp.Variable(boolean=True)
constraints.append(xdist >= -(grid_indices[i, 0] - grid_indices[j, 0])) constraints.append(y1 + y2 >= 1)
# Auxiliary variable for the distance between cell centers i and j in the y direction # Enforces separation by at least 1 in the x direction
ydist = cp.Variable(name=f"ydist_{i}_{j}") if robot_locations[i, 0] >= robot_locations[j, 0]:
constraints.append(ydist >= grid_indices[i, 1] - grid_indices[j, 1]) constraints.append(grid_indices[i, 0] - grid_indices[j, 0] + M * (1 - y1) >= 1)
constraints.append(ydist >= -(grid_indices[i, 1] - grid_indices[j, 1])) else:
constraints.append(grid_indices[j, 0] - grid_indices[i, 0] + M * (1 - y1) >= 1)
# Enforce that robots must be at least one cell apart # Enforces separation by at least 1 in the y direction
constraints.append(xdist + ydist >= cell_size) if robot_locations[i, 1] >= robot_locations[j, 1]:
constraints.append(grid_indices[i, 1] - grid_indices[j, 1] + M * (1 - y2) >= 1)
else:
constraints.append(grid_indices[j, 1] - grid_indices[i, 1] + M * (1 - y2) >= 1)
# Solve the optimization problem # Solve the optimization problem
prob = cp.Problem(cp.Minimize(cost), constraints) prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve(solver=cp.SCIP, scip_params={ prob.solve(solver=cp.SCIP)
"numerics/feastol": 1e-6,
"numerics/dualfeastol": 1e-6,
})
if prob.status not in ["optimal", "optimal_inaccurate"]: if prob.status not in ["optimal", "optimal_inaccurate"]:
print("problem could not be solved to optimality") print("Problem could not be solved to optimality.")
return None return None
print(prob.status)
return origin.value, cell_centers.value return origin.value, cell_centers.value
def main(): def main():
robot_locations = [(1.2, 1.6), (1.6, 1.2)] robot_locations = np.random.uniform(low=0, high=5, size=(3, 2))
cell_size = 1 cell_size = 1
grid_shape = (5, 5) grid_shape = (5, 5)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment