Beispiel #1
0
    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))
Beispiel #2
0
 def load_graphs(self):
     """
     Load graph instances from package
     :return: 
     """
     ph = ProcessHandler()
     graphs = {}
     for graph in ph.run_command('ls -v ./../assets/graphs/'):
         graphs[graph] = []
         for graph_instance in ph.run_command('ls -v ./../assets/graphs/' +
                                              graph):
             graphs[graph].append(graph_instance)
     return graphs
Beispiel #3
0
    def run_graph_instance(self, graph, graph_instance, **kwargs):
        """
        Run a specific instance on dreadnaut
        dreadnaut At -a V=0 -m <"[path]" x q
        :param graph: 
        :param graph_instance: 
        :param kwargs: 
        :return: 
        """
        # Init
        ph = ProcessHandler()
        path = "./../assets/graphs/" + graph + "/" + graph_instance
        is_dimacs = self.is_dimacs(graph)

        if is_dimacs:
            process = False
            nodes = ph.run_command("head '" + path + "'")[0].split(" ")[2]
            program = kwargs.get("program", "bliss")
        else:
            nodes = re.search("(n=?)=\d+", ' '.join(
                ph.run_command("head '" + path + "'"))).group(0)[2:]
            process = ph.open_process("dreadnaut")
            program = kwargs.get("program", "traces")

        command = self.get_command(program, path)

        # Set timeout (seconds)
        signal.signal(signal.SIGALRM, signal_handler)
        signal.alarm(kwargs.get("timeout", kwargs.get("timeout", 0)))

        # Gather results
        try:
            time, d_time = self.run_program(process, command, program)

        except TimeoutError:
            print "Timed out: Took too long to validate"
            time = -1
            d_time = -1
            if not is_dimacs:
                process.kill()
                process.terminate()
        finally:
            signal.alarm(0)

        return {
            "name": graph_instance,
            "nodes": nodes,
            "time": time,
            "d_time": d_time
        }
Beispiel #4
0
 def load_results(self):
     """
     Load results from tests
     :return: 
     """
     ph = ProcessHandler()
     fh = FileHandler()
     results = {}
     run = ph.run_command("ls -v ./../assets/graphs_run/")
     for graph_run in run:
         results[graph_run[:-4]] = fh.read_from_file(
             "./../assets/graphs_run/" + graph_run)
     return results
Beispiel #5
0
    def run_graph(self, graphs, graph, program, **kwargs):
        """
        Run instances in a graph
        :param graph: 
        :param kwargs: 
        :return: 
        """
        fh = FileHandler()
        ph = ProcessHandler()
        run = ph.run_command("ls -v ./../assets/graphs_run/")
        save = kwargs.get("save", False)
        outstanding = kwargs.get("outstanding", False)

        graph_name = self.get_filename(graph, program)

        # Load previous results
        if outstanding and graph_name in run:
            graph_results = fh.read_from_file(
                "./../assets/graphs_run/{0}".format(graph_name))
        else:
            graph_results = []

        # Gather results
        for graph_instance in graphs:
            print "{0} {1}".format(graph_instance, datetime.datetime.now())

            # Skip existing graph
            if outstanding and graph_name in run:
                if any(d['name'] == graph_instance for d in graph_results):
                    continue

            kwargs["program"] = program
            result = self.run_graph_instance(graph, graph_instance, **kwargs)

            graph_results.append(result)

            # Save
            if save:
                print "Saving..."
                fh.write_to_file("./../assets/graphs_run/" + graph_name,
                                 graph_results)

        return graph_results
Beispiel #6
0
    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)
Beispiel #7
0
    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/")