def test_can_run_a_random_tiny_scml_world_with_no_factory_with_delay_no_immediate_neg( ): n_steps = 10 horizon = 4 world = SCML2019World.chain_world( n_intermediate_levels=-1, log_file_name="", n_steps=n_steps, default_signing_delay=1, consumer_kwargs={ "immediate_cfp_update": False, "consumption_horizon": horizon }, ) world.run() # print('') # for key in sorted(world.stats.keys()): # print(f'{key}:{world.stats[key]}') # assert world.stats['n_negotiations'][0] >= horizon - 1, "at least n negotiations occur where n is the number of " \ # "horizon - 1" assert sum(world.stats["n_contracts_concluded"] ) >= world.stats["n_contracts_concluded"][-1] + sum( world.stats["n_contracts_signed"]), "some contracts signed" assert sum(world.stats["n_breaches"]) == 0, "No breaches" assert sum(world.stats["market_size"]) == 0, "No change in the market size"
def test_can_run_a_random_tiny_scml_world_with_no_factory(): n_steps = 10 world = SCML2019World.chain_world( n_intermediate_levels=-1, log_file_name="", n_steps=n_steps, negotiation_speed=21, neg_n_steps=20, n_miners=2, n_consumers=2, ignore_agent_exceptions=False, ignore_contract_execution_exceptions=False, ) world.run() # print('') # for key in sorted(world.stats.keys()): # print(f'{key}:{world.stats[key]}') assert world.stats["n_negotiations"][0] >= n_steps - 1, ( "at least n negotiations occur where n is the number of " "steps - 1") assert world.stats["n_negotiations"][2:] == [0] * ( len(world.stats["n_negotiations"]) - 2), ("All negotiations " "happen in steps 0, 1") assert sum(world.stats["n_contracts_concluded"]) >= sum( world.stats["n_contracts_signed"]), "some contracts signed" assert sum(world.stats["n_breaches"]) == 0, "No breaches" assert sum(world.stats["market_size"]) == 0, "No change in the market size"
def test_can_run_a_random_tiny_scml_world_with_insurance(): world = SCML2019World.chain_world( log_file_name="", n_steps=5 # , factory_kwargs={'max_insurance_premium': 1e6} , consumer_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, miner_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, ) world.run()
def test_scml_picklable(tmp_path): import pickle import dill file = tmp_path / "world.pckl" n_steps = 10 horizon = 4 world = SCML2019World.chain_world( n_intermediate_levels=-1, log_file_name="", n_steps=n_steps, default_signing_delay=1, consumer_kwargs={ "immediate_cfp_update": False, "consumption_horizon": horizon }, ) world.step() world.step() with open(file, "wb") as f: dill.dump(world, f) with open(file, "rb") as f: w = dill.load(f) assert world.current_step == w.current_step assert sorted(world.agents.keys()) == sorted(w.agents.keys()) assert w.bulletin_board is not None assert w.n_steps == world.n_steps assert w.negotiation_speed == world.negotiation_speed world.step() with open(file, "wb") as f: dill.dump(world, f) with open(file, "rb") as f: w = dill.load(f) assert world.current_step == w.current_step assert sorted(world.agents.keys()) == sorted(w.agents.keys()) assert w.bulletin_board is not None assert w.n_steps == world.n_steps assert w.negotiation_speed == world.negotiation_speed w.run() assert sum(w.stats["n_contracts_concluded"] ) >= w.stats["n_contracts_concluded"][-1] + sum( w.stats["n_contracts_signed"]), "some contracts signed" assert sum(w.stats["n_breaches"]) == 0, "No breaches" assert sum(w.stats["market_size"]) == 0, "No change in the market size"
def test_can_run_a_random_tiny_scml_world(): world = SCML2019World.chain_world( log_file_name="", n_steps=5, n_factories_per_level=1, consumer_kwargs={ "negotiator_type": "negmas.sao.NiceNegotiator", "consumption_horizon": 2, } # , factory_kwargs={'max_insurance_premium': 100} , miner_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, ) world.run() # print('') # for key in sorted(world.stats.keys()): # print(f'{key}:{world.stats[key]}') winners = [_.name for _ in world.winners]
def test_java_factory_manager(java_class): class JFM(JavaFactoryManager): def __init__(self, *args, **kwargs): super().__init__(*args, java_class_name=java_class, **kwargs) with jnegmas_connection(shutdown=SHUTDOWN_AFTER_EVERY_TEST): log_file = os.path.expanduser(f'test{java_class.split(".")[-1]}.txt') print(log_file) world = SCML2019World.chain_world( manager_types=(JFM, GreedyFactoryManager), n_steps=5, n_factories_per_level=3, n_default_per_level=1, n_intermediate_levels=1, agent_names_reveal_type=True, # log_folder=str(Path.home() / "negmas" / "logs" / "debug"), # log_file_name=log_file, ) world.run()
def test_world_auto_checkpoint(tmp_path, single_checkpoint, checkpoint_every, exist_ok): import shutil new_folder: Path = tmp_path / unique_name("empty", sep="") new_folder.mkdir(parents=True, exist_ok=True) shutil.rmtree(new_folder) new_folder.mkdir(parents=True, exist_ok=True) filename = "scml" n_steps = 5 world = SCML2019World.chain_world( log_file_name="", n_steps=n_steps, n_factories_per_level=1, consumer_kwargs={ "negotiator_type": "negmas.sao.NiceNegotiator", "consumption_horizon": 2, }, miner_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, checkpoint_every=checkpoint_every, checkpoint_folder=new_folder, checkpoint_filename=filename, extra_checkpoint_info=None, exist_ok=exist_ok, single_checkpoint=single_checkpoint, ) world.run() if 0 < checkpoint_every <= n_steps: if single_checkpoint: assert len(list(new_folder.glob("*"))) == 2, print( f"SCML2020World ran for: {world.current_step}") else: assert len(list(new_folder.glob("*"))) >= 2 * (max( 1, world.current_step // checkpoint_every)) elif checkpoint_every > n_steps: assert len(list(new_folder.glob("*"))) == 2 else: assert len(list(new_folder.glob("*"))) == 0
def test_can_run_a_random_tiny_scml_world_with_no_factory_with_delay(): n_steps = 10 world = SCML2019World.chain_world( n_intermediate_levels=-1, log_file_name="", n_steps=n_steps, default_signing_delay=1, ) world.run() # print('') # for key in sorted(world.stats.keys()): # print(f'{key}:{world.stats[key]}') assert world.stats["n_negotiations"][0] >= n_steps - 1, ( "at least n negotiations occur where n is the number of " "steps - 1") # assert world.stats['n_negotiations'][1:] == [0] * (len(world.stats['n_negotiations']) - 1), "All negotiations " \ # "happen in step 0" assert sum(world.stats["n_contracts_concluded"]) >= sum( world.stats["n_contracts_signed"]), "all contracts signed" assert sum(world.stats["n_breaches"]) == 0, "No breaches" assert sum(world.stats["market_size"]) == 0, "No change in the market size"
def test_can_run_a_random_tiny_scml_world_with_linear_production(): horizon = None signing_delay = 0 n_factory_levels = 0 n_factories_per_level = 2 n_steps = 10 world = SCML2019World.chain_world( n_intermediate_levels=n_factory_levels - 1, log_file_name="", n_steps=n_steps, n_factories_per_level=n_factories_per_level, default_signing_delay=signing_delay, negotiation_speed=21, neg_n_steps=20, consumer_kwargs={ "consumption_horizon": horizon, "negotiator_type": "negmas.sao.NiceNegotiator", }, miner_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, ignore_agent_exceptions=False, ) world.run() assert sum(world.stats["n_contracts_concluded"]) > 0
def test_can_run_a_random_tiny_scml_world_no_immediate(): world = SCML2019World.chain_world(log_file_name="", n_steps=5) world.run()
def test_world_checkpoint(tmp_path): world = SCML2019World.chain_world( log_file_name="", n_steps=5, n_factories_per_level=1, consumer_kwargs={ "negotiator_type": "negmas.sao.NiceNegotiator", "consumption_horizon": 2, } # , factory_kwargs={'max_insurance_premium': 100} , miner_kwargs={"negotiator_type": "negmas.sao.NiceNegotiator"}, ) world.step() world.step() file_name = world.checkpoint(tmp_path) info = SCML2019World.checkpoint_info(file_name) assert isinstance(info["time"], str) assert info["step"] == 2 assert info["type"].endswith("SCML2019World") assert info["id"] == world.id assert info["name"] == world.name w = SCML2019World.from_checkpoint(file_name) assert world.current_step == w.current_step assert len(world.agents) == len(w.agents) assert world.agents.keys() == w.agents.keys() assert len(world.factories) == len(w.factories) assert w.bulletin_board is not None assert w.n_steps == world.n_steps assert w.negotiation_speed == world.negotiation_speed world = w file_name = world.checkpoint(tmp_path) w = SCML2019World.from_checkpoint(file_name) assert world.current_step == w.current_step assert len(world.agents) == len(w.agents) assert world.agents.keys() == w.agents.keys() assert len(world.factories) == len(w.factories) assert w.bulletin_board is not None assert w.n_steps == world.n_steps assert w.negotiation_speed == world.negotiation_speed w.step() world = w file_name = world.checkpoint(tmp_path) w = SCML2019World.from_checkpoint(file_name) assert world.current_step == w.current_step assert len(world.agents) == len(w.agents) assert world.agents.keys() == w.agents.keys() assert len(world.factories) == len(w.factories) assert w.bulletin_board is not None assert w.n_steps == world.n_steps assert w.negotiation_speed == world.negotiation_speed w.run()