Пример #1
0
 def load_results(self):
     """
     Load Sat Solver execution results
     :return: 
     """
     fh = FileHandler()
     results = fh.read_from_file("./../assets/systems_run/run")
     return results
Пример #2
0
 def plot_generate_systems_results(self, filename, **kwargs):
     """
     Plot time taken to generate instances
     :param filename: 
     :param kwargs: 
     :return: 
     """
     ph = PlotHandler()
     fh = FileHandler()
     data = fh.read_from_file(filename, **kwargs)
     ph.plot_sat_results(data)
Пример #3
0
 def save_system(self, n, m, system, location):
     """
     Save a system of equations to file
     :param n: 
     :param m: 
     :param system: 
     :param location: 
     :return: 
     """
     fh = FileHandler()
     path = location + "{0}_{1}".format(n, m)
     fh.write_to_file(path, system)
Пример #4
0
 def test_17(self):
     fh = FileHandler()
     data = fh.read_from_file(
         "../assets/results/sat/0-n-10000_0-m-10000_step-100/results")
     for d in data:
         print "{0}  {1} a".format(d[1], d[2])
     i = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
     gi = Gi()
     for j in i:
         a = gi.run_graph_instance("ga_vs_gb", "{0}_{1}_A.dre".format(j, j))
         b = gi.run_graph_instance("ga_vs_gb", "{0}_{1}_B.dre".format(j, j))
         print "{0}  {1} a".format(j, a["time"], 1)
         print "{0}  {1} b".format(j, b["time"], 1)
Пример #5
0
 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/")
Пример #6
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
Пример #7
0
    def is_k_consistent(self, n, m, system):
        """
        Looking for systems that are faster with gauss off => K-local consistent
              
        on vs off
        faster on versus off
        
        Looking for systems that are FASTER (take less time) with GAUSS ON than GAUSS OFF
        
        :param n: 
        :param m: 
        :param system: 
        :return: 
        """
        ph = ProcessHandler()
        fh = FileHandler()

        # Get times
        time_off, time_on = self.get_gauss_times(n, m, system)

        # If Gauss On - Gauss Off > Threshold (sec)
        # threshold = time_b - time_a > float(1)
        # I.e. time_off - time_on > 0
        threshold = time_on < time_off  # No threshold determined

        return threshold
Пример #8
0
 def save_results_systems(self, systems, location):
     fh = FileHandler()
     ph = ProcessHandler()
     print "Saving systems..."
     save_systems_time = ph.run_function_timed(self.save_systems,
                                               (systems, location))
     print "Time taken: ", save_systems_time
Пример #9
0
    def is_system_slower(self, n, m, system):
        """
        Determine if a given system is slower than the current slowest system stored in "systems_strongly_k"
        :param n: 
        :param m: 
        :param system: 
        :return: 
        """
        fh = FileHandler()
        path = "./../assets/systems_strongly_k/{0}_{1}".format(n, m)
        temp_path_a = "./../assets/temp/temp_a"
        temp_path_b = "./../assets/temp/temp_b"
        system_old = fh.read_from_file(path)

        if not system_old:
            return True

        fh.write_to_file_simple(
            temp_path_a, self.prepare_cryptominisat_system(n, m, system))
        fh.write_to_file_simple(
            temp_path_b, self.prepare_cryptominisat_system(n, m, system_old))
        diff_a = self.get_gauss_off_time(temp_path_a) - self.get_gauss_on_time(
            temp_path_a)
        diff_b = self.get_gauss_off_time(temp_path_b) - self.get_gauss_on_time(
            temp_path_b)

        if diff_a > diff_b:
            print "Slower {0}".format(diff_a - diff_b)
        pass

        return diff_a > diff_b
Пример #10
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
Пример #11
0
    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
Пример #12
0
    def convert_graph_to_traces(self, n, m, G, type, dir):
        """
        Convert a given networkx graph into dreadnaut format
        :param n: 
        :param m: 
        :param G: 
        :return: 
        """
        if type == "B":
            nodes = (2 * n) + (4 * m)
            variables = (2 * n)
        else:
            nodes = n + m
            variables = n

        # Init
        fh = FileHandler()
        path = dir + "{0}_{1}_{2}.dre".format(n, m, type)
        path_temp = "./../assets/temp/temp.adjlist"

        # Convert to Adjlist and store temporarily
        nx.write_adjlist(G, path_temp)

        # Read data and convert
        data = fh.read_from_file_simple(path_temp)
        output = ["n={0} $=0 g".format(nodes)]
        for i in range(3, variables + 3):
            datum = data[i].split()
            datum[0] = "{0}:".format(datum[0])
            output.append(" ".join(datum))
        output[-1] = "{}.".format(output[-1])
        output.append("$$")

        # Save as .dre
        fh.write_to_file_simple(path, output)

        # Convert to dot if necessary
        # ./nauty26r7/dretodot construction/3.dre construction/3.dot

        return path
Пример #13
0
    def get_configuration(self):
        _cached_content = RedisCacheAdapter.get(
            self.key, machine_alias=self.machine_alias
        )
        if _cached_content:
            self.value = _cached_content
            return self.value
        else:
            url = settings.WEATHER_API["URL"]
            params = {
                settings.WEATHER_API["PARAMETER_NAMES"]["latitude"]: settings.WEATHER_API["PARAMETER_VALUES"][
                    "latitude"],
                settings.WEATHER_API["PARAMETER_NAMES"]["longitude"]: settings.WEATHER_API["PARAMETER_VALUES"][
                    "longitude"],
                settings.WEATHER_API["PARAMETER_NAMES"]["count"]: settings.WEATHER_API["PARAMETER_VALUES"][
                    "count"],
                settings.WEATHER_API["PARAMETER_NAMES"]["secret_key"]: settings.WEATHER_API["PARAMETER_VALUES"][
                    "secret_key"]
            }

            r = requests.get(url=url, params=params)
            if r.status_code == 200:
                data = r.json()
                weather_data = data.get("list")

                self.value = weather_data
                self.set_configuration(weather_data)
                file_handler = FileHandler(data=weather_data)
                file_handler.delete_excel_file()
                file_handler.create_excel_file()
                return weather_data

            else:
                return []
Пример #14
0
 def save_results(self, results, location):
     """
     Save a set of results
     :param location: 
     :param results: 
     :return: 
     """
     fh = FileHandler()
     ph = ProcessHandler()
     print "Saving results..."
     save_results_time = ph.run_function_timed(fh.update_file,
                                               (location, results))
     print "Time taken: ", save_results_time
Пример #15
0
    def get_gauss_times(self, n, m, system):
        """
        Retrieve the validation times (unique satisfiability) on a given system executed on a Sat solver
        :param n: 
        :param m: 
        :param system: 
        :return: 
        """
        path = "./../assets/temp/temp_gauss_check"
        fh = FileHandler()

        # Create cryptominisat system
        input = self.prepare_cryptominisat_system(n, m, system)
        fh.write_to_file_simple(path, input)

        # run gauss off
        time_off = self.get_gauss_off_time(path)

        # run gauss on
        time_on = self.get_gauss_on_time(path)

        return time_off, time_on
Пример #16
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)
Пример #17
0
    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
Пример #18
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/")
Пример #19
0
    def execute_constructions(self):
        """
        Run new constructions through Traces
        :return: 
        """
        gi = Gi()
        fh = FileHandler()
        graphs = {"con_all": gi.load_graphs()["con_all"]}
        results = gi.run_graphs(graphs, save=True, timeout=7200)

        # Init
        con_4_10 = []
        con_10_100 = []
        con_100_1000 = []
        con_n = []
        con_2n = []
        con_3n = []
        con_sml = []
        found = []

        # Extract packages
        for result in results["con_all"]:
            n = int(result["name"].split("_")[0])
            m = int(result["name"].split("_")[1])
            if n in range(4, 10, 1):
                con_4_10.append(result)
            if n in range(10, 100, 10):
                con_10_100.append(result)
            if n in range(100, 1000, 100):
                con_100_1000.append(result)
            if n == m:
                con_n.append(result)
            if 2 * n == m:
                con_2n.append(result)
            if 3 * n == m:
                con_3n.append(result)

        # Extract smallest n : m ratio
        for i in results["con_all"]:
            n = int(i["name"].split("_")[0])
            m = int(i["name"].split("_")[1])

            if n in found:
                continue

            for j in results["con_all"]:
                n_1 = int(j["name"].split("_")[0])
                m_1 = int(j["name"].split("_")[1])
                if n == n_1 and m_1 <= m:
                    con_sml.append(j)
                    found.append(n)

        # Produce packages
        packages = {
            "con_4_10": con_4_10,
            "con_10_100": con_10_100,
            "con_100_1000": con_100_1000,
            "con_n": con_n,
            "con_2n": con_2n,
            "con_3n": con_3n,
            "con_sml": con_sml
        }

        # Save packages
        for package in packages:
            fh.write_to_file("./../assets/graphs_run/{0}.txt".format(package),
                             packages[package])
            fh.makedir("./../assets/graphs/{0}".format(package))
            for instance in packages[package]:
                name = instance["name"]
                fh.copy_file(
                    "./../assets/graphs/con_all/{0}".format(name),
                    "./../assets/graphs/{0}/{1}".format(package, name))

        return results