[74] | 1 | import json
|
---|
| 2 |
|
---|
| 3 | import plotly.graph_objects as go
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | def get_pareto_frontier(data):
|
---|
| 7 | x = []
|
---|
| 8 | y = []
|
---|
| 9 | pareto = data["pareto_front"]
|
---|
| 10 | for case in pareto:
|
---|
| 11 | print(case)
|
---|
| 12 | x.append(case['utility'][0])
|
---|
| 13 | y.append(case['utility'][1])
|
---|
| 14 | return x, y
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | def get_kalai(data):
|
---|
| 18 | kalai = data["kalai"]
|
---|
| 19 | return kalai["utility"][0], kalai["utility"][1]
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | def get_nash(data):
|
---|
| 23 | nash = data["nash"]
|
---|
| 24 | return nash["utility"][0], nash["utility"][1]
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | def sketch_domain(domain):
|
---|
| 28 | """
|
---|
| 29 | Sketch the domain, including the Nash product, Kalai-Smorodinsky and Pareto frontier.
|
---|
| 30 | """
|
---|
| 31 | with open(f"domains/{domain}/specials.json") as specials:
|
---|
| 32 | data = json.load(specials)
|
---|
| 33 |
|
---|
| 34 | # sketch pareto frontier
|
---|
| 35 | pareto_coords = get_pareto_frontier(data)
|
---|
| 36 | pareto_trace = go.Scatter(x=pareto_coords[0], y=pareto_coords[1],
|
---|
| 37 | mode='lines',
|
---|
| 38 | name='Pareto Frontier')
|
---|
| 39 | # sketch kalai-smorodinsky
|
---|
| 40 | kalai_coords = get_kalai(data)
|
---|
| 41 | kalai_trace = go.Scatter(x=[kalai_coords[0]], y=[kalai_coords[1]], mode='markers',
|
---|
| 42 | name='Kalai-Smorodinsky')
|
---|
| 43 | # sketch nash product
|
---|
| 44 | nash_coords = get_nash(data)
|
---|
| 45 | nash_trace = go.Scatter(x=[nash_coords[0]], y=[nash_coords[1]], mode='markers',
|
---|
| 46 | name="Nash Product")
|
---|
| 47 |
|
---|
| 48 | # set up the graph
|
---|
| 49 | fig = go.Figure(pareto_trace)
|
---|
| 50 | fig.add_trace(kalai_trace)
|
---|
| 51 | fig.add_trace(nash_trace)
|
---|
| 52 | fig.update_layout(xaxis_title="profile B utility",
|
---|
| 53 | yaxis_title="profile A utility",
|
---|
| 54 | font=dict(size=18),
|
---|
| 55 | showlegend=True,
|
---|
| 56 | legend=dict(x=0.85, y=1, font=dict(size=16, color="black")), )
|
---|
| 57 | fig.update_yaxes(range=[0, 1])
|
---|
| 58 | fig.update_xaxes(range=[0, 1])
|
---|
| 59 | fig.update_traces(marker=dict(size=18,
|
---|
| 60 | line=dict(width=2,
|
---|
| 61 | color='DarkSlateGrey')),
|
---|
| 62 | selector=dict(mode='markers')
|
---|
| 63 | )
|
---|
| 64 | return fig
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | def sketch_negotiations(domain_fig, result_trace):
|
---|
| 68 | """
|
---|
| 69 | Sketch the utilities of the bids offered by both agents during the negotiations.
|
---|
| 70 | """
|
---|
| 71 | with open(result_trace) as results:
|
---|
| 72 |
|
---|
| 73 | agent1_coords = [[], []]
|
---|
| 74 | agent2_coords = [[], []]
|
---|
| 75 | final_coords = [[], []]
|
---|
| 76 | results_data = json.load(results)
|
---|
| 77 |
|
---|
| 78 | agent1 = results_data['connections'][0]
|
---|
| 79 | agent1_name = agent1[:-2].rpartition('_')[2]
|
---|
| 80 | agent2 = results_data['connections'][1]
|
---|
| 81 | agent2_name = agent2[:-2].rpartition('_')[2]
|
---|
| 82 |
|
---|
| 83 | for action in results_data['actions']:
|
---|
| 84 | if 'Offer' in action.keys():
|
---|
| 85 | offer = action['Offer']
|
---|
| 86 | if offer['actor'] == agent1:
|
---|
| 87 | agent1_coords[0].append(list(offer["utilities"].values())[0])
|
---|
| 88 | agent1_coords[1].append(list(offer["utilities"].values())[1])
|
---|
| 89 | else:
|
---|
| 90 | agent2_coords[0].append(list(offer["utilities"].values())[0])
|
---|
| 91 | agent2_coords[1].append(list(offer["utilities"].values())[1])
|
---|
| 92 | else:
|
---|
| 93 | final_coords[0] = list(action['Accept']['utilities'].values())[0]
|
---|
| 94 | final_coords[1] = list(action['Accept']['utilities'].values())[1]
|
---|
| 95 | agent1_trace = go.Scatter(x=agent1_coords[0], y=agent1_coords[1], mode='markers+lines',
|
---|
| 96 | name=agent1_name)
|
---|
| 97 | agent2_trace = go.Scatter(x=agent2_coords[0], y=agent2_coords[1], mode='markers+lines',
|
---|
| 98 | name=agent2_name)
|
---|
| 99 | final_point = go.Scatter(x=[final_coords[0]], y=[final_coords[1]], mode='markers',
|
---|
| 100 | name="Accepted offer", marker=dict(size=15,
|
---|
| 101 | line=dict(width=2,
|
---|
| 102 | color='DarkSlateGrey')), )
|
---|
| 103 | domain_fig.add_trace(agent1_trace)
|
---|
| 104 | domain_fig.add_trace(agent2_trace)
|
---|
| 105 | domain_fig.add_trace(final_point)
|
---|
| 106 | domain_fig.update_layout(xaxis_title=f"{agent1_name} utility",
|
---|
| 107 | yaxis_title=f"{agent2_name} utility", )
|
---|
| 108 | domain_fig.show()
|
---|
| 109 |
|
---|
| 110 | return domain_fig
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | if __name__ == "__main__":
|
---|
| 114 | domain_fig = sketch_domain("domain02")
|
---|
| 115 | final_fig = sketch_negotiations(domain_fig, "results/results_trace.json")
|
---|