0

I'm confused about the inner workings of ASP. Say I'm modelling a country's. For example, say we have

city(london).
city(manchester).
city(leeds).
city(newcastle).
city(sheffield).

road(a1).
road(b1).
connects(a1, london,sheffield).
connects(b1, sheffield, newcastle).
closed(b1).

etc. What I'm wondering is the following. Say I want to work out whether there is a route between two cities. So, I define a predicate like this:

is_road(A, B) :- connects(R, A, B),
                  -closed(R).

road(A,B) :- is_road(A,B).
road(A,C) :- is_road(A,B),
             road(B,C).

Then does ASP work out all the routes or does it just work out there is a route between two cities and stop there? This has become important for me because I'm trying to define a predicate for a slow road, and how I do this depends on whether ASP has access to all the possible routes or not. My intuition tells me that it should because ASP works by grounding and solving (so surely all the correct variable instantiations should show up, but the algorithm ASP uses to compute answer sets is somewhat mysterious.

To clarify Say there's a road from London to Sheffield and Sheffield to Newcastle. Then there is a route from London to Newcastle by using those two roads. But another route would be going back to London then going back to Newcastle, or if there also a road from Sheffield to Leeds and Leeds to Newcastle, then a third route would be to go to Sheffield then go to Newcastle via Leeds.

Thank you for any help.

Dan Öz
  • 319
  • 1
  • 8
  • You should focus your question on a specific problem. In order to provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), you should clarify what are the actual and desired outputs. – logi-kal Feb 15 '23 at 08:04
  • I would also suggest you to choose less ambiguous names for predicates. How can you semantically distinguish `road/2` and `is_road/2`? – logi-kal Feb 15 '23 at 08:06
  • 1
    You should look at logi-kal's answer here https://stackoverflow.com/questions/74715495/calculating-the-distance-between-two-nodes-in-a-directed-graph where your cities are nodes and roads are edges. Regarding your question about the solver, it might be useful to consider the solver lazy: if you only ask it to look if there is a route from A to B, then internally if it finds one such route, it can stop looking for another one. You can however ask for more: e.g. look at the `distance` predicate in the answer linked above, which will tell you if there is a route of specific _length_ from A to B. – vukk Feb 16 '23 at 18:00

0 Answers0