using GameTheory using PlotlyJS # Create the payoff matrix for prisoner's dilemma pd_payoff = [9.0 1.0; 10.0 3.0] # Create two players: A and B A, B = Player(pd_payoff), Player(pd_payoff) # These two players and their payoffs define a stage game nfg = NormalFormGame((A, B)) # The repeated game is stage game plus a discount factor delta = 0.75 rpd = RepeatedGame(nfg, delta) n = 2 ls_m5_p5 = [i for i in range(-5, stop=5, length=n)] # Build traces t_s1 = scatter(;x=fill(4, n), y=ls_m5_p5, name="s_1") t_s2 = scatter(;x=ls_m5_p5, y=fill(3, n), name="s_2") t_s3 = scatter(;x=fill(-2, n), y=ls_m5_p5, name="s_3") t_s4 = scatter(;x=ls_m5_p5, y=fill(-1, n), name="s_4") # Build layout h_s1 = Dict("x"=>1, "y"=>0,"arrowhead"=>2, "arrowsize"=>1, "arrowwidth"=>2, "showarrow"=>true, "ax"=>-105, "ay"=>0) h_s2 = Dict("x"=>0, "y"=>1,"arrowhead"=>2, "arrowsize"=>1, "arrowwidth"=>2, "showarrow"=>true, "ax"=>0, "ay"=>65) h_s3 = Dict("x"=>-1, "y"=>0,"arrowhead"=>2, "arrowsize"=>1, "arrowwidth"=>2, "showarrow"=>true, "ax"=>105, "ay"=>0) h_s4 = Dict("x"=>0, "y"=>-1,"arrowhead"=>2, "arrowsize"=>1, "arrowwidth"=>2, "showarrow"=>true, "ax"=>0, "ay"=>-65) h_1 = Dict("x"=>1, "y"=>0.25,"arrowhead"=>2, "text"=>"h_1", "showarrow"=>false) h_2 = Dict("x"=>0.25, "y"=>1,"arrowhead"=>2, "text"=>"h_2", "showarrow"=>false) h_3 = Dict("x"=>-1, "y"=>0.25,"arrowhead"=>2, "text"=>"h_3", "showarrow"=>false) h_4 = Dict("x"=>0.25, "y"=>-0.75,"arrowhead"=>2, "text"=>"h_4", "showarrow"=>false) shapes = [rect(-2, 4, -1, 3, fillcolor="red", opacity=0.4, line_width=0)] l = Layout(;shapes=shapes, title="Intersection of Half-spaces", xaxis_range=[-3.5, 4.5], yaxis_range=[-3.0, 4.0], annotations=[h_s1, h_s2, h_s3, h_s4, h_1, h_2, h_3, h_4], autosize=false, width=700, height=500) plot([t_s1, t_s2, t_s3, t_s4], l) n = 20 pts = GameTheory.unitcircle(n) p = plot(scatter(), Layout(;title="Subgradients from Unit Circle", autosize=false, height=500, width=700)) for i=1:n xi, yi = pts[i, :] ti = scatter(;x=[0.0, xi], y=[0.0, yi], mode="lines", name="Subgradient $i", line_color="grey", showlegend=false) addtraces!(p, ti) end p n = 20 C, H, Z = GameTheory.initialize_sg_hpl(rpd, n) t = scatter(;x=vcat(Z'[:, 1], Z[1, 1]), y=vcat(Z'[:, 2], Z[2, 1])) l = Layout(;title="Starting Set", xaxis_title="w_1", yaxis_title="w_2", autosize=false, height=500, width=700) p = plot(t, l) pd_payoff = [9.0 1.0; 10.0 3.0] A = Player(pd_payoff) B = Player(pd_payoff) pd = NormalFormGame((A, B)) rpd = RepeatedGame(pd, 0.75) hp_pts = outerapproximation(rpd; nH=64, maxiter=500, tol=1e-8, verbose=true, nskipprint=10); # Add the first point to the bottom as well so that # the polygon connects plot_pts = vcat(hp_pts, hp_pts[1, :]') plot(scatter(;x=plot_pts[:, 1], y=plot_pts[:, 2], mode="lines+markers"), Layout(;xaxis_range=[0, 15], yaxis_range=[0, 15], title="Set of Subgame Perfect Values", xaxis_title="w_1", yaxis_title="w_2", autosize=false, height=500, width=700)) pd_payoff = [9.0 1.0 10.0 3.0] A = Player(pd_payoff) B = Player(pd_payoff) pd = NormalFormGame((A, B)) rpd1 = RepeatedGame(pd, 0.9) rpd2 = RepeatedGame(pd, 0.2475) hp_pts1 = outerapproximation(rpd1; nH=128, maxiter=750, tol=1e-8); hp_pts2 = outerapproximation(rpd2; nH=128, maxiter=750, tol=1e-8); pts_1 = vcat(hp_pts1, hp_pts1[1, :]') t1 = scatter(;x=pts_1[:, 1], y=pts_1[:, 2], mode="lines+markers", name="High Discount") pts_2 = vcat(hp_pts2, hp_pts2[1, :]') t2 = scatter(;x=pts_2[:, 1], y=pts_2[:, 2], mode="lines+markers", name="Low Discount") plot([t1, t2], Layout(;xaxis_range=[0, 12], yaxis_range=[0, 12], title="Set of Subgame Perfect Values", xaxis_title="w_1", yaxis_title="w_2", autosize=false, height=500, width=700))