Optimizing node placement in a 2D grid to match specific geodesic distances

I’m working on a problem where I need to arrange a set of nodes within a 2D grid such that the distance between pairs of nodes either approximate specific values as closely as possible or meet a minimum threshold.

In other words, for some node pairs, the distance should be approximately equal to a predefined value (≈). For other node pairs, the distance should be greater than or equal to a predefined threshold (≥).

Additional challenge: the grid is inscribed inside a concave polygon, so distances must be geodesic.

Question: Using OR-Tools, how can I approximate the location of the nodes given the constraints above-mentioned?

Here’s an incomplete minimal example script that:

  • creates a simple conforming grid
  • pre-computes geodesic distances between all pairs of cells within that grid
  • indicates the number of nodes to place as well as their associated target pairwise distances
    • each pairwise distance comes with an objective to match (either ≈ or ≥)
  • declares the CP-SAT model and creates the main variables for the problem

Having absolutely no experience of OR-tools I’m unfortunately having a hard time implementing the constraints and correctly defining the objectives to be optimized.

from ortools.sat.python import cp_model
from itertools import combinations
import networkx as nx

# Dimensions of the original grid (width & height)
w, h = 7, 5

# Selection of grid-cell indices (conforming/concave grid)
cell_indices = set(range(w*h)) - set([0, 1, 2, 3, 7, 8, 9, 10, 28, 29])

# Topology of the conforming/concave grid
T = nx.Graph()

for i in cell_indices:
    if i >= w and (i - w) in cell_indices:
        T.add_edge(i, i - w, weight=1)
    if i < w * (h - 1) and (i + w) in cell_indices:
        T.add_edge(i, i + w, weight=1)
    if i % w != 0 and (i - 1) in cell_indices:
        T.add_edge(i, i - 1, weight=1)
    if (i + 1) % w != 0 and (i + 1) in cell_indices:
        T.add_edge(i, i + 1, weight=1)
    
# Precompute geodesic distances using Dijkstra's algorithm
geodesic_distances = dict(nx.all_pairs_dijkstra_path_length(T))

# Get the smallest and largest geodesic distances 
max_distance = float('-inf')
min_distance = float('inf')
for i1 in geodesic_distances:
    for i2 in geodesic_distances[i1]:
        if i1 != i2 and i1 > i2:
            distance = geodesic_distances[i1][i2]
            if distance > max_distance: max_distance = distance
            if distance < min_distance: min_distance = distance
      
# Number of nodes to place
num_nodes = 5

# Target distances to match between each pair of nodes + type of objective (≈ or ≥)
objective_distances = {(0, 1): (2, '≈'),
                       (0, 2): (2, '≥'),
                       (0, 3): (2, '≥'),
                       (0, 4): (3, '≈'),
                       (1, 2): (3, '≥'),
                       (1, 3): (3, '≥'),
                       (1, 4): (4, '≥'),
                       (2, 3): (2, '≈'),
                       (2, 4): (4, '≥'),
                       (3, 4): (3, '≈')}

# Instantiate model
model = cp_model.CpModel()

# Possible locations for a node (indices of grid points)
indices = [model.NewIntVarFromDomain(cp_model.Domain.FromValues(list(cell_indices)), f'p{i}') for i in range(num_nodes)]

# Ensure all nodes are placed at different grid locations
model.AddAllDifferent(indices)

# Objective variable to minimize (probably needed for distance approximation -> ≈)
total_deviation = model.NewIntVar(0, 1000, 'total_deviation')

# Accumulate the absolute differences between geodesic distances and target distances
deviations = []

# For each pair of nodes
for i1, i2 in combinations(range(num_nodes), 2):

    # Get the target distance that should ideally separate the pair
    target_dist, obj_type = objective_distances[(i1, i2)]
    
    # Set a variable for the actual distance
    actual_dist = model.NewIntVar(min_distance, max_distance, f'dist_{i1}_{i2}')
    
    # !! Don't know what to do from there !!
    
    # if obj_type == '≈':
    #
    #    - how to set a deviation from the target distance based on the set of all possible geodesic distances
    #    - how to minimize that deviation
    #
    # elif obj_type == '≥'
    #
    #    - how to make sure that 'actual_dist' is greater than or equal to the 'target_dist'

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật