Esempio n. 1
0
def test_complex_topology() -> None:
    # make the experiment
    outer_path = ('universe', 'agent')
    pq = PoQo({})
    pq_composite = pq.generate(path=outer_path)
    pq_composite.pop('_schema')
    experiment = Engine(composite=pq_composite)

    # get the initial state
    initial_state = experiment.state.get_value()
    print('time 0:')
    pp(initial_state)

    # simulate for 1 second
    experiment.update(1)

    next_state = experiment.state.get_value()
    print('time 1:')
    pp(next_state)

    # pull out the agent state
    initial_agent_state = initial_state['universe']['agent']
    agent_state = next_state['universe']['agent']

    assert agent_state['aaa']['a1'] == initial_agent_state['aaa']['a1'] + 1
    assert agent_state['aaa']['x'] == initial_agent_state['aaa']['x'] - 9
    assert agent_state['ccc']['a3'] == initial_agent_state['ccc']['a3'] + 1
Esempio n. 2
0
def run_burst():
    out_dir = os.path.join(PROCESS_OUT_DIR, NAME)
    os.makedirs(out_dir, exist_ok=True)
    output = test_burst()
    pp(output)
Esempio n. 3
0
def test_burst():
    agent_1_id = '1'
    agent_2_id = '2'

    # initial state
    initial_a = 10
    initial_state = {
        'concentrations': {
            'A': initial_a
        },
        'agents': {
            agent_1_id: {
                'trigger': False,
                'concentrations': {
                    'A': 0
                },
                'agents': {
                    agent_2_id: {
                        'trigger': False,
                        'concentrations': {
                            'A': 0
                        },
                    }
                },
            }
        }
    }

    # timeline triggers burst for agent_s
    time_burst = 3
    time_total = 5
    timeline = [(0, {
        ('agents', agent_1_id, 'agents', agent_2_id, 'trigger'): False
    }),
                (time_burst, {
                    ('agents', agent_1_id, 'agents', agent_2_id, 'trigger'):
                    True
                }), (time_total, {})]

    # declare the hierarchy
    hierarchy = {
        COMPOSER_KEY: [{
            'type': TimelineProcess,
            'config': {
                'timeline': timeline
            },
            'topology': {
                'global': ('global', ),
                'agents': ('agents', )
            }
        }],
        'agents': {
            agent_1_id: {
                COMPOSER_KEY: {
                    'type': ToyAgent,
                    'config': {
                        'agent_id': agent_1_id,
                    }
                },
                'agents': {
                    agent_2_id: {
                        COMPOSER_KEY: {
                            'type': ToyAgent,
                            'config': {
                                'agent_id': agent_2_id
                            },
                        }
                    }
                }
            }
        }
    }

    # configure experiment
    settings = {}
    experiment = compose_experiment(hierarchy=hierarchy,
                                    initial_state=initial_state,
                                    settings=settings)

    pp(experiment.topology)
    pp(experiment.state.get_value())

    # run simulation
    experiment.update(time_total)
    output = experiment.emitter.get_data()
    experiment.end()  # end required for parallel processes

    # asserts total A is the same at the beginning and the end
    assert output[0.0]['concentrations']['A'] == initial_a
    assert output[5.0]['concentrations']['A'] + output[5.0]['agents'][
        agent_1_id]['concentrations']['A'] == initial_a

    return output
Esempio n. 4
0
def test_units() -> None:
    class UnitsMicrometer(Process):
        name = 'units_micrometer'

        def ports_schema(self) -> Schema:
            return {
                'A': {
                    'a': {
                        '_default': 0 * units.um,
                        '_emit': True
                    },
                    'b': {
                        '_default': 'string b',
                        '_emit': True,
                    },
                    'c': {
                        '_default': 0,
                        '_emit': True,
                    }
                }
            }

        def next_update(self, timestep: Union[float, int],
                        states: State) -> Update:
            return {
                'A': {
                    'a': 1 * units.um,
                    'c': 1,
                }
            }

    class UnitsMillimeter(Process):
        name = 'units_millimeter'

        def ports_schema(self) -> Schema:
            return {
                'A': {
                    'a': {
                        # '_default': 0 * units.mm,
                        '_emit': True
                    }
                }
            }

        def next_update(self, timestep: Union[float, int],
                        states: State) -> Update:
            return {'A': {'a': 1 * units.mm}}

    class MultiUnits(Composer):
        name = 'multi_units_composer'

        def generate_processes(self, config: Optional[dict]) -> Dict[str, Any]:
            return {
                'units_micrometer': UnitsMicrometer({}),
                'units_millimeter': UnitsMillimeter({})
            }

        def generate_topology(self, config: Optional[dict]) -> Topology:
            return {
                'units_micrometer': {
                    'A': ('aaa', )
                },
                'units_millimeter': {
                    'A': ('aaa', )
                }
            }

    # run experiment
    multi_unit = MultiUnits({})
    network = multi_unit.generate()
    exp = Engine(**{
        'processes': network['processes'],
        'topology': network['topology']
    })

    exp.update(5)
    timeseries = exp.emitter.get_timeseries()
    print('TIMESERIES')
    pp(timeseries)

    data = exp.emitter.get_data()
    print('DATA')
    pp(data)

    data_deserialized = exp.emitter.get_data_deserialized()
    print('DESERIALIZED')
    pp(data_deserialized)

    data_unitless = exp.emitter.get_data_unitless()
    print('UNITLESS')
    pp(data_unitless)

    query = [('aaa', 'a'), ('aaa', 'c')]
    query_data = exp.emitter.get_data(query)
    print('QUERY DATA')
    pp(query_data)
Esempio n. 5
0
def _run_tree_mass():
    out_dir = os.path.join(PROCESS_OUT_DIR, NAME)
    os.makedirs(out_dir, exist_ok=True)
    output = test_tree_mass()
    pp(output)