"""
This script is written for running Uwe Schöning's WalkSAT algorithm
    [Uwe Schöning:
    A probabilistic algorithm for k-SAT and constraint satisfaction problems.
    In: Proceedings of FOCS 1999, IEEE, pages 410–414.]
via the command line
by specifying the instance to solve, and the seed to use.
Uwe Schöning's WalkSAT algorithm can be seen as a special case of the probSAT algorithm
    [Adrian Balint, Uwe Schöning:
    Choosing Probability Distributions for Stochastic Local Search and the Role of Make versus Break.    
    In: Lecture Notes in Computer Science, 2012, Volume 7317, Theory and Applications of Satisfiability Testing - SAT 2012, pages 16-29.
    https://www.uni-ulm.de/fileadmin/website_uni_ulm/iui.inst.190/Mitarbeiter/balint/SAT2012.pdf]
with exponential function (break-only-exp-algorithm) and c_b = 1.

The script will record the number of flips, the time used, and the seed used.
"""

import argparse
from subprocess import Popen, PIPE
from time import sleep
from os.path import exists
import sys
from timeit import default_timer as timer


def run_alg(instance, seed):
    p = Popen(['./uweAlg', '--cb', '1.0', '--fct', str(1), instance, str(abs(seed))], stdin = PIPE, stdout = PIPE, stderr=PIPE)
    output, err = p.communicate()
    rc = p.returncode
    err = err.decode("utf-8")
    if err != "":
        print(err, file=sys.stderr)
    return output.decode("utf-8").replace(" 1\n","").replace("\n","")
    

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--seed", type=int, default=1909, help="The seed.")
    parser.add_argument("-i", "--instance", default='./instances/uf250-01.cnf', help="The instance.")

    args = parser.parse_args()

    start = timer()
    flips = run_alg(args.instance, args.seed)
    end = timer()
    print(flips, end-start, str(abs(args.seed)))


    
    

    
