示例#1
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
示例#2
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