Question

I am at wit's end as to create a "function" in clingo, if such can be defined. In a simplified version of what I am trying to accomplish, I wrote this very short program:

a(1..3).

adj(X,Y) :- a(X), a(Y), abs(X-Y)==1.

#hide a/1.

Basically, I want to define an adjacency function that given any two numbers will hold true if the absolute value of their difference equals one. What I'd expect from the above program is to output the results: adj(1,2) adj(2,1) adj(2,3) adj(3,2), yet I am presented with this output:

Answer: 1

SATISFIABLE

Models       : 1
Time         : 0.000
  Prepare    : 0.000
  Prepro.    : 0.000
  Solving    : 0.000

(if I omit #hide a/1. it will output the answer: a(1) a(2) a(3)).

I feel like this is clingo 101, so maybe I'm misunderstanding the basics here. I'm trying to learn clingo by studying examples I stumble upon online but I'm kind of rushing through it since I have an assignment due tomorrow. If anyone could shed some light it'd be great. Thanks.

Was it helpful?

Solution

Solved. It turns out the native abs function doesn't really work as I expected it to, but defining both conditions for the adj function does the disjunctive trick and works like a charm. This:

a(1..3).

adj(X,Y) :- a(X), a(Y), X-Y==1.
adj(X,Y) :- a(X), a(Y), Y-X==1.

#hide a/1.

Outputs:

Answer: 1
adj(3,2) adj(2,1) adj(2,3) adj(1,2) 
SATISFIABLE

Models      : 1     
Time        : 0.000
  Prepare   : 0.000
  Prepro.   : 0.000
  Solving   : 0.000
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top