import os 
import shutil
import time

from docplex.mp.error_handler import DOcplexException

from random_lp.random_qp import RandomQP

DIR = 'TEST_DATA' + "/" + time.strftime("%d_%m_%Y")


def getPath(filename = "", directory = ""):
    return DIR + "/" + directory + "/" + filename

DIR

shutil.rmtree(getPath(directory = "SPARSE"), ignore_errors=True)

os.makedirs(getPath(directory = "SPARSE"))

# create sparse random binary quadratic Programs
# 3 variables with 2 constraints each
max_qubits = 290
var = 3
cstr = 2

multiple = 10
while True:
    qp_bin = RandomQP.create_random_binary_prog("test_sparse_" + str(multiple), cstr, var, multiple=multiple)   
    try:         
        qp_bin.write_to_lp_file(getPath(qp_bin.name, directory = "SPARSE"))
        if qp_bin.complexity() > max_qubits :
            print(multiple)
            break
        if qp_bin.complexity() > 100 :
            multiple = multiple + 6
        else:
            multiple = multiple + 3
    except DOcplexException as ex:
        print(ex)
        print(qp_bin.complexity())
    

qp_bin.qubo.to_docplex().prettyprint()

