#!/usr/bin/perl

# ----------------------------------------------------------------
# DTPgen.pl - generates a random Disjunctive Temporal Problem,
#             given a set of parameters <k,n,m,L>
# Author     : Massimo Idini
# Updated by : Claudio Castellini
# Date       : Summer 2003 
#
# Use DTPgen <k> <n> <m> <L> <seed>, where
#   k     is the number of disjuncts per disjunction (forced to be 2)
#   n     is the number of variables
#   m     is the number of disjunctions
#   L     is the bound on the constant term (usually 100)
#   seed  is the random seed.
# ----------------------------------------------------------------

# parse command line switches

$k =$ARGV[0];
$n =$ARGV[1];
$m =$ARGV[2];
$L =$ARGV[3];
$seed =$ARGV[4];

# check their consistency

($k==2)     || die "ERROR: only k == 2 is admitted.\n" ;
($n>=3)     || die "ERROR: n must be greater than 2.\n";
($m>=3)     || die "ERROR: m must be greater than 2.\n";
($L>=0)     || die "ERROR: L cannot be negative.\n";
($#ARGV==4) || die "ERROR: use DTPgen <k> <n> <m> <L> <seed>\n";

# initialise random seed

srand($seed);

# stamp the problem with the problem's parameters

print STDOUT "#\n";
print STDOUT "# A random Disjunctive Temporal Problem\n";
print STDOUT "#\n";
print STDOUT "# k = $k literals per clause,\n";
print STDOUT "# n = $n arithmetic vars,\n";
print STDOUT "# m = $m clauses,\n";
print STDOUT "# L = 100 is the coefficients' range,\n";
print STDOUT "# random seed is $seed\n";
print STDOUT "#\n";

# generate m constraints:

for ( $i=0; $i<$m; $i++ ) {
    # place an AND after every clause but the last one
    if ( $i!=0 ) {
        print STDOUT " ^\n";
    }
    # now generate six random numbers a,b,c,d,e,f such that
    do {
        # the variable indexes (a,b) differ,
        while ( ($a=int(rand()*$n)) == ($b=int(rand()*$n)) ) { }
        # the constant term c is chosen randomly in [-L,L]
        $c = int(rand()*$L);
        $neg = rand();
        if ($neg>= 0.5) {
            $c=$c*-1;
        }
        # ditto for the second relation
        while ( ($d=int(rand()*$n)) == ($e=int(rand()*$n)) ) { }
        $f = int(rand()*$L);
        $neg = rand();
        if ($neg>= 0.5) {
            $f=$f*-1;
        }
    } 
    # finally check that we haven't generated the same relation twice!
    while ( ($a==$d) && ($b==$e) && ($c==$f) );
      
    # write otu the relations as a binary clause
    print STDOUT "x$a-x$b<=$c v x$d-x$e<=$f";
}

print STDOUT "\n";
