I’m currently looking at trying to find the longest shortest-path through a grid i.e. between two points in a grid, find the shortest path, then find the two points that would have the longest shortest path. Here’s my encoding of it (ignore the filled
atoms, they’re for when I start constructing obstacles in my grid):
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Find the maximum shortest-length path through an arbitrarily filled grid
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Generate a grid
g(X, Y) :- X=0..x-1, Y=0..y-1.
% WLOG choose a start and end cell from an empty cell in the grid
{start(X, Y) : g(X, Y), not filled(X, Y)} = 1.
{end(X, Y) : g(X, Y), not filled(X, Y), not start(X, Y)} = 1.
% Create edges between empty cells. Choose a subset of edges.
edge(X1, Y1, X2, Y2) :- g(X1, Y1), g(X2, Y2), |X1-X2| = 1, Y1 = Y2, not filled(X1, Y1), not filled(X2, Y2).
edge(X1, Y1, X2, Y2) :- g(X1, Y1), g(X2, Y2), |Y1-Y2| = 1, X1 = X2, not filled(X1, Y1), not filled(X2, Y2).
% For each destination E, some outgoing edge from the start node should be selected
:- start(X1, Y1), not selected(X1, Y1, _, _).
% No incoming edge to the start node should be selected
:- start(X, Y), selected(_, _, X, Y).
% If an edge points to the end node, then it may be (or not be) selected for reaching it
0 { selected(X1, Y1, X2, Y2) } 1 :- edge(X1, Y1, X2, Y2), end(X2, Y2).
% If an outgoing edge from Y has been selected for reaching E, then an incoming edge may be (or not be) selected for reaching E
0 { selected(X1, Y1, X2, Y2) } 1 :- edge(X1, Y1, X2, Y2), selected(X2, Y2, _, _).
%%%%%Where I would try to optimize%%%%%%%
#minimize{1, X1, Y1, X2, Y2: selected(X1, Y1, X2, Y2)}.
#show start/2.
#show end/2.
#show selected/4.
The current code above just finds the two points that have the shortest distance between them. So in the end, I would want to do something like #maximize{#minimize{...}}
, but that’s possible. How should I approach it?
For reference, the command I’m running is clingo program.lp -c x=3 -c y-3
, where x, y
are the height/width of the grid.