def test_satisficer_n_agent_per_level(method, n_agents, n_processes, n_steps): from scml.scml2020 import SCML2021World from pathlib import Path from negmas.situated import save_stats from negmas.helpers import force_single_thread force_single_thread(True) world = SCML2021World( **SCML2021World.generate( [SatisficerAgent] * n_processes, n_agents_per_process=n_agents, n_processes=n_processes, n_steps=n_steps, method=method, random_agent_types=False, neg_step_time_limit=float("inf"), neg_time_limit=float("inf"), ), compact=True, no_logs=True, avoid_ultimatum=True, end_negotiation_on_refusal_to_propose=True, name=f"Satisficer1{method}-a{n_agents}p{n_processes}s{n_steps}", ) world.run() world.save_negotiations = True save_stats( world, log_dir=Path.home() / "negmas" / "logs" / "scml" / "scml2021" / world.name, ) force_single_thread(False) assert True
def plot(world): world.draw(steps=(0, world.n_steps), together=False, ncols=2, figsize=(20, 20)) plt.show() save_stats(world, world.log_folder) show_agent_scores(world)
def test_satisficer(n_processes): world = generate_world( [SatisficerAgent], buy_missing_products=True, n_processes=n_processes, compact=COMPACT, no_logs=NOLOGS, ) world.run() save_stats(world, world.log_folder)
def test_can_run_with_a_single_agent_type(agent_type, n_processes): world = generate_world( [agent_type], n_processes=n_processes, name=unique_name( f"scml2020tests/single/{agent_type.__name__}" f"Fine{n_processes}", add_time=True, rand_digits=4, ), compact=COMPACT, no_logs=NOLOGS, ) world.run() save_stats(world, world.log_folder)
def test_can_run_with_a_multiple_agent_types(agent_types, n_processes): world = generate_world( agent_types, name=unique_name( f"scml2020tests/multi/{'-'.join(_.__name__[:3] for _ in agent_types)}/" f"Fine_p{n_processes}", add_time=True, rand_digits=4, ), n_processes=n_processes, compact=COMPACT, no_logs=NOLOGS, ) world.run() save_stats(world, world.log_folder)
def test_can_run_with_a_multiple_agent_types(agent_types, buy_missing, n_processes): world = generate_world( agent_types, buy_missing_products=buy_missing, name=unique_name( f"scml2020tests/multi/{'-'.join(_.__name__[:3] for _ in agent_types)}/" f"{'Buy' if buy_missing else 'Fine'}_p{n_processes}", add_time=True, rand_digits=4, ), n_processes=n_processes, initial_balance=10_000, compact=COMPACT, no_logs=NOLOGS, ) world.run() save_stats(world, world.log_folder)
def scml(steps, levels, neg_speedup, negotiator, agents, horizon, min_consumption, max_consumption, transport, time, neg_time, neg_steps, sign, guaranteed, lines, retrials, use_consumer, max_insurance, riskiness, log): params = { "steps": steps, "levels": levels, "neg_speedup": neg_speedup, "negotiator": negotiator, "agents": agents, "horizon": horizon, "min_consumption": min_consumption, "max_consumption": max_consumption, "transport": transport, "time": time, "neg_time": neg_time, "neg_steps": neg_steps, "sign": sign, "guaranteed": guaranteed, "lines": lines, "retrials": retrials, "use_consumer": use_consumer, "max_insurance": max_insurance, "riskiness": riskiness } neg_speedup = neg_speedup if neg_speedup is not None and neg_speedup > 0 else None if min_consumption == max_consumption: consumption = min_consumption else: consumption = (min_consumption, max_consumption) customer_kwargs = { 'negotiator_type': negotiator, 'consumption_horizon': horizon } miner_kwargs = {'negotiator_type': negotiator, 'n_retrials': retrials} factory_kwargs = { 'negotiator_type': negotiator, 'n_retrials': retrials, 'sign_only_guaranteed_contracts': guaranteed, 'use_consumer': use_consumer, 'riskiness': riskiness, 'max_insurance_premium': max_insurance } if log.startswith('~/'): log_dir = Path.home() / log[2:] else: log_dir = Path(log) log_dir = log_dir / unique_name(base='scml', add_time=True, rand_digits=0) log_dir = log_dir.absolute() os.makedirs(log_dir, exist_ok=True) log_file_name = str(log_dir / 'log.txt') stats_file_name = str(log_dir / 'stats.json') params_file_name = str(log_dir / 'params.json') world = SCMLWorld.single_path_world(log_file_name=log_file_name, n_steps=steps, negotiation_speed=neg_speedup, n_intermediate_levels=levels, n_miners=agents, n_consumers=agents, n_factories_per_level=agents, consumption=consumption, consumer_kwargs=customer_kwargs, miner_kwargs=miner_kwargs, manager_kwargs=factory_kwargs, transportation_delay=transport, time_limit=time, neg_time_limit=neg_time, neg_n_steps=neg_steps, default_signing_delay=sign, n_lines_per_factory=lines) failed = False strt = perf_counter() try: for i in progressbar.progressbar(range(world.n_steps), max_value=world.n_steps): elapsed = perf_counter() - strt if world.time_limit is not None and elapsed >= world.time_limit: break if not world.step(): break except Exception: exception = traceback.format_exc() failed = True elapsed = perf_counter() - strt def print_and_log(s): world.logdebug(s) print(s) world.logdebug(f'{pformat(world.stats, compact=True)}') world.logdebug( f'=================================================\n' f'steps: {steps}, horizon: {horizon}, time: {time}, levels: {levels}, agents_per_level: ' f'{agents}, lines: {lines}, guaranteed: {guaranteed}, negotiator: {negotiator}\n' f'consumption: {consumption}' f', transport_to: {transport}, sign: {sign}, speedup: {neg_speedup}, neg_steps: {neg_steps}' f', retrials: {retrials}' f', neg_time: {neg_time}\n' f'==================================================') save_stats(world=world, log_dir=log_dir, params=params) if len(world.saved_contracts) > 0: data = pd.DataFrame(world.saved_contracts) data = data.sort_values(['delivery_time']) data = data.loc[:, [ 'seller_type', 'buyer_type', 'seller_name', 'buyer_name', 'delivery_time', 'unit_price', 'quantity', 'product_name', 'n_neg_steps', 'signed_at', 'concluded_at', 'cfp' ]] print_and_log(tabulate(data, headers='keys', tablefmt='psql')) n_executed = sum(world.stats['n_contracts_executed']) n_negs = sum(world.stats["n_negotiations"]) n_contracts = len(world.saved_contracts) winners = [ f'{_.name} gaining {world.a2f[_.id].balance / world.a2f[_.id].initial_balance - 1.0:0.0%}' for _ in world.winners ] print_and_log(f'{n_contracts} contracts :-) [N. Negotiations: {n_negs}' f', Agreement Rate: {world.agreement_rate:0.0%}]') print_and_log(f'Executed: {world.contract_execution_fraction:0.0%}' f', Breached: {world.breach_rate:0.0%}' f', N. Executed: {n_executed}' f', Business size: {world.business_size}\n' f'Winners: {winners}\n' f'Running Time {humanize_time(elapsed)}') else: print_and_log('No contracts! :-(') print_and_log(f'Running Time {humanize_time(elapsed)}') if failed: print(exception) world.logdebug(exception) print(f'FAILED at step {world.current_step} of {world.n_steps}\n')
def scml( steps, levels, neg_speedup, negotiator, agents, horizon, min_consumption, max_consumption, transport, time, neg_time, neg_steps, sign, guaranteed, lines, retrials, use_consumer, max_insurance, riskiness, competitors, jcompetitors, log, compact, log_ufuns, log_negs, reserved_value, balance, shared_profile, raise_exceptions, path, world_config, ): kwargs = dict( no_bank=True, no_insurance=False, prevent_cfp_tampering=True, ignore_negotiated_penalties=False, neg_step_time_limit=10, breach_penalty_society=0.02, premium=0.03, premium_time_increment=0.1, premium_breach_increment=0.001, max_allowed_breach_level=None, breach_penalty_society_min=0.0, breach_penalty_victim=0.0, breach_move_max_product=True, transfer_delay=0, start_negotiations_immediately=False, catalog_profit=0.15, financial_reports_period=10, default_price_for_products_without_one=1, compensation_fraction=0.5, ) if world_config is not None and len(world_config) > 0: for wc in world_config: kwargs.update(load(wc)) if len(path) > 0: sys.path.append(path) if max_insurance < 0: warnings.warn( f"Negative max insurance ({max_insurance}) is deprecated. Set --max-insurance=inf for always " f"buying and --max-insurance=0.0 for never buying. Will continue assuming --max-insurance=inf" ) max_insurance = float("inf") if "." not in negotiator: negotiator = "negmas.sao." + negotiator params = { "steps": steps, "levels": levels, "neg_speedup": neg_speedup, "negotiator": negotiator, "agents": agents, "horizon": horizon, "min_consumption": min_consumption, "max_consumption": max_consumption, "transport": transport, "time": time, "neg_time": neg_time, "neg_steps": neg_steps, "sign": sign, "guaranteed": guaranteed, "lines": lines, "retrials": retrials, "use_consumer": use_consumer, "max_insurance": max_insurance, "riskiness": riskiness, } if compact: log_ufuns = False log_negs = False neg_speedup = neg_speedup if neg_speedup is not None and neg_speedup > 0 else None if min_consumption == max_consumption: consumption = min_consumption else: consumption = (min_consumption, max_consumption) customer_kwargs = { "negotiator_type": negotiator, "consumption_horizon": horizon } miner_kwargs = {"negotiator_type": negotiator, "n_retrials": retrials} factory_kwargs = { "negotiator_type": negotiator, "n_retrials": retrials, "sign_only_guaranteed_contracts": guaranteed, "use_consumer": use_consumer, "riskiness": riskiness, "max_insurance_premium": max_insurance, "reserved_value": reserved_value, } if log.startswith("~/"): log_dir = Path.home() / log[2:] else: log_dir = Path(log) world_name = unique_name(base="scml", add_time=True, rand_digits=0) log_dir = log_dir / world_name log_dir = log_dir.absolute() os.makedirs(log_dir, exist_ok=True) exception = None def _no_default(s): return not (s.startswith("negmas.apps.scml") and s.endswith("GreedyFactoryManager")) all_competitors = competitors.split(";") for i, cp in enumerate(all_competitors): if "." not in cp: all_competitors[i] = "negmas.apps.scml.factory_managers." + cp all_competitors_params = [ dict() if _no_default(_) else factory_kwargs for _ in all_competitors ] if jcompetitors is not None and len(jcompetitors) > 0: jcompetitor_params = [{ "java_class_name": _ } for _ in jcompetitors.split(";")] for jp in jcompetitor_params: if "." not in jp["java_class_name"]: jp["java_class_name"] = ( "jnegmas.apps.scml.factory_managers." + jp["java_class_name"]) jcompetitors = ["negmas.apps.scml.JavaFactoryManager" ] * len(jcompetitor_params) all_competitors += jcompetitors all_competitors_params += jcompetitor_params print( "You are using some Java agents. The tournament MUST run serially") parallelism = "serial" if not jnegmas_bridge_is_running(): print( "Error: You are using java competitors but jnegmas bridge is not running\n\nTo correct this issue" " run the following command IN A DIFFERENT TERMINAL because it will block:\n\n" "$ negmas jnegmas") exit(1) world = SCMLWorld.chain_world( n_steps=steps, negotiation_speed=neg_speedup, n_intermediate_levels=levels, n_miners=agents, n_consumers=agents, n_factories_per_level=agents, consumption=consumption, consumer_kwargs=customer_kwargs, miner_kwargs=miner_kwargs, default_manager_params=factory_kwargs, transportation_delay=transport, time_limit=time, neg_time_limit=neg_time, neg_n_steps=neg_steps, default_signing_delay=sign, n_lines_per_factory=lines, compact=compact, agent_names_reveal_type=True, log_ufuns=log_ufuns, manager_types=all_competitors, manager_params=all_competitors_params, log_negotiations=log_negs, log_folder=log_dir, name=world_name, shared_profile_per_factory=shared_profile, initial_wallet_balances=balance, ignore_agent_exceptions=not raise_exceptions, ignore_contract_execution_exceptions=not raise_exceptions, **kwargs, ) failed = False strt = perf_counter() try: for i in progressbar.progressbar(range(world.n_steps), max_value=world.n_steps): elapsed = perf_counter() - strt if world.time_limit is not None and elapsed >= world.time_limit: break if not world.step(): break except Exception: exception = traceback.format_exc() failed = True elapsed = perf_counter() - strt def print_and_log(s): world.logdebug(s) print(s) world.logdebug(f"{pformat(world.stats, compact=True)}") world.logdebug( f"=================================================\n" f"steps: {steps}, horizon: {horizon}, time: {time}, levels: {levels}, agents_per_level: " f"{agents}, lines: {lines}, guaranteed: {guaranteed}, negotiator: {negotiator}\n" f"consumption: {consumption}" f", transport_to: {transport}, sign: {sign}, speedup: {neg_speedup}, neg_steps: {neg_steps}" f", retrials: {retrials}" f", neg_time: {neg_time}\n" f"==================================================") save_stats(world=world, log_dir=log_dir, params=params) if len(world.saved_contracts) > 0: data = pd.DataFrame(world.saved_contracts) data = data.sort_values(["delivery_time"]) data = data.loc[data.signed_at >= 0, [ "seller_type", "buyer_type", "seller_name", "buyer_name", "delivery_time", "unit_price", "quantity", "product_name", "n_neg_steps", "signed_at", ], ] data.columns = [ "seller_type", "buyer_type", "seller", "buyer", "t", "price", "q", "product", "steps", "signed", ] print_and_log(tabulate(data, headers="keys", tablefmt="psql")) data["product_id"] = np.array([_.id for _ in data["product"].values]) d2 = (data.loc[(~(data["signed"].isnull())) & (data["signed"] > -1), :].groupby( ["product_id"]).apply(lambda x: pd.DataFrame([{ "uprice": np.sum(x["price"] * x["q"]) / np.sum(x["q"]), "quantity": np.sum(x["q"]), }]))) d2 = d2.reset_index().sort_values(["product_id"]) products = dict(zip([_.id for _ in world.products], world.products)) d2["Product"] = np.array( [products[_] for _ in d2["product_id"].values]) d2 = d2.loc[:, ["Product", "uprice", "quantity"]] d2.columns = ["Product", "Avg. Unit Price", "Total Quantity"] print_and_log(tabulate(d2, headers="keys", tablefmt="psql")) n_executed = sum(world.stats["n_contracts_executed"]) n_negs = sum(world.stats["n_negotiations"]) n_contracts = len(world.saved_contracts) try: agent_scores = sorted( [[_.name, world.a2f[_.id].total_balance] for _ in world.agents.values() if isinstance(_, FactoryManager)], key=lambda x: x[1], reverse=True, ) agent_scores = pd.DataFrame(data=np.array(agent_scores), columns=["Agent", "Final Balance"]) print_and_log( tabulate(agent_scores, headers="keys", tablefmt="psql")) except: pass winners = [ f"{_.name} gaining {world.a2f[_.id].total_balance / world.a2f[_.id].initial_balance - 1.0:0.0%}" for _ in world.winners ] print_and_log( f"{n_contracts} contracts :-) [N. Negotiations: {n_negs}, Agreement Rate: " f"{world.agreement_rate:0.0%}]" f" (rounds/successful negotiation: {world.n_negotiation_rounds_successful:5.2f}, " f"rounds/broken negotiation: {world.n_negotiation_rounds_failed:5.2f})" ) print_and_log( f"Cancelled: {world.cancellation_rate:0.0%}, Executed: {world.contract_execution_fraction:0.0%}" f", Breached: {world.breach_rate:0.0%}, N. Executed: {n_executed}, Business size: " f"{world.business_size}\n" f"Winners: {winners}\n" f"Running Time {humanize_time(elapsed)}") else: print_and_log("No contracts! :-(") print_and_log(f"Running Time {humanize_time(elapsed)}") if failed: print(exception) world.logdebug(exception) print(f"FAILED at step {world.current_step} of {world.n_steps}\n")