Exemple #1
0
def test_regression_deepcopy():
    initial_state = {'state_a': 0}

    state_update_blocks = [
        {
            'policies': {},
            'variables': {
                'state_a': update_state_a
            }
        },
    ]

    params = {}

    TIMESTEPS = 10
    RUNS = 1

    model = Model(initial_state=initial_state,
                  state_update_blocks=state_update_blocks,
                  params=params)
    simulation = Simulation(model=model)
    experiment = Experiment(simulation)

    simulation.run()
    _ = copy.deepcopy(simulation)
Exemple #2
0
def test_to_HDF5():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = basic.TIMESTEPS
    RUNS = basic.RUNS

    model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)

    HDF5_store_file = 'experiment_results.hdf5'
    
    experiment = Experiment(simulations=[simulation])
    experiment.after_experiment = lambda experiment: save_to_HDF5(experiment, HDF5_store_file, 'experiment_0')

    raw_result = experiment.run()

    df = pd.read_hdf(HDF5_store_file, 'experiment_0')
    assert df.equals(pd.DataFrame(raw_result))

    experiment = Experiment(simulations=[simulation])
    experiment.after_experiment = lambda experiment: save_to_HDF5(experiment, HDF5_store_file, 'experiment_1')

    raw_result = experiment.run()

    df = pd.read_hdf(HDF5_store_file, 'experiment_1')
    assert df.equals(pd.DataFrame(raw_result))
    
    assert len(raw_result) > 0
    assert raw_result == experiment.results
    assert simulation.run() == raw_result
Exemple #3
0
def test_policy_result_type_error():
    initial_state = {
        'state_a': 0
    }

    state_update_blocks = [
        {
            'policies': {
                'p1': policy_function_invalid_result
            },
            'variables': {
                'state_a': update_state_a
            }
        },
    ]

    params = {}

    TIMESTEPS = 10
    RUNS = 1

    model = Model(initial_state=initial_state, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)

    with pytest.raises(Exception) as e:
        result = simulation.run()
    assert str(e.value) == "Failed to extract policy function result as dictionary"
Exemple #4
0
def test_policy_exception():
    initial_state = {
        'state_a': 0
    }

    state_update_blocks = [
        {
            'policies': {
                'p1': policy_function_raises
            },
            'variables': {
                'state_a': update_state_a
            }
        },
    ]

    params = {}

    TIMESTEPS = 10
    RUNS = 1

    model = Model(initial_state=initial_state, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)

    with pytest.raises(Exception) as e:
        result = simulation.run()
    assert str(e.value) == "Forced exception from policy function"
Exemple #5
0
def test_regression_policy_signals():
    initial_state = {'a': 0, 'b': 0}

    state_update_blocks = [
        {
            'policies': {},
            'variables': {
                'a': update_a_from_signal,
                'b': update_b_from_signal
            }
        },
        {
            'policies': {
                'policy': policy_two_signals
            },
            'variables': {
                'a': update_a_from_signal,
                'b': update_b_from_signal
            }
        },
    ]

    params = {}

    TIMESTEPS = 10
    RUNS = 1

    model = Model(initial_state=initial_state,
                  state_update_blocks=state_update_blocks,
                  params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    result = simulation.run()

    df = pd.DataFrame(result)
    assert df.query('timestep == 10 and substep == 2')['a'].item() == 10
Exemple #6
0
def test_paralell_state_update():
    def update_a(params, substep, state_history, previous_state, policy_input):
        if previous_state['timestep'] == 1: assert previous_state['b'] == 0
        return 'a', previous_state['a'] + 1

    def update_b(params, substep, state_history, previous_state, policy_input):
        if previous_state['timestep'] == 1: assert previous_state['a'] == 0
        return 'b', previous_state['b'] + 1

    initial_state = {'a': 0, 'b': 0}

    state_update_blocks = [
        {
            'policies': {},
            'variables': {
                'a': update_a,
                'b': update_b
            }
        },
    ]

    params = {}

    TIMESTEPS = 1
    RUNS = 1

    model = Model(initial_state=initial_state,
                  state_update_blocks=state_update_blocks,
                  params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    result = simulation.run()
    df = pd.DataFrame(result)
def test_state_history():
    initial_state = {'a': 1, 'b': 1}

    state_update_blocks = [
        {
            'policies': {},
            'variables': {
                'a': update_a
            }
        },
        {
            'policies': {},
            'variables': {
                'a': update_a
            }
        },
    ]

    model = Model(initial_state=initial_state,
                  state_update_blocks=state_update_blocks,
                  params={})
    simulation = Simulation(model=model, timesteps=10)

    result = simulation.run()

    assert True
Exemple #8
0
def test_run():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = basic.TIMESTEPS
    RUNS = basic.RUNS

    model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    experiment = Experiment(simulations=[simulation])
    
    raw_result = experiment.run()
    
    assert len(raw_result) > 0
    assert raw_result == experiment.results
    assert simulation.run() == raw_result
def test_base_results():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = basic.TIMESTEPS
    RUNS = basic.RUNS

    model = Model(initial_state=states, state_update_blocks=state_update_blocks, params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    experiment = Experiment([simulation])
    
    simulation_results = simulation.run()
    experiment_results = experiment.run()

    # Check Executable results & exceptions
    assert simulation_results == experiment_results
    assert simulation.results == experiment.results
    assert simulation.exceptions == experiment.exceptions
Exemple #10
0
def test_initial_state_and_previous_state():
    model = Model(initial_state=initial_state,
                  state_update_blocks=state_update_blocks)
    simulation = Simulation(model=model, timesteps=10, runs=1)
    simulation.engine = Engine(backend=Backend.SINGLE_PROCESS)

    result = simulation.run()
    df = pd.DataFrame(result)

    # Check that the initial state transition is correct
    assert timesteps[:4] == [
        0,  # Timestep 1; substep 1
        1,  # Timestep 1; substep 2
        1,  # Timestep 1; substep 3
        1  # Timestep 2; substep 1
    ]
    assert substeps[:4] == [
        0,  # Timestep 1; substep 1
        1,  # Timestep 1; substep 2
        2,  # Timestep 1; substep 3
        3  # Timestep 2; substep 1
    ]

    # Check that the previous state is correctly passed to next timestep/substep
    assert timesteps[len(timesteps) - 4:] == [
        9,  # Timestep 9; substep 3
        9,  # Timestep 10; substep 1
        10,  # Timestep 10; substep 2
        10  # Timestep 10; substep 3
    ]
    assert substeps[len(substeps) - 4:] == [
        2,  # Timestep 9; substep 3
        3,  # Timestep 10; substep 1
        1,  # Timestep 10; substep 2
        2  # Timestep 10; substep 3
    ]

    assert df.query("timestep == 0")["substep"].iloc[0] == 0
    assert df.query("timestep == 1")["substep"].iloc[0] == df.query(
        "timestep == 2")["substep"].iloc[0]
    assert df.query("timestep == 9")["substep"].iloc[0] == df.query(
        "timestep == 10")["substep"].iloc[0]
Exemple #11
0
def test_model_generator():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = 10
    RUNS = basic.RUNS

    model = Model(initial_state=states,
                  state_update_blocks=state_update_blocks,
                  params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    experiment = Experiment(simulations=[simulation])

    raw_result_experiment = experiment.run()
    # The Model generator doesn't handle parameter sweeps, and only executes one run
    df_experiment = pd.DataFrame(raw_result_experiment).query(
        'run == 1 and subset == 0')

    assert len(raw_result_experiment) > 0
    assert raw_result_experiment == experiment.results
    assert simulation.run() == raw_result_experiment

    # Create a generator from the Model instance
    model_generator = iter(model)
    raw_result_model = []
    # Set initial state
    raw_result_model.append(model.state)

    # Emulate the behaviour of the radCAD Engine
    for t in range(TIMESTEPS):
        _model = next(model_generator)
        raw_result_model.append(_model.substeps)

    # Flatten the results
    raw_result_model = utils.flatten(raw_result_model)
    df_model = pd.DataFrame(raw_result_model)

    assert_frame_equal(df_experiment, df_model)
Exemple #12
0
def test_run():
    states = basic.states
    state_update_blocks = basic.state_update_blocks
    params = basic.params
    TIMESTEPS = 10
    RUNS = 1

    model = Model(initial_state=states,
                  state_update_blocks=state_update_blocks,
                  params=params)
    simulation = Simulation(model=model, timesteps=TIMESTEPS, runs=RUNS)
    experiment = Experiment(simulations=[simulation])
    experiment.engine = Engine(drop_substeps=True)

    drop_substeps_result = pd.DataFrame(experiment.run())

    simulation_result = pd.DataFrame(simulation.run())
    keep = (simulation_result.substep == simulation_result['substep'].max())
    keep |= (simulation_result.substep == 0)
    simulation_result = simulation_result.loc[keep]

    assert simulation_result.reset_index(drop=True).equals(
        drop_substeps_result.reset_index(drop=True))
Exemple #13
0
OO........O...O.OO....O.O
..........O.....O.......O
...........O...O
............OO
'''

cells_to_board(cells)

model.initial_state = {
    "full": full,
    "board": board,
}
simulation.model = model

if __name__ == "__main__":
    result = simulation.run()

    df = pd.DataFrame(result)
    df

    fig = plt.figure(figsize=(8, 8))
    fig.set_tight_layout(True)
    fig = plt.figure()

    state = df.iloc[0]
    im = plt.imshow(state["board"], cmap="binary", interpolation="none")
    plt.axis("off")

    def animate_func(timestep):
        state = df.iloc[timestep]
        im.set_array(state["board"])