[74] | 1 | import os
|
---|
| 2 | from collections import defaultdict
|
---|
| 3 |
|
---|
| 4 | import plotly.graph_objects as go
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | def plot_trace(results_trace: dict, plot_file: str):
|
---|
| 8 | utilities = defaultdict(lambda: defaultdict(lambda: {"x": [], "y": [], "bids": []}))
|
---|
| 9 | accept = {"x": [], "y": [], "bids": []}
|
---|
| 10 | for index, action in enumerate(results_trace["actions"], 1):
|
---|
| 11 | if "Offer" in action:
|
---|
| 12 | offer = action["Offer"]
|
---|
| 13 | actor = offer["actor"]
|
---|
| 14 | for agent, util in offer["utilities"].items():
|
---|
| 15 | utilities[agent][actor]["x"].append(index)
|
---|
| 16 | utilities[agent][actor]["y"].append(util)
|
---|
| 17 | utilities[agent][actor]["bids"].append(offer["bid"]["issuevalues"])
|
---|
| 18 | elif "Accept" in action:
|
---|
| 19 | offer = action["Accept"]
|
---|
| 20 | index -= 1
|
---|
| 21 | for agent, util in offer["utilities"].items():
|
---|
| 22 | accept["x"].append(index)
|
---|
| 23 | accept["y"].append(util)
|
---|
| 24 | accept["bids"].append(offer["bid"]["issuevalues"])
|
---|
| 25 |
|
---|
| 26 | fig = go.Figure()
|
---|
| 27 | fig.add_trace(
|
---|
| 28 | go.Scatter(
|
---|
| 29 | mode="markers",
|
---|
| 30 | x=accept["x"],
|
---|
| 31 | y=accept["y"],
|
---|
| 32 | name="agreement",
|
---|
| 33 | marker={"color": "green", "size": 15},
|
---|
| 34 | hoverinfo="skip",
|
---|
| 35 | )
|
---|
| 36 | )
|
---|
| 37 |
|
---|
| 38 | color = {0: "red", 1: "blue"}
|
---|
| 39 | for i, (agent, data) in enumerate(utilities.items()):
|
---|
| 40 | for actor, utility in data.items():
|
---|
| 41 | name = "_".join(agent.split("_")[-2:])
|
---|
| 42 | text = []
|
---|
| 43 | for bid, util in zip(utility["bids"], utility["y"]):
|
---|
| 44 | text.append(
|
---|
| 45 | "<br>".join(
|
---|
| 46 | [f"<b>utility: {util:.3f}</b><br>"]
|
---|
| 47 | + [f"{i}: {v}" for i, v in bid.items()]
|
---|
| 48 | )
|
---|
| 49 | )
|
---|
| 50 | fig.add_trace(
|
---|
| 51 | go.Scatter(
|
---|
| 52 | mode="lines+markers" if agent == actor else "markers",
|
---|
| 53 | x=utilities[agent][actor]["x"],
|
---|
| 54 | y=utilities[agent][actor]["y"],
|
---|
| 55 | name=f"{name} offered" if agent == actor else f"{name} received",
|
---|
| 56 | legendgroup=agent,
|
---|
| 57 | marker={"color": color[i]},
|
---|
| 58 | hovertext=text,
|
---|
| 59 | hoverinfo="text",
|
---|
| 60 | )
|
---|
| 61 | )
|
---|
| 62 |
|
---|
| 63 | fig.update_layout(
|
---|
| 64 | # width=1000,
|
---|
| 65 | height=800,
|
---|
| 66 | legend={
|
---|
| 67 | "yanchor": "bottom",
|
---|
| 68 | "y": 1,
|
---|
| 69 | "xanchor": "left",
|
---|
| 70 | "x": 0,
|
---|
| 71 | },
|
---|
| 72 | )
|
---|
| 73 | fig.update_xaxes(title_text="round", range=[0, index + 1], ticks="outside")
|
---|
| 74 | fig.update_yaxes(title_text="utility", range=[0, 1], ticks="outside")
|
---|
| 75 | print("{os.path.splitext(plot_file)[0]}.html")
|
---|
| 76 | fig.write_html(f"{os.path.splitext(plot_file)[0]}.html")
|
---|
| 77 |
|
---|
| 78 | def plot_characteristics(characteristics: dict[str, tuple[list[int], list[float], str]], n_rounds: int):
|
---|
| 79 | fig = go.Figure()
|
---|
| 80 |
|
---|
| 81 | for title, data in characteristics.items():
|
---|
| 82 | fig.add_trace(
|
---|
| 83 | go.Scatter(
|
---|
| 84 | mode="lines+markers",
|
---|
| 85 | x=data[0],
|
---|
| 86 | y=data[1],
|
---|
| 87 | name=title,
|
---|
| 88 | marker={"color": data[2],"size": 5},
|
---|
| 89 | hoverinfo="skip",
|
---|
| 90 | )
|
---|
| 91 | )
|
---|
| 92 |
|
---|
| 93 | fig.update_layout(
|
---|
| 94 | # width=1000,
|
---|
| 95 | height=800,
|
---|
| 96 | legend={
|
---|
| 97 | "yanchor": "bottom",
|
---|
| 98 | "y": 1,
|
---|
| 99 | "xanchor": "left",
|
---|
| 100 | "x": 0,
|
---|
| 101 | },
|
---|
| 102 | )
|
---|
| 103 | fig.update_xaxes(title_text="round", range=[0, n_rounds], ticks="outside")
|
---|
| 104 | fig.update_yaxes(title_text="utility", range=[0, 1], ticks="outside")
|
---|
| 105 | fig.write_html(f"characteristics.html")
|
---|