2023-02-16 18:20:43 +01:00
|
|
|
#!/usr/bin/env python3
|
2025-01-10 11:35:44 +01:00
|
|
|
# Copyright 2010-2025 Google LLC
|
2023-02-16 18:20:43 +01:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2023-07-01 06:06:53 +02:00
|
|
|
|
2023-11-16 19:46:56 +01:00
|
|
|
"""maximize the minimum of pairwise distances between n robots in a square space."""
|
2023-02-16 18:20:43 +01:00
|
|
|
|
2023-02-17 13:59:43 +01:00
|
|
|
import math
|
2023-02-16 18:20:43 +01:00
|
|
|
from typing import Sequence
|
|
|
|
|
from absl import app
|
|
|
|
|
from absl import flags
|
|
|
|
|
from ortools.sat.python import cp_model
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
_NUM_ROBOTS = flags.DEFINE_integer("num_robots", 8, "Number of robots to place.")
|
|
|
|
|
_ROOM_SIZE = flags.DEFINE_integer(
|
|
|
|
|
"room_size", 20, "Size of the square room where robots are."
|
|
|
|
|
)
|
2023-02-16 18:20:43 +01:00
|
|
|
_PARAMS = flags.DEFINE_string(
|
2023-07-01 06:06:53 +02:00
|
|
|
"params",
|
|
|
|
|
"num_search_workers:16, max_time_in_seconds:20",
|
|
|
|
|
"Sat solver parameters.",
|
2023-02-16 18:20:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-07-23 14:07:41 +02:00
|
|
|
def spread_robots(num_robots: int, room_size: int, params: str) -> None:
|
2023-02-16 18:20:43 +01:00
|
|
|
"""Optimize robots placement."""
|
|
|
|
|
model = cp_model.CpModel()
|
|
|
|
|
|
|
|
|
|
# Create the list of coordinates (x, y) for each robot.
|
2023-11-16 19:46:56 +01:00
|
|
|
x = [model.new_int_var(1, room_size, f"x_{i}") for i in range(num_robots)]
|
|
|
|
|
y = [model.new_int_var(1, room_size, f"y_{i}") for i in range(num_robots)]
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# The specification of the problem is to maximize the minimum euclidian
|
|
|
|
|
# distance between any two robots. Unfortunately, the euclidian distance
|
|
|
|
|
# uses the square root operation which is not defined on integer variables.
|
2023-02-17 13:59:43 +01:00
|
|
|
# To work around, we will create a min_square_distance variable, then we make
|
|
|
|
|
# sure that its value is less than the square of the euclidian distance
|
|
|
|
|
# between any two robots.
|
2023-02-16 18:20:43 +01:00
|
|
|
#
|
|
|
|
|
# This encoding has a low precision. To improve the precision, we will scale
|
2023-02-17 13:59:43 +01:00
|
|
|
# the domain of the min_square_distance variable by a constant factor, then
|
|
|
|
|
# multiply the square of the euclidian distance between two robots by the same
|
|
|
|
|
# factor.
|
2023-02-16 18:20:43 +01:00
|
|
|
#
|
2023-02-17 13:59:43 +01:00
|
|
|
# we create a scaled_min_square_distance variable with a domain of
|
|
|
|
|
# [0..scaling * max euclidian distance**2] such that
|
2023-02-16 18:20:43 +01:00
|
|
|
# forall i:
|
2023-02-17 13:59:43 +01:00
|
|
|
# scaled_min_square_distance <= scaling * (x_diff_sq[i] + y_diff_sq[i])
|
|
|
|
|
scaling = 1000
|
2023-11-16 19:46:56 +01:00
|
|
|
scaled_min_square_distance = model.new_int_var(
|
2023-07-01 06:06:53 +02:00
|
|
|
0, 2 * scaling * room_size**2, "scaled_min_square_distance"
|
|
|
|
|
)
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Build intermediate variables and get the list of squared distances on
|
|
|
|
|
# each dimension.
|
|
|
|
|
for i in range(num_robots - 1):
|
|
|
|
|
for j in range(i + 1, num_robots):
|
|
|
|
|
# Compute the distance on each dimension between robot i and robot j.
|
2023-11-16 19:46:56 +01:00
|
|
|
x_diff = model.new_int_var(-room_size, room_size, f"x_diff{i}")
|
|
|
|
|
y_diff = model.new_int_var(-room_size, room_size, f"y_diff{i}")
|
|
|
|
|
model.add(x_diff == x[i] - x[j])
|
|
|
|
|
model.add(y_diff == y[i] - y[j])
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Compute the square of the previous differences.
|
2023-11-16 19:46:56 +01:00
|
|
|
x_diff_sq = model.new_int_var(0, room_size**2, f"x_diff_sq{i}")
|
|
|
|
|
y_diff_sq = model.new_int_var(0, room_size**2, f"y_diff_sq{i}")
|
|
|
|
|
model.add_multiplication_equality(x_diff_sq, x_diff, x_diff)
|
|
|
|
|
model.add_multiplication_equality(y_diff_sq, y_diff, y_diff)
|
2023-02-16 18:20:43 +01:00
|
|
|
|
2023-02-17 13:59:43 +01:00
|
|
|
# We just need to be <= to the scaled square distance as we are
|
|
|
|
|
# maximizing the min distance, which is equivalent as maximizing the min
|
|
|
|
|
# square distance.
|
2023-11-16 19:46:56 +01:00
|
|
|
model.add(scaled_min_square_distance <= scaling * (x_diff_sq + y_diff_sq))
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Naive symmetry breaking.
|
|
|
|
|
for i in range(1, num_robots):
|
2023-11-16 19:46:56 +01:00
|
|
|
model.add(x[0] <= x[i])
|
|
|
|
|
model.add(y[0] <= y[i])
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Objective
|
2023-11-16 19:46:56 +01:00
|
|
|
model.maximize(scaled_min_square_distance)
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Creates a solver and solves the model.
|
|
|
|
|
solver = cp_model.CpSolver()
|
|
|
|
|
if params:
|
2025-07-23 17:38:49 +02:00
|
|
|
solver.parameters.parse_text_format(params)
|
2023-02-16 18:20:43 +01:00
|
|
|
solver.parameters.log_search_progress = True
|
2023-11-16 19:46:56 +01:00
|
|
|
status = solver.solve(model)
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
# Prints the solution.
|
|
|
|
|
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
|
2023-07-01 06:06:53 +02:00
|
|
|
print(
|
|
|
|
|
f"Spread {num_robots} with a min pairwise distance of"
|
2023-11-16 19:46:56 +01:00
|
|
|
f" {math.sqrt(solver.objective_value / scaling)}"
|
2023-07-01 06:06:53 +02:00
|
|
|
)
|
2023-02-16 18:20:43 +01:00
|
|
|
for i in range(num_robots):
|
2023-11-16 19:46:56 +01:00
|
|
|
print(f"robot {i}: x={solver.value(x[i])} y={solver.value(y[i])}")
|
2023-02-16 18:20:43 +01:00
|
|
|
else:
|
2023-07-01 06:06:53 +02:00
|
|
|
print("No solution found.")
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv: Sequence[str]) -> None:
|
|
|
|
|
if len(argv) > 1:
|
2023-07-01 06:06:53 +02:00
|
|
|
raise app.UsageError("Too many command-line arguments.")
|
2023-02-16 18:20:43 +01:00
|
|
|
|
|
|
|
|
spread_robots(_NUM_ROBOTS.value, _ROOM_SIZE.value, _PARAMS.value)
|
|
|
|
|
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
if __name__ == "__main__":
|
2023-02-16 18:20:43 +01:00
|
|
|
app.run(main)
|