def generate_random_graphs(self): """ Extend random graphs by generating larger versions provided in the Traces package. Random graphs are defined with edge probability 1/2, 1/10 and sqrt(n) :return: """ ph = ProcessHandler() instances = [ 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000, 10000, 20000, 30000 ] probabilities = ["1/2", "1/10", "sqrt"] names = ["ran2_custom", "ran10_custom", "ransq_custom"] for p, n in zip(probabilities, names): print p for i in instances: dest = "./../assets/graphs_custom/{0}/{1}".format(n, i) print dest p = "1/" + str(int(math.ceil(math.sqrt(float(i))))) print "./../assets/nauty26r7/genrang -P{0} {1} 1 {2}.g6".format( p, i, dest) ph.run_command( "./../assets/nauty26r7/genrang -P{0} {1} 1 {2}.g6".format( p, i, dest)) ph.run_command( "./../assets/nauty26r7/showg -d {0}.g6 {1}.dre".format( dest, dest))
def update_strongly_k(self, n, m, system): """ Update the folder of stored systems which are a catalogue of slow systems :param n: :param m: :param system: :return: """ ph = ProcessHandler() fh = FileHandler() path = "./../assets/systems_strongly_k/{0}_{1}".format(n, m) fh.delete_file(path) self.save_system(n, m, system, "./../assets/systems_strongly_k/")
def run_solver(self, **kwargs): """ Run Traces through systems and record times Looking for systems that are faster with gauss off => K-local consistent :param kwargs: :return: Null """ fh = FileHandler() ph = ProcessHandler() results = [] skip = kwargs.get("outstanding", False) completed = [] if fh.read_from_file("./../assets/systems_run/run"): for result in fh.read_from_file("./../assets/systems_run/run"): completed.append(result[0]) for filename in ph.run_command('ls -v ./../assets/systems/'): # Init path = './../assets/systems/' + filename system = fh.read_from_file(path) split = filename.split("_") n = split[0] m = split[1] # Skip completed systems key = "{0}:{1}".format(n, m) if skip and key in completed: continue print key # Create cryptominisat system time_off, time_on = self.get_gauss_times(n, m, system) # Save results.append([key, n, m, time_off, time_on, time_off - time_on]) fh.update_file("./../assets/systems_run/run", results)
def is_system_eligble(self, n, m, system, gi, location): """ Check if graph meets the construction criteria - k consistent - No automorphisms :param n: :param m: :param gi: :param system: :return: """ # Init ph = ProcessHandler() fh = FileHandler() graph_path = "{0}/constructions/".format(location) system_path = "{0}/constructions/{1}_{2}".format(location, n, m) construct_a_location = "{0}{1}_{2}_A.dre".format(graph_path, n, m) # Save system temporarily self.save_system(n, m, system, graph_path) G = self.convert_system_to_graph(n, m, system) gi.convert_graph_to_traces(n, m, G, "A", graph_path) # First construction # Check for k-local consistency if not self.is_k_consistent(n, m, system): # print "Not K consistent" fh.delete_file(system_path) return False elif not gi.graph_has_automorphisms(construct_a_location): # print "No Automorphisms. Construct." G = self.convert_system_to_construction(n, m, system) gi.convert_graph_to_traces(n, m, G, "B", graph_path) # Second construction fh.delete_file(system_path) return True else: # print "Automorphisms. Remove." fh.delete_file(construct_a_location) # Remove unwanted graph fh.delete_file(system_path) # Remove unwanted system return False
def convert_systems_to_constructions(self, **kwargs): """ Convert found systems into graphs and run them through Traces :return: """ # Init gi = Gi() sat = Sat() ph = ProcessHandler() fh = FileHandler() paths = ph.run_command("ls -v ./../assets/systems_to_convert/") validate = kwargs.get("validate", False) delete = kwargs.get("delete", False) # Iterate systems for path in paths: print "Checking " + path # Paths graph_path = "./../assets/construction/" + path + "_A.dre" system_path = "./../assets/systems_to_convert/" + path # Extract n and m values n, m = path.split("_") n = int(n) m = int(m) # Load system system = fh.read_from_file(system_path) if validate: # Check for k-local consistency if not sat.is_k_consistent(n, m, system): print "\t Not K consistent system. Removing and skipping." if delete: fh.delete_file(system_path) continue else: print "\t K consistent system. Constructing A." # Convert system into graphs and check for automorphisms G = sat.convert_system_to_graph(n, m, system) gi.convert_graph_to_traces( n, m, G, "A", "./../assets/construction/") # First construction if not gi.graph_has_automorphisms(graph_path): print "\t No Automorphisms. Constructing B." G = sat.convert_system_to_construction(n, m, system) gi.convert_graph_to_traces( n, m, G, "B", "./../assets/construction/") # Second construction if delete: fh.delete_file(graph_path) else: print "\t Automorphisms. Removing and skipping." if delete: fh.delete_file(graph_path) # Remove unwanted graph fh.delete_file(system_path) # Remove unwanted system else: G = sat.convert_system_to_construction(n, m, system) gi.convert_graph_to_traces(n, m, G, "B", "./../assets/construction/")
def generate_systems(self, **kwargs): """ Generate instances by searching through combinations of n and m Save these results as files :param kwargs: :return: Results of time taken to search """ # Init fh = FileHandler() ph = ProcessHandler() all_results = [['key', 'n', 'm', 'tries', 'vTime']] all_systems = [] # Looping params step = kwargs.get("step", 1) max_tries = kwargs.get("max_tries", 30) min_m = kwargs.get("min_m", 4) n = kwargs.get("n", 4) max_n = kwargs.get("max_n", 100) max_m = kwargs.get("max_m", 100) # Complex looping params efficient_search = kwargs.get("limited_search", False) limit = kwargs.get("limit", False) upper_bound = kwargs.get("upper_bound", 4) lower_bound = kwargs.get("lower_bound", 1) # Additional params save_results_dir = "./../assets/sat_run/{0}-n-{1}_{2}-m-{3}_step-{4}".format( n, max_n, min_m, max_m, step) save_results = kwargs.get("save_results", False) save_systems = kwargs.get("save_systems", False) gi = kwargs.get("gi", False) update_strongly_k = kwargs.get("update_strongly_k", False) # Prep results folder if save_results or save_systems or gi: save_results_dir = fh.makedir(save_results_dir) save_results_location = save_results_dir + "/results" save_systems_location = save_results_dir + "/systems/" save_constructions_location = save_results_dir + "/constructions/" fh.makedir(save_systems_location) fh.makedir(save_constructions_location) # Loop n value while n <= max_n: # Init loop tries = 0 found = 0 smallest_m_found = False n_results = [] n_systems = [] if min_m < n: # If m is smaller than n, then bring m up to speed m = lower_bound * n else: m = min_m # Loop m value while m <= max_m: # Handle Iterators if max_tries == tries: # Failed to find and tried too many times print "Skipping: {0} {1}".format(n, m) tries = 0 all_results.append([key, n, m, tries, -1, -1]) n_results.append([key, n, m, tries, -1, -1]) m += step continue elif m > (upper_bound * n) or (found and found == limit): # Do not search for m > 4n or continue to next m if adequate systems are found break # Generate random system and record time taken to find key = ` n ` + ':' + ` m ` validation_start = timeit.default_timer() generate_time, system = ph.run_function_timed( self.generate_rand_system, (n, m), return_args=True) # Validate system if self.is_system_uniquely_satisfiable(system, n) \ and ((gi and self.is_system_eligble(n, m, system, gi, save_results_dir)) or not gi) \ and ((update_strongly_k and self.is_system_slower(n, m, system)) or not update_strongly_k): # Found unique system print "Found: {0} {1}".format(n, m) # Record times validation_time = timeit.default_timer() - validation_start all_results.append( [key, n, m, tries, validation_time, generate_time]) n_results.append( [key, n, m, tries, validation_time, generate_time]) all_systems.append([key, n, m, system]) n_systems.append([key, n, m, system]) # Update iterators tries = 0 found += 1 if efficient_search and not smallest_m_found: # Update the lower bound min_m = m - step smallest_m_found = True if update_strongly_k: self.update_strongly_k(n, m, system) else: # Failed to find, try again # print 'Couldnt find for {0} {1} Misses {2}'.format(n, m, tries) tries += 1 m -= step # Increment m m += step # Save search information if save_results: self.save_results(n_results, save_results_location) if save_systems: self.save_results_systems(n_systems, save_systems_location) # Increment n n += step return all_results, all_systems