def sat_time(G, CNF): """sat_time gives the running time of minisat on instance.cnf and whether it is satisfiable in the appropriate form for check_graph. """ _verbose("running " + _SOLVER + "...") start = tm.default_timer() ans = run_solver(CNF) running_time = tm.default_timer() - start return [("Running Time", running_time), ("Satisfiable", ans)]
def make_graph(vars, clas, grps, prob): """Makes a CNF clause (each clause with maximum 3 variables) out of VARS variables and CLAS clauses. Each variable can be in one of GRPS grous, which guide the CNF formula to have a community structure based on these groups. The Q-value of the corresponding graph is approximated by PROB. Returns (graph, CNF formula) as a tuple. """ _verbose("constructing CNF and incidence graph...") variables = [i for i in range(1, vars + 1)] grp_size = int(round(float(vars) / grps)) G = ig.Graph() G.add_vertices(vars) CNF = [] for i in range(clas): rand_var = variables[rm.randrange(vars)] rand_grp = (rand_var - 1) % grps var2 = var3 = 0 if _with_prob(prob): x = - 1 while (x < 0 or x >= vars): x = rm.randrange(grp_size + 1) * grps + rand_grp var2 = variables[x] else: var2 = variables[rm.randrange(vars)] if _with_prob(prob): x = -1 while (x < 0 or x >= vars): x = rm.randrange(grp_size + 1) * grps + rand_grp var3 = variables[x] else: var3 = variables[rm.randrange(vars)] clause = [] if _with_prob(.5): clause.append(rand_var) else: clause.append(-rand_var) if rand_var != var2: if _with_prob(.5): clause.append(var2) else: clause.append(-var2) if rand_var != var3 and var2 != var3: if _with_prob(.5): clause.append(var3) else: clause.append(-var3) G.add_edges([(rand_var - 1, var2 - 1), (rand_var - 1, var3 - 1), (var2 - 1, var3 - 1)]) CNF.append(clause) G.simplify() create_DIMACS(CNF, vars, clas) return (G, CNF)
def mod_check(G, CNF): """mod_check gives the modularity of the graph G as an attribute tuple for check graph. It is calculated by the provided library igraph. """ _verbose("calculating Q-value...") return [("Q-value", G.community_fastgreedy().as_clustering().q)]