Example #1
0
def run(n_steps=52, n_processes=3):
    """
    **Not needed for submission.** You can use this function to test your agent.

    Args:
        n_steps:     The number of simulation steps.
        n_processes: # processes.

    Returns:
        None

    Remarks:

        - This function will take several minutes to run.
        - To speed it up, use a smaller `n_step` value

    """

    start = time.perf_counter()
    world = SCML2020World(**SCML2020World.generate(
        agent_types=competitors, n_steps=n_steps, n_processes=n_processes))
    world.run()
    scores = world.scores()
    pprint(scores)
    f = open("scores", "a")
    f.write(scores)
    f.close()
    print(f"Finished in {humanize_time(time.perf_counter() - start)}")
Example #2
0
    def run_with_save(n_steps, n_processes) -> Dict[str, float]:
        """
        Same as run() but with save functionality for benchmarking
        Returns:
            new_dict: dictionary with all the average scores of each agent at each level
             (accumulating all xxDec@0 into just Dec@0)
        """

        start = time.perf_counter()
        world = SCML2020World(**SCML2020World.generate(
            agent_types=competitors, n_steps=n_steps, n_processes=n_processes))
        world.run()

        # iterate through dictionary of agents and scores and only take "agent@x", adding up all the scores of agent@x
        scores = world.scores()  # a dict
        new_dict = {}
        agent_count_dict = {}
        for agent_name in scores:
            if agent_name[2:] in new_dict:
                agent_count_dict[agent_name[2:]] += 1
                new_dict[agent_name[2:]] += scores[agent_name]
            else:
                agent_count_dict[agent_name[2:]] = 1
                new_dict[agent_name[2:]] = scores[agent_name]

        # divide by number of occurrences of each agent at each level
        for agent_name in new_dict:
            new_dict[agent_name] = new_dict[agent_name] / agent_count_dict[
                agent_name]

        pprint(scores)
        pprint(f"Aggregated scores: {new_dict}")
        print(f"Finished in {humanize_time(time.perf_counter() - start)}")

        return new_dict
Example #3
0
def test_generate():
    world = SCML2020World(**SCML2020World.generate(agent_types=DoNothingAgent,
                                                   n_steps=10,
                                                   n_processes=4,
                                                   initial_balance=None))
    world.run()
    assert True
Example #4
0
def test_graphs_lead_to_no_unknown_nodes():
    world = SCML2020World(
        **SCML2020World.generate(
            agent_types=[DecentralizingAgent, BuyCheapSellExpensiveAgent], n_steps=10
        ),
        construct_graphs=True,
    )
    world.graph((0, world.n_steps))
def test_can_run(fm):
    n_steps = 5
    world = SCML2020World(**SCML2020World.generate(
        agent_types=["scml.scml2020.DecentralizingAgent", fm],
        n_steps=n_steps,
    ))
    world.run()
    assert sum(world.stats["n_contracts_concluded"]) >= 0
Example #6
0
def run_single_session():
    world = SCML2020World(
        **SCML2020World.generate(agent_types=competitors,
                                 n_steps=50,
                                 n_processes=3),
        construct_graphs=True,
    )

    _, _ = world.draw()

    world.run_with_progress()

    contracts = world.contracts_df
    signed = contracts.loc[contracts.signed_at >= 0, :]
    fields = [
        "seller_name", "buyer_name", "delivery_time", "quantity", "unit_price",
        "signed_at", "executed", "breached", "nullified", "erred"
    ]
    signed[fields].sort_values(["quantity", "unit_price"],
                               ascending=False).head(10)
    df1 = signed.loc[signed.executed,
                     fields].sort_values(["quantity", "unit_price"],
                                         ascending=False).head(10)
    df2 = signed.loc[signed.breached, fields[:-4] + ["breaches"]].sort_values(
        ["quantity", "unit_price"], ascending=False).head(10)
    #df1.to_csv('C:/Users/ED2016/Documents/SCML/scml2020/newsvendorlogs/eg17.csv')
    #df2.to_csv('C:/Users/ED2016/Documents/SCML/scml2020/newsvendorlogs/eg18.csv')

    fig, (profit, score) = plt.subplots(1, 2, figsize=(15, 15))
    snames = sorted(world.non_system_agent_names)
    for name in snames:
        profit.plot(100.0 * (np.asarray(world.stats[f'balance_{name}']) /
                             world.stats[f'balance_{name}'][0] - 1.0),
                    label=name)
        score.plot(100 * np.asarray(world.stats[f'score_{name}']), label=name)
    profit.set(xlabel='Simulation Step',
               ylabel='Player Profit Ignoring Inventory (%)')
    profit.legend(loc='lower left')
    score.set(xlabel='Simulation Step', ylabel='Player Score (%)')
    fig.show()

    fig, (score, profit) = plt.subplots(1, 2, figsize=(15, 15))
    final_scores = [
        world.stats[f"score_{_}"][-1] * (world.stats[f"balance_{_}"][0])
        for _ in world.non_system_agent_names
    ]
    final_profits = [
        world.stats[f"balance_{_}"][-1] - world.stats[f"balance_{_}"][0]
        for _ in world.non_system_agent_names
    ]
    plt.setp(score.xaxis.get_majorticklabels(), rotation=45)
    plt.setp(profit.xaxis.get_majorticklabels(), rotation=45)
    score.bar(world.non_system_agent_names, final_scores)
    profit.bar(world.non_system_agent_names, final_profits)
    score.set(ylabel="Final Unnormalized Score ($)")
    profit.set(ylabel="Final Balance  ($)")

    fig.show()
Example #7
0
def test_scml_world():
    agent_types = [
        DecentralizingAgent, BuyCheapSellExpensiveAgent,
        IndDecentralizingAgent, MovingRangeAgent
    ]

    world = SCML2020World(
        **SCML2020World.generate(agent_types=agent_types, n_steps=50))

    world.draw()
    world.run_with_progress()
Example #8
0
    def make_world(self, config=None) -> TrainWorld:
        # configuration, for Scenario scml
        if config is None:
            agent_types = [
                get_class(agent_type, ) for agent_type in TRAINING_AGENT_TYPES
            ]
            n_steps = N_STEPS
            world_configuration = SCML2020World.generate(
                agent_types=agent_types, n_steps=n_steps)
        else:
            world_configuration = SCML2020World.generate(
                agent_types=config['agent_types'],
                agent_params=config['agent_params'][:-2],
                n_steps=config['n_steps'])

        world = TrainWorld(configuration=world_configuration)

        if config is None:
            self.reset_world(world)

        return world
Example #9
0
    def reset_world(self, world):
        # callback, reset

        # reset world, agents, factories
        # fixed position
        agent_types = world.configuration['agent_types']
        agent_params = world.configuration['agent_params'][:-2]
        n_steps = world.configuration['n_steps']

        reset_configuration = SCML2020World.generate(
            #TODO: [Future work Improvement] could be reset
            agent_types=agent_types,
            agent_params=agent_params,
            n_steps=n_steps)

        world.__init__(configuration=reset_configuration)
Example #10
0
def run(n_steps=52):
    """
    **Not needed for submission.** You can use this function to test your agent.

    Args:
        n_steps:     The number of simulation steps.

    Returns:
        None

    Remarks:

        - This function will take several minutes to run.
        - To speed it up, use a smaller `n_step` value

    """

    start = time.perf_counter()
    world = SCML2020World(**SCML2020World.generate(
        agent_types=competitors, n_steps=n_steps, n_processes=3))
    world.run()
    pprint(world.scores())
Example #11
0
    def __init__(self, configuration=None, *args, **kwargs):
        # maddpg drived agents, heuristic agents, script drived agents, interative agents
        # self.agents = []
        # SELLER, BUYER
        self.system_entities = []

        # communication channel dimensionality
        self.dim_c = 2
        # negotiation management dimensionality
        self.dim_m = DIM_M  # seller
        self.dim_b = DIM_B  # buyer

        # simulation timestep
        self.dt = 0.1

        # world done
        self.__done = False

        # set up the scml2020world
        if configuration is None:
            configuration = SCML2020World.generate(*args, **kwargs)

        self.configuration = copy.deepcopy(configuration)

        super().__init__(**self.configuration)
        # set action_callback for agent which hasnot it
        for agent in self.agents.values():
            if not hasattr(agent, 'action_callback'):
                if is_system_agent(agent.id):
                    agent.action_callback = 'system'
                    self.system_entities.append(agent)
                else:
                    agent.action_callback = 'heuristic'

            if not hasattr(agent, 'interactive'):
                agent.interactive = False

            if not hasattr(agent, 'state'):
                agent.state = AgentState()
Example #12
0
    n_processes=3,
    n_steps=10,
    n_agents_per_process=2,
    n_lines=10,
    initial_balance=10_000,
    buy_missing_products=True,
    **kwargs,
):
    kwargs["no_logs"] = True
    kwargs["compact"] = True
    world = SCML2020World(
        **SCML2020World.generate(
            agent_types,
            n_processes=n_processes,
            n_steps=n_steps,
            n_lines=n_lines,
            n_agents_per_process=n_agents_per_process,
            initial_balance=initial_balance,
            buy_missing_products=buy_missing_products,
            **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
Example #13
0
# simulation with our agent
# world = SCML2020World(
#     **SCML2020World.generate([MyDrorAgent, TheirFinalAgent, DrorFinalAgent, comp2, ComparisonAgent], n_steps=15),
#     construct_graphs=True,
# )

scores = {}
scores["DrorStepAgent"] = {}
scores["DecentralizingAgent"] = {}
for output_step in range(0, 2):
    for n_simulations in range(10):
        test_agent = DrorStepAgent
        # test_agent.set_param(test_agent, steps_forward=output_step)
        world = SCML2020World(
            **SCML2020World.generate([test_agent, DecentralizingAgent],
                                     n_steps=10),
            construct_graphs=True,
        )

        world.run()
        returned_scores = return_agent_scores(world)
        print("dror:%s" % returned_scores["DrorStepAgent"])
        print("agent:%s" % returned_scores["DecentralizingAgent"])

        if scores["DrorStepAgent"].get(output_step):
            scores["DrorStepAgent"][output_step] += returned_scores[
                "DrorStepAgent"]
        else:
            scores["DrorStepAgent"][output_step] = returned_scores[
                "DrorStepAgent"]
Example #14
0
    #    self.inputs_needed[threshold:] = self.inputs_needed[threshold:]*0


if __name__ == "__main__":
    from collections import defaultdict

    def show_agent_scores(world):
        scores = defaultdict(list)
        for aid, score in world.scores().items():
            scores[world.agents[aid].__class__.__name__.split(".")[-1]].append(score)
        scores = {k: sum(v) / len(v) for k, v in scores.items()}
        plt.bar(list(scores.keys()), list(scores.values()), width=0.2)
        plt.show()

    import matplotlib.pyplot as plt

    world = SCML2020World(
        **SCML2020World.generate(
            [MixedNegAgent, DecentralizingAgent],
            n_steps=30,
            n_processes=2,
            n_agents_per_process=2,
            log_stats_every=1,
        ),
        construct_graphs=True,
    )
    world.run_with_progress()
    world.draw(steps=(0, world.n_steps), together=False, ncols=2, figsize=(20, 20))
    plt.show()
    show_agent_scores(world)
scores = {}
wins = {}
scores["DrorStepAgent"] = {}
scores["DecentralizingAgent"] = {}
scores["IndDecentralizingAgent"] = {}
exec_fraction = 0.1
for output_step in range(3, 8):
    for n_simulations in range(10):
        # test_agent = DrorStepAgent(exec=0.1)
        # test_agent = DrorStepAgent
        # test_agent.set_param(test_agent, exec_fraction=output_step/10)
        exec_fraction = output_step / 10
        world = SCML2020World(
            **SCML2020World.generate(
                [DrorStepAgent, DecentralizingAgent, IndDecentralizingAgent], n_steps=10
            ),
            construct_graphs=True,
        )

        world.run()
        returned_scores = return_agent_scores(world)
        winner = (
            "DrorStepAgent"
            if returned_scores.get("DrorStepAgent", -20)
            >= returned_scores.get("DecentralizingAgent", -20)
            else "DecentralizingAgent"
        )
        print(
            "step:%s, simulation:%s, winner_is:%s"
            % (output_step, n_simulations, winner)
Example #16
0
        super().trade_prediction_init()
        self.expected_inputs = 0 * self.expected_inputs


if __name__ == "__main__":
    from collections import defaultdict

    def show_agent_scores(world):
        scores = defaultdict(list)
        for aid, score in world.scores().items():
            scores[world.agents[aid].__class__.__name__.split(".")[-1]].append(
                score)
        scores = {k: sum(v) / len(v) for k, v in scores.items()}
        plt.bar(list(scores.keys()), list(scores.values()), width=0.2)
        plt.show()

    world = SCML2020World(**SCML2020World.generate(
        [DecentralizingAgent, MyAgent5, MyAgent6],
        n_steps=10,
        n_processes=3,
        n_agents_per_process=2,
    ),
                          construct_graphs=True)
    world.run_with_progress()
    world.draw(steps=(0, world.n_steps),
               together=False,
               ncols=2,
               figsize=(20, 20))
    plt.show()
    show_agent_scores(world)