def test_ufun_penalty_scales_are_correct(penalties_scale): from scml.oneshot.ufun import OneShotUFun world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( MyIndNeg, n_agents_per_process=3, n_processes=2, n_steps=30, penalties_scale=penalties_scale, ), compact=True, no_logs=True, ) for _ in range(30): old_trading = world.trading_prices.copy() world.step() for _, a in world.agents.items(): if is_system_agent(a.id): continue u: OneShotUFun = a.ufun if penalties_scale == "trading": assert (u.output_penalty_scale # == a.awi.trading_prices[a.awi.my_output_product] == old_trading[a.awi.my_output_product]) assert (u.input_penalty_scale # == a.awi.trading_prices[a.awi.my_input_product] == old_trading[a.awi.my_input_product]) else: assert (u.output_penalty_scale == a.awi.catalog_prices[ a.awi.my_output_product]) assert (u.input_penalty_scale == a.awi.catalog_prices[ a.awi.my_input_product])
def test_sync_counter_all_reenters_as_expected( use_sleep, check_negs, single_thread, raise_expected ): from scml.oneshot import SCML2020OneShotWorld types = { (False, False): NotSleepingNotChecking, (False, True): NotSleepingChecking, (True, False): SleepingNotChecking, (True, True): SleepingChecking, } n_steps = 5 world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( [types[(use_sleep, check_negs)], RandomOneShotAgent], n_agents_per_process=2, n_processes=2, n_steps=n_steps, ), compact=True, no_logs=True, ) force_single_thread(single_thread) with (raises if raise_expected else does_not_raise)(RuntimeError): world.run() force_single_thread(False)
def test_graphs_lead_to_no_unknown_nodes(): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate(agent_types=[RandomOneShotAgent], n_steps=10), construct_graphs=True, ) world.graph((0, world.n_steps))
def test_production_cost_increase(): from scml.oneshot.agents import GreedyOneShotAgent from scml.oneshot.world import SCML2020OneShotWorld NPROCESSES = 5 costs = [[] for _ in range(NPROCESSES)] for _ in range(100): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( GreedyOneShotAgent, n_agents_per_process=10, n_processes=NPROCESSES, ), compact=True, no_logs=True, ) for aid in world.agent_profiles.keys(): if is_system_agent(aid): continue profile = world.agent_profiles[aid] costs[profile.input_product].append(profile.cost) mean_costs = [sum(_) / len(_) for _ in costs] assert all([ b > (0.5 * (i + 2) / (i + 1)) * a for i, (a, b) in enumerate(zip(mean_costs[:-1], mean_costs[1:])) ]), f"non-ascending costs {mean_costs}"
def test_basic_awi_info_suppliers_consumers(): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( agent_types=RandomOneShotAgent, n_steps=10, n_processes=2, compact=True, no_logs=True, ) ) for aid in world.agents: if is_system_agent(aid): continue a = world.agents[aid] assert a.id in a.awi.all_suppliers[a.awi.my_output_product] assert a.id in a.awi.all_consumers[a.awi.my_input_product] assert a.awi.my_consumers == world.agent_consumers[aid] assert a.awi.my_suppliers == world.agent_suppliers[aid] l = a.awi.my_input_product assert all( _.endswith(str(l - 1)) or a.awi.is_system(_) for _ in a.awi.my_suppliers ) assert all( _.endswith(str(l + 1)) or a.awi.is_system(_) for _ in a.awi.my_consumers )
def test_adapter(atype): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate(agent_types=atype, n_steps=10), construct_graphs=False, compact=True, no_logs=True, ) world.run()
def test_generate(): world = SCML2020OneShotWorld(**SCML2020OneShotWorld.generate( agent_types=RandomOneShotAgent, n_steps=10, n_processes=2, compact=True, no_logs=True, )) world.run() assert True
def test_ufun_min_max(): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate(agent_types=[RandomOneShotAgent], n_steps=10), construct_graphs=True, ) world.step() for aid, agent in world.agents.items(): if is_system_agent(aid): continue ufun = agent.make_ufun() mn, mx = ufun.min_utility, ufun.max_utility assert mx > mn or mx == mn == 0
def test_underfullfilment_is_irrational(): agent_types = [RandomOneShotAgent, MyExogAgent] * 2 for _ in range(10): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( agent_types, n_steps=100, n_processes=2, n_agents_per_process=2, random_agent_types=False, ), ) with single_thread(): world.run()
def test_adapter_example(): atype = [ scml.scml2020.agents.random.RandomAgent, scml.oneshot.agents.nothing.OneshotDoNothingAgent, scml.scml2020.agents.do_nothing.DoNothingAgent, scml.scml2020.agents.indneg.IndependentNegotiationsAgent, scml.scml2020.agents.decentralizing.DecentralizingAgent, scml.scml2020.agents.decentralizing.DecentralizingAgentWithLogging, ] world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate(agent_types=atype, n_steps=10), construct_graphs=False, ) world.run()
def test_sync_agent_receives_first_proposals_before_counter_all( agent_types, n_agents_per_process, n_processes): n_steps = 50 world = SCML2020OneShotWorld(**SCML2020OneShotWorld.generate( [MySyncAgent] + agent_types, n_processes=n_processes, n_steps=n_steps, n_lines=10, n_agents_per_process=n_agents_per_process, no_logs=True, compact=True, ignore_agent_exceptions=False, ignore_negotiation_exceptions=False, ignore_contract_execution_exceptions=False, ignore_simulation_exceptions=False, )) world.run() assert world.current_step >= n_steps - 1
def test_ufun_min_max_in_world(): for _ in range(20): world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate(agent_types=[RandomOneShotAgent], n_steps=10), construct_graphs=False, compact=True, no_logs=True, ) world.step() for aid, agent in world.agents.items(): if is_system_agent(aid): continue ufun = agent.make_ufun(add_exogenous=True) ufun.find_limit(True) ufun.find_limit(False) mn, mx = ufun.min_utility, ufun.max_utility assert mx >= mn
def generate_world( agent_types, n_processes=3, n_steps=10, n_agents_per_process=2, n_lines=10, **kwargs, ): kwargs["no_logs"] = True kwargs["compact"] = True world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( agent_types, n_processes=n_processes, n_steps=n_steps, n_lines=n_lines, n_agents_per_process=n_agents_per_process, **kwargs, ) ) for s1, s2 in zip(world.suppliers[:-1], world.suppliers[1:]): assert len(set(s1).intersection(set(s2))) == 0 for s1, s2 in zip(world.consumers[:-1], world.consumers[1:]): assert len(set(s1).intersection(set(s2))) == 0 for p in range(n_processes): assert len(world.suppliers[p + 1]) == n_agents_per_process assert len(world.consumers[p]) == n_agents_per_process for a in world.agents.keys(): if is_system_agent(a): continue assert len(world.agent_inputs[a]) == 1 assert len(world.agent_outputs[a]) == 1 assert len(world.agent_processes[a]) == 1 assert len(world.agent_suppliers[a]) == ( n_agents_per_process if world.agent_inputs[a][0] != 0 else 1 ) assert len(world.agent_consumers[a]) == ( n_agents_per_process if world.agent_outputs[a][0] != n_processes else 1 ) return world
def test_trading_prices_updated(n_agents, n_processes, n_steps): from scml.oneshot import SCML2020OneShotWorld from negmas.helpers import force_single_thread eps = 1e-3 world = SCML2020OneShotWorld( **SCML2020OneShotWorld.generate( [MyRandomAgent] * n_processes, n_agents_per_process=n_agents, n_processes=n_processes, n_steps=n_steps, ), compact=True, no_logs=True, ) catalog_prices = world.catalog_prices diffs = np.zeros_like(catalog_prices) # we start at catlaog prices for aid, agent in world.agents.items(): assert np.abs(agent.awi.trading_prices - catalog_prices).max() < eps force_single_thread(True) for _ in range(n_steps): world.step() trading_prices = None for aid, agent in world.agents.items(): if is_system_agent(aid): continue trading_prices = agent.awi.trading_prices.copy() break diffs = np.maximum(diffs, np.abs(trading_prices - catalog_prices)) assert diffs.max() > eps force_single_thread(False)