Exemple #1
0
 def test_srea_case_uniform_2(self):
     """Test two agent sync with uniform dists, no solution."""
     stn = stntools.load_stn_from_json_file(STN4)["stn"]
     result = srea.srea(stn)
     # We expect this one to never give a guide. It's impossible
     # to succeed, and thus inconsistent.
     self.assertEqual(result, None)
Exemple #2
0
 def test_srea_sim_1(self):
     stn = stntools.load_stn_from_json_file(STN1)["stn"]
     sim = Simulator(42)
     successes = 0
     res = 50
     for i in range(res):
         result = sim.simulate(stn, execution_strat="srea")
         if result:
             successes += 1
     robustness = successes / res
     self.assertTrue(0.60 < robustness < 0.80)
Exemple #3
0
 def test_early_sim(self):
     stn = stntools.load_stn_from_json_file(STN1)["stn"]
     sim = Simulator(42)
     successes = 0
     res = 100
     for i in range(res):
         result = sim.simulate(stn, execution_strat="early")
         if result:
             successes += 1
     robustness = successes / res
     self.assertTrue(0.10 < robustness < 0.20)
Exemple #4
0
 def test_srea_case_uniform_1(self):
     """Test two agent sync with uniform dists, easy solution."""
     stn = stntools.load_stn_from_json_file(STN3)["stn"]
     alpha, guide = srea.srea(stn)
     self.assertEqual(guide.get_assigned_time(1), 0.0)
     self.assertEqual(alpha, 0.0)
Exemple #5
0
 def test_srea_case_2(self):
     stn = stntools.load_stn_from_json_file(STN2)["stn"]
     alpha, guide = srea.srea(stn)
     self.assertEqual(guide.get_assigned_time(1), 0.0)
     self.assertEqual(alpha, 0.481)
Exemple #6
0
 def test_srea_case_1(self):
     stn = stntools.load_stn_from_json_file(STN1)["stn"]
     alpha, guide = srea.srea(stn)
     self.assertTrue(0.504 < alpha < 0.508)
Exemple #7
0
def across_paths(stn_paths,
                 execution,
                 threads,
                 sim_count,
                 sim_options,
                 output=None,
                 live_updates=True,
                 random_seed=None,
                 mitparse=False,
                 start_index=0,
                 stop_index=None,
                 ordering_pairs=None):
    """Runs multiple simulations for each STN in the provided iterable.

    Args:
        stn_paths (iterable): iterable (like a List) of strings.
        execution (str): Execution strategy to use on each STN.
        threads (int): Number of threads to use.
        sim_count (int): Number of simulations (samples) to use.
        sim_options (dict): Dictionary of simulation options to use.
        output (str, optional): Output file path. Default no output.
        live_updates (boolean, optional): Whether to provide live updates.
        random_seed (int, optional): The random seed to start out with,
            defaults to a random... random seed.
        mitparse (boolean, optional): Parse STN JSON files as MIT format.
        ordering_pairs (list, optional): List of tuples of AR and SC settings.
            Each STN will be run with a separate simulation for each tuple.
    """
    stn_pairs = []
    # Collect the STNs from all the passed in paths
    # Make sure we keep the path around though, and keep them in the pair.
    for i, path in enumerate(stn_paths):
        if mitparse:
            mitstns = mitparser.mit2stn(path, add_z=True, connect_origin=True)
            stn_pairs += [(path, k) for k in mitstns]
        else:
            stn = load_stn_from_json_file(path)["stn"]
            stn_pairs.append((path, stn))

    # We must separate these for loops because MIT stns can hold several
    # instances in a single file.
    for i, pair in enumerate(stn_pairs):
        if i < start_index:
            continue
        if stop_index is not None:
            if i >= stop_index:
                break
        if ordering_pairs is not None:
            for j, execution_setting in enumerate(ordering_pairs):
                sim_option_instance = sim_options.copy()
                sim_option_instance["ar_threshold"] = execution_setting[0]
                sim_option_instance["si_threshold"] = execution_setting[1]
                results_dict = _run_stage(pair, execution, sim_count, threads,
                                          random_seed, sim_option_instance)
                if live_updates:
                    _print_results(results_dict,
                                   j + len(ordering_pairs) * i + 1,
                                   len(stn_pairs) * len(ordering_pairs))

                if output is not None:
                    sim2csv.save_csv_row(results_dict, output)

        else:
            results_dict = _run_stage(pair, execution, sim_count, threads,
                                      random_seed, sim_options)
            if live_updates:
                _print_results(results_dict, i + 1, len(stn_pairs))

            if output is not None:
                sim2csv.save_csv_row(results_dict, output)
Exemple #8
0
 def test_stn_interagent_edges(self):
     stn = stntools.load_stn_from_json_file(STN1)["stn"]
     self.assertEqual(list(stn.interagent_edges.keys()), [(2, 4)])
     self.assertTrue(True)
Exemple #9
0
 def test_decouple_sim_3(self):
     stn = stntools.load_stn_from_json_file(STN3)["stn"]
     sim = DecoupledSimulator(random_seed=1000)
     self.assertTrue(sim.simulate(stn))
Exemple #10
0
 def test_opt_case_2(self):
     stn = stntools.load_stn_from_json_file(STN2)["stn"]
     alpha, subproblems = optdecouple.decouple_agents(stn, fidelity=0.001)
     self.assertTrue(0.0 <= alpha <= 0.01)
     for g in subproblems:
         self.assertEqual(g.get_assigned_time(0), 0)