def test_multiple_handlers():
    """Test live plotting works with multiple handlers """

    seed = 42
    np.random.seed(seed)
    num_simulation_steps = 30
    time_interval = 1
    plot_frequency = 10

    hedgehog_simulator = build_default_simple_reentrant_line_simulator(seed)

    handlers = [
        hand.StateCostPlotter(num_simulation_steps,
                              time_interval,
                              plot_frequency,
                              testing_mode=True),
        hand.WorkloadPlotter(num_simulation_steps,
                             time_interval,
                             plot_frequency,
                             testing_mode=True)
    ]

    reporter = rep.Reporter(handlers=handlers)

    # Run Simulation
    with snc.simulation.plot.plotting_utils.non_gui_matplotlib():
        hedgehog_simulator.run(num_simulation_steps, reporter=reporter)

    for h in handlers:
        assert h.data_cache.shape[1] == num_simulation_steps
        assert not np.all(h.data_cache == np.zeros_like(h.data_cache))
def test_live_plotting_to_existing_axes():
    """Test live plotting works on existing axes."""

    seed = 42
    np.random.seed(seed)
    num_simulation_steps = 30
    time_interval = 1
    plot_frequency = 10

    hedgehog_simulator = build_default_simple_reentrant_line_simulator(seed)

    fig = plt.figure(figsize=(6, 6))
    ax1 = fig.add_subplot(111)

    handlers = [
        hand.StateCostPlotter(num_simulation_steps,
                              time_interval,
                              plot_frequency,
                              ax=ax1,
                              testing_mode=True)
    ]
    reporter = rep.Reporter(handlers=handlers)

    # Run Simulation
    with snc.simulation.plot.plotting_utils.non_gui_matplotlib():
        hedgehog_simulator.run(num_simulation_steps, reporter=reporter)

    for h in handlers:
        assert h.data_cache.shape[1] == num_simulation_steps
        assert not np.all(h.data_cache == np.zeros_like(h.data_cache))
Exemple #3
0
def run_simulations(
    num_sim: int, num_sim_steps: int, env: crw.ControlledRandomWalk,
    discount_factor: float
) -> Tuple[List[snc_types.ActionSpace], List[snc_types.StateSpace]]:
    """ Run multiple simulations on a given model and return all the actions and states.

    :param num_sim: The number of simulations to run.
    :param num_sim_steps: The number of simulation steps to run for each simulation.
    :param env: the environment to stepped through.
    :param discount_factor: discount factor used to compute the long term cost function.
    """
    data_actions = []  # type: List[snc_types.ActionSpace]
    data_states = []  # type: List[snc_types.StateSpace]

    num_steps = num_sim * num_sim_steps
    # Set Up Handlers
    handlers = [
        ProgressBarHandler(num_simulation_steps=num_steps, trigger_frequency=1)
    ]  # type: List[Handler]

    # Create Reporter
    reporter = rep.Reporter(handlers=handlers)  # fill with handlers

    # Agent parameters
    overrides: Dict[str, Dict[str, Union[str, float]]] = {}
    ac_params, wk_params, si_params, po_params, hh_params, name, include_hedging \
        = load_agents.get_hedgehog_hyperparams(**overrides)

    for i in np.arange(num_sim):
        job_gen_seed = int(42 + i)
        np.random.seed(job_gen_seed)

        # Create Policy Simulator
        agent = BigStepHedgehogAgent(env, discount_factor, wk_params,
                                     hh_params, ac_params, si_params,
                                     po_params, include_hedging, name)
        simulator = ps.SncSimulator(env,
                                    agent,
                                    discount_factor=discount_factor)

        # Run Simulation
        data = simulator.run(num_simulation_steps=num_sim_steps,
                             reporter=reporter,
                             job_gen_seed=job_gen_seed)

        data_actions.extend(data["action"])
        data_states.extend(data["state"])
    return data_actions, data_states
def run_policy(policy_simulator: ps.SncSimulator,
               num_simulation_steps: int,
               server_mode: bool,
               is_hedgehog: bool,
               save_location: str,
               job_gen_seed: Optional[int] = None):
    """
    Run a policy simulator, with a reporter, saving the result in given location.

    :param policy_simulator: the policy simulator object.
    :param num_simulation_steps: the number of steps the simulation runs.
    :param server_mode: when experiment runs locally, this flag controls whether to show live plots
        and wait for input before closing them at the end of the simulation.
    :param is_hedgehog: whether the policy simulator object is hedgehog specifically.
    :param save_location: what to name the data when it is saved in logs directory.
    :param job_gen_seed: Job generator random seed.
    :return: Run results - a dictionary including data on cost, state and action processes.
    """
    env = policy_simulator.env

    initial_state = env.state
    time_interval = env.job_generator.sim_time_interval

    min_draining_time = mdt.compute_minimal_draining_time_from_env_cvxpy(
        initial_state, env)
    print(
        f"Minimal draining time: {min_draining_time}\n"
        f"Minimal draining simulation steps: {min_draining_time * time_interval}"
    )

    plot_freq = 100
    is_routing = is_routing_network(env)
    handlers = validation_utils.get_handlers(server_mode, num_simulation_steps,
                                             plot_freq, time_interval,
                                             is_hedgehog, is_routing)
    reporter = rep.Reporter(handlers=handlers)

    return policy_simulator.run(num_simulation_steps, reporter, save_location,
                                job_gen_seed)
Exemple #5
0
def main():
    """
    Reproduce a single validation run, i.e., it plots all data in the live plot panel from data.
    It accepts two parameters by command line:
    --path: Mandatory string with path to the directory with all runs.
    --is_hedgehog: Optional. If passed it understands that the experiment in the path corresponds
        to Hedgehog, so it will reproduce variables from the workload space. Otherwise, it assumes
        it is not Hedgehog and plots a reduced panel.
    Example:
        python experiment/process_results/reproduce_validation_run.py --is_hedgehog
        --path path_to_data_folder
    """
    parsed_args = handle_results.parse_args()
    data_path = parsed_args.path
    is_hedgehog = parsed_args.is_hedgehog
    is_routing = parsed_args.is_routing

    data_dict_read_ok = False
    try:
        with open('{}/datadict.json'.format(data_path), 'r') as f:
            data_dict = json.load(f)
        data_dict_read_ok = True
    except IOError:
        print(f'Cannot open {data_path}')

    params_read_ok = False
    try:
        with open('{}/parameters.json'.format(data_path), 'r') as f:
            params = json.load(f)
        params_read_ok = True
    except IOError:
        print(f'Cannot open {data_path}')

    if data_dict_read_ok and params_read_ok:
        n = len(data_dict["state"])
        time_interval = params["env"]["_job_generator"]["sim_time_interval"]
    else:
        print('Not possible to continue.')
        sys.exit()  # Exit script.

    handlers = validation_utils.get_handlers(False,
                                             n,
                                             n - 1,
                                             time_interval,
                                             is_hedgehog,
                                             is_routing,
                                             reproduce_mode=True)
    reporter = rep.Reporter(handlers=handlers)
    reporter._cache = params

    if is_hedgehog:
        strategic_idling_tuple_read_ok = False
        try:
            with open(
                    '{}/reporter/strategic_idling_tuple.json'.format(
                        data_path), "r") as f:
                sub = json.load(f)
            strategic_idling_tuple_read_ok = True
        except IOError:
            print(f'Cannot open {data_path}')
        if strategic_idling_tuple_read_ok:
            for k, v in sub.items():
                sub[k] = np.array(v)
            reporter.cache["strategic_idling_tuple"] = sub

        try:
            with open('{}/reporter/w.json'.format(data_path), "r") as f:
                reporter.cache.update(json.load(f))
        except IOError:
            print(f'Cannot open {data_path}')

        try:
            with open('{}/reporter/num_mpc_steps.json'.format(data_path),
                      "r") as f:
                reporter.cache.update(json.load(f))
        except IOError:
            print(f'Cannot open {data_path}')

        try:
            with open('{}/reporter/horizon.json'.format(data_path), "r") as f:
                reporter.cache.update(json.load(f))
        except IOError:
            print(f'Cannot open {data_path}')

    reporter.report(data_dict, 0)
    reporter.report(data_dict, n - 1)

    # Keep the window open
    plt.ioff()
    plt.show()