Category Archives: Programming

Recursive aggregates in Answer Set Programming

The use of recursive aggregates in Answer Set Programming (ASP) was so far limited because current solvers only deal with monotone aggregates. The grounder is thus in charge for rewriting aggregates into the more constrained form accepted by solvers. However, until now, no rewriting that correctly transform non-convex aggregates was known. This is going to change soon! Give a try to this script on my GitHub. File README, reported below, should give enough information on how to use the script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
The following tools are required:
 
- python 3: https://www.python.org/downloads/
 
- gringo 3: http://sourceforge.net/projects/potassco/files/gringo/3.0.5/
 
- clasp 3: http://sourceforge.net/projects/potassco/files/clasp/3.1.1/
 
 
To compute all F-stable models of program example1.asp, use the following command-line:
 
$ ./f-aggregates.py example1.asp | clasp 0
 
If everything is OK, you should see the following output:
 
clasp version 3.1.1
Reading from stdin
Solving...
Answer: 1
var(exists,x1,1) var(exists,x2,2) var(forall,y1,2) var(forall,y2,3) int(5) unequal true(forall,y2,3) true(forall,y1,2) true(exists,x1,1)
SATISFIABLE
 
Models       : 1     
Calls        : 1
Time         : 0.001s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.000s
 
 
If command 'gringo' is not in the PATH, use the -g flag:
 
$ ./f-aggregates.py -g /path/to/gringo example1.asp
 
Flags for gringo can also be specified (at the end of the command line).

Since this is just a proof-of-concept, aggregates must be encoded using predicates. Run the script with flag --help-syntax for more instruction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ ./f-aggregates.py --help-syntax
 
The syntax is almost conformant to ASP Core 2.0, with the exception of aggregates.
Moreover, predicates starting with the prefix 'f_' are reserved for internal use
and must not be used by the user. Remember also to not hide them!
 
 
Supported aggregates are COUNT, SUM, AVG, MIN, and MAX. Aggregate sets are
declared by means of predicate f_set/3, where:
 
- the first argument is the ID of an aggregate;
 
- the second argument is an integer;
 
- the third argument is an atom.
 
The second argument is optional, with default value 1.
 
 
A COUNT aggregate can be added in a rule body by using predicate f_count/3, where:
 
- the first argument is the ID of an aggregate;
 
- the second argument is a comparator among ">=", ">", "<=", "<", "=", "!=";
 
- the third argument is an integer.
 
 
Aggregates SUM, AVG, MIN, and MAX are similar, and use predicates f_sum/3, f_avg/3,
f_min/3, and f_max/3.
 
 
Remember that COUNT is applied on true atoms in the aggregate set, while SUM, AVG, 
MIN, and MAX are applied on the multiset of integers associated with true atoms in
the aggregate set.
 
 
Variables can be used taking into account the previous description.

Second patch for bfsms – Send concatenated SMS’s via bluetooth

In a previous post we talked about the program bfsms for sending SMS’s via bluetooth. We also applied a patch for sending splitted SMS’s, in other words messages which are divided in more than one SMS and sent one-by-one.

Clearly, a unique SMS is preferable, like for long SMS’s sent by moder mobile phone. This kind of messages are called concatenated SMS’s and Continue reading

Patch for bfsms – Send SMS’s via bluetooth

There are programs which are simple and really useful, but for some reason their development were stopped. Probably, bfsms is one of this, stopped still 2004. This application allows to send SMS’s from a shell, connecting to a mobile phone via bluetooth. The only inconvenience is that such SMS’s must be no longer than 160 characters (or 70 for flash SMS’s).

Since I often send SMS’s longer than 160 characters, I tried to search the web the standard defining concatenated SMS’s (which are sended by modern mobile phone when an SMS of more than 160 character is typed). I lost an afternoon trying to understand the absurd choices of the authors of this protocol. Then, I give up this street and moved to a different approach: I will automatically divide the message in more than one SMS, avoiding Continue reading

Seconda patch per bfsms – Inviare SMS concatenati tramite bluetooth

In un post precedente abbiamo parlato del programma bfsms per l’invio di SMS tramite bluetooth. Abbiamo inoltre visto come applicargli una patch per poter inviare SMS splittati, ovvero messaggi che, se di dimensione superiore ai 160 caratteri consentiti, vengono suddivisi in più SMS e spediti singolarmente.

Naturalmente sarebbe preferibile che il messaggio arrivasse al destinatario come un unico SMS, come avviene per i messaggi lunghi inviati dalla maggior parte dei cellulari moderni. Questo tipo di messaggi viene chiamato concatenated SMSes (SMS concatenati) e Continue reading

Patch per bfsms – Inviare SMS tramite bluetooth

Ci sono programmi semplici e dall’indubbia utilità, che però per un motivo o per un altro non vengono più portati avanti dal loro sviluppatore. Probabilmente, bfsms è uno di questi, fermo dal 2004. L’applicazione consente di inviare SMS dalla shell collegandosi al proprio cellulare tramite bluetooth. Unico inconveniente, gli SMS sono limitati a 160 caratteri (o 70 nel caso di SMS flash).

Trovandomi spesso a dover inviare SMS più lunghi di 160 caratteri, ho provato a cercare in rete lo standard per l’invio dei messaggi concatenati (quelli che i cellulari più moderni inviano e ricompongono automaticamente quando si scrive un SMS più lungo di 160 caratteri). Dopo un pomeriggio sofferto nel cercare di capire le astruse scelte degli ideatori di questo protocollo, ho deciso di rinunciare e seguire un’altra strada: dividere in automatico il messaggio in più SMS, magari evitando Continue reading

Come accedere a file che iniziano per trattino nella shell

I file che iniziano per trattino (o meno) possono essere difficili da gestire tramite la shell, poiché il trattino viene generalmente utilizzato dalle opzioni del comando eseguito. Quindi, per specificare un file nella directory corrente, non si può scrivere il nome del file così com’è. Inoltre, per qualche ragione misteriosa, anche se il nome del file viene quotato (racchiuso fra virgolette), il problema Continue reading