<reponame>maxiaoba/rlk
import csv
import os.path
import matplotlib 
matplotlib.rcParams.update({'font.size': 30})
from matplotlib import pyplot as plt
import numpy as np
import torch
import argparse
# Logger Params
parser = argparse.ArgumentParser()
parser.add_argument('--exp_name', type=str, default='zero_sum')
parser.add_argument('--mpl', type=int, default=5)
parser.add_argument('--log_dir', type=str, default='PRGGaussiank1oace')
parser.add_argument('--epoch', type=int, default=None)
parser.add_argument('--seed', type=int, default=0)
args = parser.parse_args()

pre_path = './Data/'+args.exp_name+'_mpl'+str(args.mpl)+'/'+args.log_dir

env_kwargs=dict(
    game_name=args.exp_name, 
    action_range=10.,
    max_path_length=args.mpl,
)
from sequential_differential_game import SequentialDifferentialGame
env = SequentialDifferentialGame(**env_kwargs)

a1s = np.linspace(-1,1,100)
a2s = np.linspace(-1,1,100)
c1s = []
c2s = []

if args.epoch or (args.epoch==0):
    d_path = pre_path+'/'+'seed'+str(args.seed)+'/itr_'+str(args.epoch)+'.pkl'
    plot_file = pre_path+'/'+'seed'+str(args.seed)+'/cactor_ep'+str(args.epoch)
else:
    d_path = pre_path+'/'+'seed'+str(args.seed)+'/params.pkl'
    plot_file = pre_path+'/'+'seed'+str(args.seed)+'/cactor'
data = torch.load(d_path,map_location='cpu')

if 'trainer/context_graph' in data.keys():
    context_graph = data['trainer/context_graph']
else:
    context_graph = None

if 'trainer/cactor' in data.keys():
    cactor = data['trainer/cactor']
    from rlkit.torch.policies.make_deterministic import MakeDeterministic
    cactor = MakeDeterministic(cactor)
else:
    cactor = None
    cgca = data['trainer/cgca']
    cactor_n = data['trainer/cactor_n']
    from rlkit.torch.policies.make_deterministic import MakeDeterministic
    cactor_n = [MakeDeterministic(cactor) for cactor in cactor_n]

for a2 in a2s:
    o_n = env.reset()
    cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[np.random.rand()*2.-1.],[a2]])],dim=-1).float()[None,:]
    # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[0.5],[a2]])],dim=-1).float()[None,:]
    if context_graph:
        contexts = context_graph(cg_inputs)
        c1, _ = cactor.get_action(contexts[:,0,:])
        c1s.append(c1[0])
    else:
        if cactor:
            cactions, _ = cactor.get_action(cg_inputs[0])
            c1s.append(cactions[0,:])
        else:
            contexts = cgca(cg_inputs)
            c1, _ = cactor_n[0].get_action(contexts[:,0,:])
            c1s.append(c1[0])
    
for a1 in a1s:
    o_n = env.reset()
    cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[a1],[np.random.rand()*2.-1.]])],dim=-1).float()[None,:]
    # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[a1],[0.5]])],dim=-1).float()[None,:]
    if context_graph:
        contexts = context_graph(cg_inputs)
        c2, _ = cactor.get_action(contexts[:,1,:])
        c2s.append(c2[0])
    else:
        if cactor:
            cactions, _ = cactor.get_action(cg_inputs[0])
            c2s.append(cactions[1,:])
        else:
            contexts = cgca(cg_inputs)
            c2, _ = cactor_n[1].get_action(contexts[:,1,:])
            c2s.append(c2[0])

plt.figure()
plt.plot(a2s,c1s)
plt.gca().set_aspect('equal', 'box')
plt.gca().axes.get_xaxis().set_ticks([-1.,0.,1.])
plt.gca().axes.get_yaxis().set_ticks([-1.,0.,1.])
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel('a2')
plt.ylabel('ca1')
plt.savefig(plot_file+'1.pdf', bbox_inches='tight')
plt.close()

plt.figure()
plt.plot(a1s,c2s)
plt.gca().set_aspect('equal', 'box')
plt.gca().axes.get_xaxis().set_ticks([-1.,0.,1.])
plt.gca().axes.get_yaxis().set_ticks([-1.,0.,1.])
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel('a1')
plt.ylabel('ca2')
plt.savefig(plot_file+'2.pdf', bbox_inches='tight')
plt.close()
import csv
import os.path
import matplotlib 
matplotlib.rcParams.update({'font.size': 30})
from matplotlib import pyplot as plt
import numpy as np
import torch
import argparse
# Logger Params
parser = argparse.ArgumentParser()
parser.add_argument('--exp_name', type=str, default='zero_sum')
parser.add_argument('--mpl', type=int, default=5)
parser.add_argument('--log_dir', type=str, default='PRGGaussiank1oace')
parser.add_argument('--epoch', type=int, default=None)
parser.add_argument('--seed', type=int, default=0)
args = parser.parse_args()

pre_path = './Data/'+args.exp_name+'_mpl'+str(args.mpl)+'/'+args.log_dir

env_kwargs=dict(
    game_name=args.exp_name, 
    action_range=10.,
    max_path_length=args.mpl,
)
from sequential_differential_game import SequentialDifferentialGame
env = SequentialDifferentialGame(**env_kwargs)

a1s = np.linspace(-1,1,100)
a2s = np.linspace(-1,1,100)
c1s = []
c2s = []

if args.epoch or (args.epoch==0):
    d_path = pre_path+'/'+'seed'+str(args.seed)+'/itr_'+str(args.epoch)+'.pkl'
    plot_file = pre_path+'/'+'seed'+str(args.seed)+'/cactor_ep'+str(args.epoch)
else:
    d_path = pre_path+'/'+'seed'+str(args.seed)+'/params.pkl'
    plot_file = pre_path+'/'+'seed'+str(args.seed)+'/cactor'
data = torch.load(d_path,map_location='cpu')

if 'trainer/context_graph' in data.keys():
    context_graph = data['trainer/context_graph']
else:
    context_graph = None

if 'trainer/cactor' in data.keys():
    cactor = data['trainer/cactor']
    from rlkit.torch.policies.make_deterministic import MakeDeterministic
    cactor = MakeDeterministic(cactor)
else:
    cactor = None
    cgca = data['trainer/cgca']
    cactor_n = data['trainer/cactor_n']
    from rlkit.torch.policies.make_deterministic import MakeDeterministic
    cactor_n = [MakeDeterministic(cactor) for cactor in cactor_n]

for a2 in a2s:
    o_n = env.reset()
    cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[np.random.rand()*2.-1.],[a2]])],dim=-1).float()[None,:]
    # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[0.5],[a2]])],dim=-1).float()[None,:]
    if context_graph:
        contexts = context_graph(cg_inputs)
        c1, _ = cactor.get_action(contexts[:,0,:])
        c1s.append(c1[0])
    else:
        if cactor:
            cactions, _ = cactor.get_action(cg_inputs[0])
            c1s.append(cactions[0,:])
        else:
            contexts = cgca(cg_inputs)
            c1, _ = cactor_n[0].get_action(contexts[:,0,:])
            c1s.append(c1[0])
    
for a1 in a1s:
    o_n = env.reset()
    cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[a1],[np.random.rand()*2.-1.]])],dim=-1).float()[None,:]
    # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[a1],[0.5]])],dim=-1).float()[None,:]
    if context_graph:
        contexts = context_graph(cg_inputs)
        c2, _ = cactor.get_action(contexts[:,1,:])
        c2s.append(c2[0])
    else:
        if cactor:
            cactions, _ = cactor.get_action(cg_inputs[0])
            c2s.append(cactions[1,:])
        else:
            contexts = cgca(cg_inputs)
            c2, _ = cactor_n[1].get_action(contexts[:,1,:])
            c2s.append(c2[0])

plt.figure()
plt.plot(a2s,c1s)
plt.gca().set_aspect('equal', 'box')
plt.gca().axes.get_xaxis().set_ticks([-1.,0.,1.])
plt.gca().axes.get_yaxis().set_ticks([-1.,0.,1.])
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel('a2')
plt.ylabel('ca1')
plt.savefig(plot_file+'1.pdf', bbox_inches='tight')
plt.close()

plt.figure()
plt.plot(a1s,c2s)
plt.gca().set_aspect('equal', 'box')
plt.gca().axes.get_xaxis().set_ticks([-1.,0.,1.])
plt.gca().axes.get_yaxis().set_ticks([-1.,0.,1.])
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel('a1')
plt.ylabel('ca2')
plt.savefig(plot_file+'2.pdf', bbox_inches='tight')
plt.close()
# Logger Params # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[0.5],[a2]])],dim=-1).float()[None,:] # cg_inputs = torch.cat([torch.tensor(o_n), torch.tensor([[a1],[0.5]])],dim=-1).float()[None,:]