def test_get_horizon():
    env = examples.double_reentrant_line_model()
    discount_factor = 0.9999
    overrides = {
        'BigStepPenaltyPolicyParams': {
            'boolean_action_flag': True,  # This should throw an error.
            'convex_solver': 'cvx.CPLEX'
        },
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepPolicy',
            'horizon_mpc_ratio': 0,
            'minimum_horizon': 1
        }
    }
    ac_params, wk_params, si_params, po_params, hh_params, _, dp_params, _ \
        = get_hedgehog_hyperparams(**overrides)
    pf_mip_agent = PureFeedbackMIPHedgehogAgent(
        env,
        discount_factor,
        wk_params,
        hh_params,
        ac_params,
        si_params,
        po_params,
        demand_planning_params=dp_params)
    assert pf_mip_agent.get_horizon() == 1
Esempio n. 2
0
def test_get_hedgehog_hyperparams_overwrite_multiple():
    overrides = {
        'BigStepPenaltyPolicyParams': {
            'boolean_action_flag': False,
            'convex_solver': 'fake_solver',
            'safety_penalty_coeff': 21,
            'nonidling_penalty_coeff': 12
        },
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepPenaltyPolicy',
            'horizon_drain_time_ratio': 0.99,
            'horizon_mpc_ratio': 0.55,
            'minimum_horizon': 0.77,
            'mpc_policy_class_name': 'fake_mpc_class',
            'theta_0': 99
        },
        'name': 'funny_name'
    }
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    po_params = BigStepPenaltyPolicyParams(**overrides['BigStepPenaltyPolicyParams'])
    hh_params = HedgehogHyperParams(**overrides['HedgehogHyperParams'])
    name = overrides['name']
    ac_params, wk_params, si_params, _, _, si_class, dp_params, _ \
        = get_hedgehog_default_values('BigStepPenaltyPolicy')
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 3
0
def test_get_hedgehog_hyperparams_overwrite_name():
    overrides = {'name': "funny_name"}
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    name = overrides['name']
    ac_params, wk_params, si_params, po_params, hh_params, si_class, dp_params, _ \
        = get_hedgehog_default_values('BigStepLayeredPolicy')
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 4
0
def test_get_hedgehog_hyperparams_overwrite_asymptotic_cov():
    overrides = {'AsymptoticCovarianceParams': {'num_presimulation_steps': 2, 'num_batch': 1}}
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    ac_params = AsymptoticCovarianceParams(**overrides['AsymptoticCovarianceParams'])
    _, wk_params, si_params, po_params, hh_params, si_class, dp_params, name \
        = get_hedgehog_default_values()
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params,
                                     po_params, hh_params, si_class, dp_params, name)
Esempio n. 5
0
def test_get_hedgehog_hyperparams_overwrite_workload():
    overrides = {'WorkloadRelaxationParams': {'num_vectors': 2, 'load_threshold': 0.1,
                                              'convex_solver': 'fake_solver'}}
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    wk_params = WorkloadRelaxationParams(**overrides['WorkloadRelaxationParams'])
    ac_params, _, si_params, po_params, hh_params, si_class, dp_params, name \
        = get_hedgehog_default_values()
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 6
0
    def test_initialised_si_object(self):
        env = self.get_klimov_environment()

        ac_params, wk_params, si_params, po_params, hh_params, _, dp_params, name \
            = get_hedgehog_hyperparams()

        agent = BigStepHedgehogGTOAgent(env, 1, wk_params, hh_params,
                                        ac_params, po_params, dp_params, name)
        agent.perform_offline_calculations()

        assert isinstance(agent.strategic_idling_object, StrategicIdlingGTO)
Esempio n. 7
0
def test_get_hedgehog_hyperparams_overwrite_demand_planning_params():
    overrides = {
        'DemandPlanningParams': {
            'demand_planning_class_name': 'fake_class',
            'params_dict': {'fake_key_1': 1, 'fake_key_2': 2}
        }
    }
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    dp_params = DemandPlanningParams(**overrides['DemandPlanningParams'])
    ac_params, wk_params, si_params, po_params, hh_params, si_class, _, name \
        = get_hedgehog_default_values()
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 8
0
def test_get_hedgehog_hyperparams_overwrite_big_step_layered_policy():
    overrides = {
        'BigStepLayeredPolicyParams': {
            'convex_solver': 'fake_solver'
        },
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepLayeredPolicy'
        }
    }
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    po_params = BigStepLayeredPolicyParams(**overrides['BigStepLayeredPolicyParams'])
    ac_params, wk_params, si_params, _, hh_params, si_class, dp_params, name \
        = get_hedgehog_default_values('BigStepLayeredPolicy')
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 9
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
Esempio n. 10
0
def test_get_hedgehog_hyperparams_overwrite_strategic_idling():
    overrides = {'StrategicIdlingParams': {
        'strategic_idling_class': 'StrategicIdlingHedgehogNaiveGTO',
        'convex_solver': 'fake_solver',
        'epsilon': 5e6,
        'shift_eps': 0.33,
        'hedging_scaling_factor': 30,
        'penalty_coeff_w_star': 3}}
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    si_params = StrategicIdlingParams(**overrides['StrategicIdlingParams'])
    si_class = load_agents.get_strategic_idling_class(
        cast(StrategicIdlingParams, si_params).strategic_idling_class)
    ac_params, wk_params, _, po_params, hh_params, _, dp_params, name \
        = get_hedgehog_default_values()
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
def build_default_simple_reentrant_line_simulator(seed):
    """
    Helper function that returns a simulator to be used by the tests below.
    """

    env = examples.simple_reentrant_line_model(job_gen_seed=seed)
    overrides = {}
    ac_params, wk_params, si_params, po_params, hh_params, si_class, dp_params, name \
        = load_agents.get_hedgehog_hyperparams(**overrides)

    # Create Policy Simulator
    discount_factor = 0.95
    agent = BigStepHedgehogAgent(env, discount_factor, wk_params, hh_params,
                                 ac_params, si_params, po_params, si_class,
                                 dp_params, name)
    return ps.SncSimulator(env, agent, discount_factor=discount_factor)
Esempio n. 12
0
def test_get_hedgehog_hyperparams_overwrite_hedgehog_hyperparams():
    overrides = {
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepPenaltyPolicy',
            'horizon_drain_time_ratio': 0.99,
            'horizon_mpc_ratio': 0.55,
            'minimum_horizon': 0.77,
            'mpc_policy_class_name': 'fake_mpc_class',
            'theta_0': 0.12
        }
    }
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    hh_params = HedgehogHyperParams(**overrides['HedgehogHyperParams'])
    ac_params, wk_params, si_params, po_params, _, si_class, dp_params, name \
        = get_hedgehog_default_values('BigStepPenaltyPolicy')
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
Esempio n. 13
0
def construct_env_and_big_step_hedgehog_agent(override_dp_params):
    env = examples.simple_reentrant_line_with_demand_model(job_gen_seed=42)
    ac_params, wk_params, si_params, po_params, hh_params, si_class, dp_params, name \
        = get_hedgehog_hyperparams(**override_dp_params)
    agent = BigStepHedgehogAgent(
        env,
        0.9999,
        wk_params,
        hh_params,
        ac_params,
        si_params,
        po_params,
        si_class,
        dp_params,
        name
    )
    return env, agent
Esempio n. 14
0
def test_get_hedgehog_hyperparams_overwrite_big_step_penalty_policy():
    overrides = {
        'BigStepPenaltyPolicyParams': {
            'convex_solver': 'fake_solver',
            'boolean_action_flag': True,
            'safety_penalty_coeff': 33,
            'nonidling_penalty_coeff': 55
        },
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepPenaltyPolicy'
        }
    }
    updated_params = load_agents.get_hedgehog_hyperparams(**overrides)
    po_params = BigStepPenaltyPolicyParams(**overrides['BigStepPenaltyPolicyParams'])
    ac_params, wk_params, si_params, _, hh_params, si_class, dp_params, name \
        = get_hedgehog_default_values('BigStepPenaltyPolicy')
    assert_equal_hedgehog_parameters(updated_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)
def test_get_horizon():
    env = examples.double_reentrant_line_model()
    discount_factor = 0.9999
    overrides = {
        'HedgehogHyperParams': {
            'horizon_mpc_ratio': 0,
            'minimum_horizon': 1
        }
    }
    ac_params, wk_params, si_params, _, hh_params, _, _, dp_params \
        = get_hedgehog_hyperparams(**overrides)
    pf_mip_agent = PureFeedbackStationaryHedgehogAgent(
        env,
        discount_factor,
        wk_params,
        hh_params,
        ac_params,
        si_params,
        demand_planning_params=dp_params)
    assert pf_mip_agent.get_horizon() == 1
def test_scenario(scenario_name, agent_class):
    """ Run a brief integration test on a given scenario
    """
    skip_tests = SKIPPED_TESTS + PULL_MODELS + PUSH_PULL_MODELS
    if scenario_name in skip_tests:
        pytest.skip()

    np.random.seed(SEED_NO)

    _, env = scenarios.load_scenario(scenario_name, SEED_NO)
    # Update parameters for quick tests.
    overrides = {
        "HedgehogHyperParams": {
            "theta_0": 0.5,
            "horizon_drain_time_ratio": 0.1,
            "horizon_mpc_ratio": 0.1,
            "minimum_horizon": 10
        },
        "AsymptoticCovarianceParams": {
            "num_presimulation_steps": 100,
            "num_batch": 20
        }
    }

    if scenario_name in MIP_REQUIRED_MODELS:
        overrides["HedgehogHyperParams"]["mpc_policy_class_name"] = "FeedbackMipFeasibleMpcPolicy"

    ac_params, wk_params, si_params, po_params, hh_params, si_class, dp_params, name \
        = load_agents.get_hedgehog_hyperparams(**overrides)
    discount_factor = 0.95
    if agent_class == BigStepHedgehogAgent:
        agent = agent_class(env, discount_factor, wk_params, hh_params, ac_params,
                            si_params, po_params, si_class, dp_params, name)
    elif agent_class == BigStepHedgehogGTOAgent:
        agent = agent_class(env, discount_factor, wk_params, hh_params, ac_params,
                            po_params, dp_params, name)
    else:
        assert False, f"Not recognised agent: {agent_class}"
    simulator = ps.SncSimulator(env, agent, discount_factor=discount_factor)
    simulator.run(num_simulation_steps=SIM_STEPS)
def test_create_agent_with_mip_flag_set_to_false():
    env = examples.double_reentrant_line_model()
    discount_factor = 0.9999
    overrides = {
        'BigStepPenaltyPolicyParams': {
            'boolean_action_flag': False,  # This should throw an error.
            'convex_solver': 'cvx.CPLEX'
        },
        'HedgehogHyperParams': {
            'activity_rates_policy_class_name': 'BigStepPolicy'
        }
    }
    ac_params, wk_params, si_params, po_params, hh_params, _, dp_params, _ \
        = get_hedgehog_hyperparams(**overrides)
    with pytest.raises(AssertionError):
        _ = PureFeedbackMIPHedgehogAgent(env,
                                         discount_factor,
                                         wk_params,
                                         hh_params,
                                         ac_params,
                                         si_params,
                                         po_params,
                                         demand_planning_params=dp_params)
Esempio n. 18
0
def test_get_hedgehog_hyperparams_default_values():
    default_params = load_agents.get_hedgehog_hyperparams()
    ac_params, wk_params, si_params, po_params, hh_params, si_class, dp_params, name \
        = get_hedgehog_default_values()
    assert_equal_hedgehog_parameters(default_params, ac_params, wk_params, si_params, po_params,
                                     hh_params, si_class, dp_params, name)