Loading

IIT-M RL-ASSIGNMENT-2-TAXI

Solution for submission 132266

A detailed solution for submission 132266 submitted for challenge IIT-M RL-ASSIGNMENT-2-TAXI

rohan_jagdish_vashodia_cs17b025

What is the notebook about?

Problem - Taxi Environment Algorithms

This problem deals with a taxi environment and stochastic actions. The tasks you have to do are:

  • Implement Policy Iteration
  • Implement Modified Policy Iteration
  • Implement Value Iteration
  • Implement Gauss Seidel Value Iteration
  • Visualize the results
  • Explain the results

How to use this notebook? 📝

  • This is a shared template and any edits you make here will not be saved.You should make a copy in your own drive. Click the "File" menu (top-left), then "Save a Copy in Drive". You will be working in your copy however you like.

  • Update the config parameters. You can define the common variables here

Variable Description
AICROWD_DATASET_PATH Path to the file containing test data. This should be an absolute path.
AICROWD_RESULTS_DIR Path to write the output to.
AICROWD_ASSETS_DIR In case your notebook needs additional files (like model weights, etc.,), you can add them to a directory and specify the path to the directory here (please specify relative path). The contents of this directory will be sent to AIcrowd for evaluation.
AICROWD_API_KEY In order to submit your code to AIcrowd, you need to provide your account's API key. This key is available at https://www.aicrowd.com/participants/me

Setup AIcrowd Utilities 🛠

We use this to bundle the files for submission and create a submission on AIcrowd. Do not edit this block.

In [5]:
!pip install aicrowd-cli > /dev/null

AIcrowd Runtime Configuration 🧷

Get login API key from https://www.aicrowd.com/participants/me

In [8]:
import os

AICROWD_DATASET_PATH = os.getenv("DATASET_PATH", os.getcwd()+"/13d77bb0-b325-4e95-a03b-833eb6694acd_a2_taxi_inputs.zip")
AICROWD_RESULTS_DIR = os.getenv("OUTPUTS_DIR", "results")
In [9]:

API Key valid
Saved API Key successfully!
13d77bb0-b325-4e95-a03b-833eb6694acd_a2_taxi_inputs.zip: 100% 31.2k/31.2k [00:00<00:00, 295kB/s]
In [10]:
!unzip $AICROWD_DATASET_PATH
Archive:  /content/13d77bb0-b325-4e95-a03b-833eb6694acd_a2_taxi_inputs.zip
   creating: inputs/
  inflating: inputs/inputs_base.npy  
  inflating: inputs/inputs_1.npy     
  inflating: inputs/inputs_0.npy     
  inflating: inputs/inputs_2.npy     
   creating: targets/
  inflating: targets/targets_2.npy   
  inflating: targets/targets_0.npy   
  inflating: targets/targets_1.npy   
  inflating: targets/targets_base.npy  
In [11]:
DATASET_DIR = 'inputs/'

Taxi Environment

Read the environment to understand the functions, but do not edit anything

In [12]:
import numpy as np

class TaxiEnv_HW2:
    def __init__(self, states, actions, probabilities, rewards, initial_policy):
        self.possible_states = states
        self._possible_actions = {st: ac for st, ac in zip(states, actions)}
        self._ride_probabilities = {st: pr for st, pr in zip(states, probabilities)}
        self._ride_rewards = {st: rw for st, rw in zip(states, rewards)}
        self.initial_policy = initial_policy
        self._verify()

    def _check_state(self, state):
        assert state in self.possible_states, "State %s is not a valid state" % state

    def _verify(self):
        """ 
        Verify that data conditions are met:
        Number of actions matches shape of next state and actions
        Every probability distribution adds up to 1 
        """
        ns = len(self.possible_states)
        for state in self.possible_states:
            ac = self._possible_actions[state]
            na = len(ac)

            rp = self._ride_probabilities[state]
            assert np.all(rp.shape == (na, ns)), "Probabilities shape mismatch"
        
            rr = self._ride_rewards[state]
            assert np.all(rr.shape == (na, ns)), "Rewards shape mismatch"

            assert np.allclose(rp.sum(axis=1), 1), "Probabilities don't add up to 1"

    def possible_actions(self, state):
        """ Return all possible actions from a given state """
        self._check_state(state)
        return self._possible_actions[state]

    def ride_probabilities(self, state, action):
        """ 
        Returns all possible ride probabilities from a state for a given action
        For every action a list with the returned with values in the same order as self.possible_states
        """
        actions = self.possible_actions(state)
        ac_idx = actions.index(action)
        return self._ride_probabilities[state][ac_idx]

    def ride_rewards(self, state, action):
        actions = self.possible_actions(state)
        ac_idx = actions.index(action)
        return self._ride_rewards[state][ac_idx]

Example of Environment usage

In [13]:
def check_taxienv():
    # These are the values as used in the pdf, but they may be changed during submission, so do not hardcode anything

    states = ['A', 'B', 'C']

    actions = [['1','2','3'], ['1','2'], ['1','2','3']]

    probs = [np.array([[1/2,  1/4,  1/4],
                    [1/16, 3/4,  3/16],
                    [1/4,  1/8,  5/8]]),

            np.array([[1/2,   0,     1/2],
                    [1/16,  7/8,  1/16]]),

            np.array([[1/4,  1/4,  1/2],
                    [1/8,  3/4,  1/8],
                    [3/4,  1/16, 3/16]]),]

    rewards = [np.array([[10,  4,  8],
                        [ 8,  2,  4],
                        [ 4,  6,  4]]),

            np.array([[14,  0, 18],
                        [ 8, 16,  8]]),

            np.array([[10,  2,  8],
                        [6,   4,  2],
                        [4,   0,  8]]),]
    initial_policy = {'A': '1', 'B': '1', 'C': '1'}

    env = TaxiEnv_HW2(states, actions, probs, rewards, initial_policy)
    print("All possible states", env.possible_states)
    print("All possible actions from state B", env.possible_actions('B'))
    print("Ride probabilities from state A with action 2", env.ride_probabilities('A', '2'))
    print("Ride rewards from state C with action 3", env.ride_rewards('C', '3'))

    base_kwargs = {"states": states, "actions": actions, 
                "probabilities": probs, "rewards": rewards,
                "initial_policy": initial_policy}
    return base_kwargs

base_kwargs = check_taxienv()
env = TaxiEnv_HW2(**base_kwargs)
All possible states ['A', 'B', 'C']
All possible actions from state B ['1', '2']
Ride probabilities from state A with action 2 [0.0625 0.75   0.1875]
Ride rewards from state C with action 3 [4 0 8]

Task 1 - Policy Iteration

Run policy iteration on the environment and generate the policy and expected reward

In [14]:
# 1.1 Policy Iteration
def policy_iteration(taxienv, gamma):
    # A list of all the states
    states = taxienv.possible_states
    # Initial values
    values = {s: 0 for s in states}

    # This is a dictionary of states to policies -> e.g {'A': '1', 'B': '2', 'C': '1'}
    p = taxienv.initial_policy.copy()

    ## Begin code here

    # Hints - 
    # Do not hardcode anything
    # Only the final result is required for the results
    # Put any extra data in "extra_info" dictonary for any plots etc
    # Use the helper functions taxienv.ride_rewards, taxienv.ride_probabilities,  taxienv.possible_actions
    # For terminating condition use the condition exactly mentioned in the pdf
    all_values = [] # Append the "values" dictionary to this after each update
    all_policies = [] # Append the "policy" dictionary to this after each update
    
    # Put your extra information needed for plots etc in this dictionary
    extra_info = []
    done=False
    while(done==False):
     
      while(1):
        lamb=0
        extra_info.append(values.copy())
        for s in states:
          j=values[s]
          
          values[s] = sum(taxienv.ride_probabilities(s, p[s])*(taxienv.ride_rewards(s, p[s]) + list(gamma*np.array(list(values.values())))))
          
          lamb=max(lamb,abs(j-values[s]))
          #print(lamb)
        #print(lamb)
        if(lamb<0.00000001):
            break
      done=True
      for s in states:
        b=p[s]
        actions=taxienv.possible_actions(s)
        sum_dict = {a : 0 for a in actions}
        for a in actions:
          sum_dict[a] += sum(taxienv.ride_probabilities(s, a)*(taxienv.ride_rewards(s, a) + list(gamma*np.array(list(values.values())))))
        temp=float('-inf') 
        for i in sum_dict.keys():
          if(sum_dict[i]>temp):
            p[s]=i
            temp=sum_dict[i]
        if(b!=p[s]):
          done=False

    ## Do not edit below this line
    
    # Final results
    return {"Expected Reward": values, "Policy": p}, extra_info

Task 2 - Policy Iteration for multiple values of gamma

Ideally this code should run as is

In [15]:
# 1.2 Policy Iteration with different values of gamma
def run_policy_iteration(env):
    gamma_values = np.arange(5, 100, 5)/100
    results, extra_info = {}, {}
    for gamma in gamma_values:
        results[gamma], extra_info[gamma] = policy_iteration(env, gamma)
    return results, extra_info

results, extra_info = run_policy_iteration(env)

Task 3 - Modifed Policy Iteration

Implement modified policy iteration (where Value iteration is done for fixed m number of steps)

In [31]:
# 1.3 Modified Policy Iteration
def modified_policy_iteration(taxienv, gamma, m):
    # A list of all the states
    states = taxienv.possible_states
    # Initial values
    values = {s: 0 for s in states}
    
    # This is a dictionary of states to policies -> e.g {'A': '1', 'B': '2', 'C': '1'}
    p = taxienv.initial_policy.copy()
    
    ## Begin code here

    # Hints - 
    # Do not hardcode anything
    # Only the final result is required for the results
    # Put any extra data in "extra_info" dictonary for any plots etc
    # Use the helper functions taxienv.ride_rewards, taxienv.ride_probabilities,  taxienv.possible_actions
    # For terminating condition use the condition exactly mentioned in the pdf
    all_values = [] # Append the "values" dictionary to this after each update
    all_policies = [] # Append the "policy" dictionary to this after each update
    
    # Put your extra information needed for plots etc in this dictionary
    extra_info =[]
    done=False
    while(done==False):
      
      for itr in range(0,m+1):
        for s in states:
          j=values[s]
          extra_info.append(values.copy())
          values[s] = sum(taxienv.ride_probabilities(s, p[s])*(taxienv.ride_rewards(s, p[s]) + list(gamma*np.array(list(values.values())))))
      done=True
      for s in states:
        b=p[s]
        actions=taxienv.possible_actions(s)
        sum_dict = {a : 0 for a in actions}
        for a in actions:
          sum_dict[a] += sum(taxienv.ride_probabilities(s, a)*(taxienv.ride_rewards(s, a) + list(gamma*np.array(list(values.values())))))
        temp=float('-inf') 
        for i in sum_dict.keys():
          if(sum_dict[i]>temp):
            p[s]=i
            temp=sum_dict[i]
        if(b!=p[s]):
          done=False

    
    ## Do not edit below this line


    # Final results
    return {"Expected Reward": values, "Policy": p}, extra_info

Task 4 Modified policy iteration for multiple values of m

Ideally this code should run as is

In [33]:
def run_modified_policy_iteration(env):
    m_values = np.arange(1, 15)
    gamma = 0.9
    results, extra_info = {}, {}
    for m in m_values:
        results[m], extra_info[m] = modified_policy_iteration(env, gamma, m)
    return results, extra_info

results, extra_info_mpi = run_modified_policy_iteration(env)

Task 5 Value Iteration

Implement value iteration and find the policy and expected rewards

In [18]:
# 1.4 Value Iteration
def value_iteration(taxienv, gamma):


  # A list of all the states
  states = taxienv.possible_states
  # Initial values
  values = {s: 0 for s in states}

  # This is a dictionary of states to policies -> e.g {'A': '1', 'B': '2', 'C': '1'}
  p = taxienv.initial_policy.copy()

  ## Begin code here

  # Hints - 
  # Do not hardcode anything
  # Only the final result is required for the results
  # Put any extra data in "extra_info" dictonary for any plots etc
  # Use the helper functions taxienv.ride_rewards, taxienv.ride_probabilities,  taxienv.possible_actions
  # For terminating condition use the condition exactly mentioned in the pdf
  all_values = [] # Append the "values" dictionary to this after each update
  all_policies = [] # Append the "policy" dictionary to this after each update
  
  # Put your extra information needed for plots etc in this dictionary
  extra_info = []
  
  H={s: 0 for s in states}
  l=0

  while(1):
    #print(values)
    l=0
    extra_info.append(values.copy())
    for s in states:
      actions=taxienv.possible_actions(s)
      sum_dict = {a : 0 for a in actions}
      temp=float('-inf')
      #print(actions,len(actions))
      for a in actions:
        t1=taxienv.ride_probabilities(s, a)
        t2=taxienv.ride_rewards(s, a)
        for i in range(len(states)):
          sum_dict[a] += t1[i]*(t2[i]+gamma*values[states[i]])
        
        if(sum_dict[a]>temp):
          temp=sum_dict[a]
          p[s]=a
      #print(sum_dict,p[s],temp)
      H[s]=temp
      l=max(l,abs(values[s]-H[s]))
    for s in states:
      values[s]=H[s]
    if(l<0.00000001):
      break
    #print(values)
    
    #print("s")
  #print(values)
  return {"Expected Reward": values, "Policy": p}, extra_info

Task 6 Value Iteration with multiple values of gamma

Ideally this code should run as is

In [19]:
def run_value_iteration(env):
    gamma_values = np.arange(5, 100, 5)/100
    results = {}
    results, extra_info1 = {}, {}
    for gamma in gamma_values:
        results[gamma], extra_info1[gamma] = value_iteration(env, gamma)
    return results, extra_info
  
results, extra_info1 = run_value_iteration(env)
(results)
Out[19]:
{0.05: {'Expected Reward': {'A': 8.511527294182539,
   'B': 16.400259908653545,
   'C': 7.498869066334438},
  'Policy': {'A': '1', 'B': '1', 'C': '1'}},
 0.1: {'Expected Reward': {'A': 9.076506148654659,
   'B': 16.856368562663413,
   'C': 8.050865123013633},
  'Policy': {'A': '1', 'B': '1', 'C': '1'}},
 0.15: {'Expected Reward': {'A': 9.708121491779233,
   'B': 17.46450304126533,
   'C': 8.669160452818193},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.2: {'Expected Reward': {'A': 10.43703007317419,
   'B': 18.482142855128945,
   'C': 9.384398494226822},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.25: {'Expected Reward': {'A': 11.274074070814656,
   'B': 19.629629626369894,
   'C': 10.207407404147988},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.3: {'Expected Reward': {'A': 12.243837242020097,
   'B': 20.934065932248725,
   'C': 11.162756160939015},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.35: {'Expected Reward': {'A': 13.378714431634197,
   'B': 22.43076922615468,
   'C': 12.282824020675294},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.4: {'Expected Reward': {'A': 14.722222216827301,
   'B': 24.16666666127172,
   'C': 13.611111105716189},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.45: {'Expected Reward': {'A': 16.33413126569031,
   'B': 26.20553359269582,
   'C': 15.207370702310026},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.5: {'Expected Reward': {'A': 18.298701293190973,
   'B': 28.636363630853307,
   'C': 17.15584415033383},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.55: {'Expected Reward': {'A': 20.789988627490125,
   'B': 31.607396689157056,
   'C': 19.830725204761144},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.6: {'Expected Reward': {'A': 24.02568643325821,
   'B': 35.327723634321096,
   'C': 23.45881309401995},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.65: {'Expected Reward': {'A': 28.276692058162972,
   'B': 40.09628057326413,
   'C': 28.12997877946505},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.7: {'Expected Reward': {'A': 34.06193076174113,
   'B': 46.43541615019445,
   'C': 34.36604100170312},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.75: {'Expected Reward': {'A': 42.317411373067756,
   'B': 55.28505390003232,
   'C': 43.106317382312746},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.8: {'Expected Reward': {'A': 55.07936504724612,
   'B': 68.55820102608209,
   'C': 56.26984123772232},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.85: {'Expected Reward': {'A': 77.24651209325512,
   'B': 90.81170061393325,
   'C': 78.43345571343316},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.9: {'Expected Reward': {'A': 121.65347103895924,
   'B': 135.30627543932593,
   'C': 122.83690299162194},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.95: {'Expected Reward': {'A': 255.02290824365164,
   'B': 268.76461832126836,
   'C': 256.2028492466015},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}}}

Task 7 Gauss Seidel Value Iteration

Implement Gauss Seidel Value Iteration

In [20]:
# 1.4 Gauss Seidel Value Iteration
def gauss_seidel_value_iteration(taxienv, gamma):
    # A list of all the states
    states = taxienv.possible_states
    # Initial values
    values = {s: 0 for s in states}

    # This is a dictionary of states to policies -> e.g {'A': '1', 'B': '2', 'C': '1'}
    p = taxienv.initial_policy.copy()

    ## Begin code here

    # Hints - 
    # Do not hardcode anything
    # Only the final result is required for the results
    # Put any extra data in "extra_info" dictonary for any plots etc
    # Use the helper functions taxienv.ride_rewards, taxienv.ride_probabilities,  taxienv.possible_actions
    # For terminating condition use the condition exactly mentioned in the pdf
    all_values = [] # Append the "values" dictionary to this after each update
    all_policies = [] # Append the "policy" dictionary to this after each update
    
    # Put your extra information needed for plots etc in this dictionary
    extra_info = []
    H={s: float('-inf') for s in states}
  
    while(1):
      l=0
      extra_info.append(values.copy())
      for s in states:
        j_old=values[s]
        actions=taxienv.possible_actions(s)
        sum_dict = {a : 0 for a in actions}
        temp=float('-inf') 
        for a in actions:
          sum_dict[a] = sum(taxienv.ride_probabilities(s, a)*(taxienv.ride_rewards(s, a) + list(gamma*np.array(list(values.values())))))
          if(sum_dict[a]>temp):
            temp=sum_dict[a]
            p[s]=a
        values[s]=temp
        l=max(l,abs(values[s]-j_old))
        #print(l)
      if(l<0.00000001):
        break
    
    return {"Expected Reward": values, "Policy": p}, extra_info
In [20]:

Task 8 Gauss Seidel Value Iteration with multiple values of gamma

Ideally this code should run as is

In [21]:
def run_gauss_seidel_value_iteration(env):
    gamma_values = np.arange(5, 100, 5)/100
    results = {}
    results, extra_info2 = {}, {}
    for gamma in gamma_values:
        results[gamma], extra_info[gamma] = gauss_seidel_value_iteration(env, gamma)
    return results, extra_info

results, extra_info2 = run_gauss_seidel_value_iteration(env)
results
Out[21]:
{0.05: {'Expected Reward': {'A': 8.511527294546923,
   'B': 16.400259909029575,
   'C': 7.4988690667106095},
  'Policy': {'A': '1', 'B': '1', 'C': '1'}},
 0.1: {'Expected Reward': {'A': 9.076506149392834,
   'B': 16.85636856362452,
   'C': 8.050865123980312},
  'Policy': {'A': '1', 'B': '1', 'C': '1'}},
 0.15: {'Expected Reward': {'A': 9.70812149293256,
   'B': 17.464503042356245,
   'C': 8.66916045411395},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.2: {'Expected Reward': {'A': 10.43703007363936,
   'B': 18.48214285510763,
   'C': 9.384398495833036},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.25: {'Expected Reward': {'A': 11.274074072135488,
   'B': 19.62962962714145,
   'C': 10.207407406785403},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.3: {'Expected Reward': {'A': 12.243837240938772,
   'B': 20.934065930435885,
   'C': 11.162756161665108},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.35: {'Expected Reward': {'A': 13.3787144345583,
   'B': 22.43076922870161,
   'C': 12.28282402456309},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.4: {'Expected Reward': {'A': 14.722222217898851,
   'B': 24.166666661495807,
   'C': 13.611111109029693},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.45: {'Expected Reward': {'A': 16.33413126434644,
   'B': 26.20553359041331,
   'C': 15.207370703537933},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.5: {'Expected Reward': {'A': 18.298701293837997,
   'B': 28.63636363077955,
   'C': 17.155844153026397},
  'Policy': {'A': '1', 'B': '2', 'C': '1'}},
 0.55: {'Expected Reward': {'A': 20.78998863222275,
   'B': 31.60739669290458,
   'C': 19.830725212339292},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.6: {'Expected Reward': {'A': 24.02568643692515,
   'B': 35.32772363671855,
   'C': 23.458813101474426},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.65: {'Expected Reward': {'A': 28.276692061545695,
   'B': 40.09628057546244,
   'C': 28.129978786496867},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.7: {'Expected Reward': {'A': 34.06193076743594,
   'B': 46.43541615465501,
   'C': 34.36604101132381},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.75: {'Expected Reward': {'A': 42.31741137784243,
   'B': 55.28505390328474,
   'C': 43.10631739208525},
  'Policy': {'A': '1', 'B': '2', 'C': '2'}},
 0.8: {'Expected Reward': {'A': 55.079365051578144,
   'B': 68.5582010300939,
   'C': 56.26984124730355},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.85: {'Expected Reward': {'A': 77.24651209951202,
   'B': 90.81170061982581,
   'C': 78.43345572578708},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.9: {'Expected Reward': {'A': 121.65347104963263,
   'B': 135.30627544959796,
   'C': 122.83690300915262},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}},
 0.95: {'Expected Reward': {'A': 255.0229082673451,
   'B': 268.7646183445272,
   'C': 256.20284927787594},
  'Policy': {'A': '2', 'B': '2', 'C': '2'}}}

Generate Results ✅

In [22]:
# Do not edit this cell
def get_results(kwargs):

    taxienv = TaxiEnv_HW2(**kwargs)

    policy_iteration_results = run_policy_iteration(taxienv)[0]
    modified_policy_iteration_results = run_modified_policy_iteration(taxienv)[0]
    value_iteration_results = run_value_iteration(taxienv)[0]
    gs_vi_results = run_gauss_seidel_value_iteration(taxienv)[0]

    final_results = {}
    final_results["policy_iteration"] = policy_iteration_results
    final_results["modifed_policy_iteration"] = modified_policy_iteration_results
    final_results["value_iteration"] = value_iteration_results
    final_results["gauss_seidel_iteration"] = gs_vi_results

    return final_results
In [23]:
# Do not edit this cell, generate results with it as is
if not os.path.exists(AICROWD_RESULTS_DIR):
    os.mkdir(AICROWD_RESULTS_DIR)

for params_file in os.listdir(DATASET_DIR):
  kwargs = np.load(os.path.join(DATASET_DIR, params_file), allow_pickle=True).item()
  results = get_results(kwargs)
  idx = params_file.split('_')[-1][:-4]
  np.save(os.path.join(AICROWD_RESULTS_DIR, 'results_' + idx), results)

Check your local score

This score is not your final score, and it doesn't use the marks weightages. This is only for your reference of how arrays are matched and with what tolerance.

In [24]:
# Check your score on the given test cases (There are more private test cases not provided)
target_folder = 'targets'
result_folder = AICROWD_RESULTS_DIR

def check_algo_match(results, targets):
    param_matches = []
    for k in results:
        param_results = results[k]
        param_targets = targets[k]
        policy_match = param_results['Policy'] == param_targets['Policy']
        rv = [v for k, v in param_results['Expected Reward'].items()]
        tv = [v for k, v in param_targets['Expected Reward'].items()]
        rewards_match = np.allclose(rv, tv, rtol=3)
        equal = rewards_match and policy_match
        param_matches.append(equal)
    return np.mean(param_matches)

def check_score(target_folder, result_folder):
    match = []
    for out_file in os.listdir(result_folder):
        res_file = os.path.join(result_folder, out_file)
        results = np.load(res_file, allow_pickle=True).item()
        idx = out_file.split('_')[-1][:-4]  # Extract the file number
        target_file = os.path.join(target_folder, f"targets_{idx}.npy")
        targets = np.load(target_file, allow_pickle=True).item()
        algo_match = []
        for k in targets:
            algo_results = results[k]
            algo_targets = targets[k]
            algo_match.append(check_algo_match(algo_results, algo_targets))
        match.append(np.mean(algo_match))
    return np.mean(match)

if os.path.exists(target_folder):
    print("Shared data Score (normalized to 1):", check_score(target_folder, result_folder))
Shared data Score (normalized to 1): 0.9955357142857143

Visualize results of Policy Iteration with multiple values of gamma

Add code to visualize the results

In [43]:
import matplotlib.pyplot as plt
#lamda =0 
extra_info_pi=extra_info[0.05]
diffs1 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs1.append(diff)

#lamda =0.25
extra_info_pi=extra_info[0.25]
diffs2 = []

for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs2.append(diff)


#lamda =0.5
extra_info_pi=extra_info[0.5]
diffs3 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs3.append(diff)
#lamda =0.75
extra_info_pi=extra_info[0.75]
diffs4 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs4.append(diff)

#lamda =0.90
extra_info_pi=extra_info[0.90]
diffs5 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs5.append(diff)
plt.plot(diffs1)
plt.plot(diffs2)
plt.plot(diffs3)
plt.plot(diffs4)
plt.plot(diffs5)
plt.xlabel('iterations')
plt.ylabel('Loss')
plt.legend(["lamdda 0.0", "lamdda 0.25","lamdda 0.50","lamdda 0.75","lamdda 0.90"])
plt.show()

Subjective questions

1.a How are values of $\gamma$ affecting results of policy iteration

Modify this cell/add code for your answer.

If we increase gamma , Number of iterations required for convergence increases

1.b For modified policy itetaration, do you find any improvement if you choose m=10.

Explain your answer.Error plot for different values of m is given below.As m increases ,Convergence to optimal value decrease

In [38]:
import matplotlib.pyplot as plt
#m =3 
extra_info_pi=extra_info_mpi[3]
diffs1 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs1.append(diff)

#m =6
extra_info_pi=extra_info_mpi[6]
diffs2 = []

for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs2.append(diff)


#lamda =9
extra_info_pi=extra_info_mpi[9]
diffs3 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs3.append(diff)
#lamda =10
extra_info_pi=extra_info_mpi[10]
diffs4 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs4.append(diff)

#lamda =12
extra_info_pi=extra_info_mpi[12]
diffs5 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs5.append(diff)
plt.plot(diffs1)
plt.plot(diffs2)
plt.plot(diffs3)
plt.plot(diffs4)
plt.plot(diffs5)
plt.xlabel('iterations')
plt.ylabel('loss')
plt.legend(["m = 3", "m = 6","m =9","m = 10","m = 12"])
plt.show()

1.c Compare and contrast the behavior of Value Iteration and Gauss Seidel Value Iteraton

Add code/markdown as required

The error plots for both the algorithms coincide

In [39]:
#error plot
#gamma =0.05
import matplotlib.pyplot as plt
extra_info_1=extra_info1[0.95]
extra_info_2=extra_info2[0.95]

diffs1 = []
for ii in range(0,len(extra_info_1)):
    diff = np.linalg.norm(np.array(list(extra_info_1[ii].values())) - np.array(list(extra_info_1[len(extra_info_1)-1].values())))/10
    diffs1.append(diff)
diffs2 = []
for ii in range(0,len(extra_info_2)):
    diff = np.linalg.norm(np.array(list(extra_info_2[ii].values())) - np.array(list(extra_info_2[len(extra_info_2)-1].values())))/10
    diffs2.append(diff)
plt.plot(diffs1,'r')
plt.plot(diffs2,'b')
plt.legend(["VI", "Gauss Seidel"])
plt.show()
print(diffs1)
print(diffs2)
[45.04564393792667, 42.33693600940437, 39.85920568851596, 37.51348670754064, 35.36643143006773, 33.36765010330956, 31.48417941267985, 29.70728572729977, 28.030708235224495, 26.448755218355224, 24.956082524661543, 23.54765098545368, 22.2187062660882, 20.964762407555433, 19.781586629897173, 18.665185037041425, 17.611789134177258, 16.617843106802507, 15.679991817796889, 14.795069481910414, 13.960088979424711, 13.172231772913062, 12.428838393062302, 11.727399461440658, 11.06554721990834, 10.44104753807768, 9.851792371843498, 9.295792647526852, 8.77117154761218, 8.276158175413203, 7.809081577282363, 7.368365102185244, 6.952521079600272, 6.560145797778676, 6.189914765413373, 5.840578240722183, 5.51095701285358, 5.199938421374617, 4.906472600404749, 4.629568934717144, 4.368292715844948, 4.121761986904844, 3.889144565487448, 3.669655234565025, 3.462553091934317, 3.2671390492472527, 3.0827534721873717, 2.9087739538261936, 2.7446132136433725, 2.589717115118561, 2.4435627952032863, 2.3056568993586968, 2.1755339162014202, 2.0527546061360082, 1.9369045186697829, 1.8275925934050044, 1.724449839986045, 1.6271280925456266, 1.5352988344456133, 1.448652089345183, 1.3668953748531405, 1.2897527152323147, 1.2169637098234367, 1.148282654043723, 1.0834777099931796, 1.0223301238689335, 0.9646334875458578, 0.9101930418309088, 0.8588250190392998, 0.8103560226733538, 0.7646224421099527, 0.7214699003210192, 0.6807527327626272, 0.6423334956737051, 0.6060825021246535, 0.5718773842496827, 0.5396026801851592, 0.5091494443196946, 0.4804148795403013, 0.4533019902332735, 0.42771925486842727, 0.40358031706152797, 0.3808036940720251, 0.35931250175213647, 0.3390341950188101, 0.31990032297252763, 0.30184629783629785, 0.28481117693493985, 0.2687374569786334, 0.2535708799563896, 0.2392602499841631, 0.22575726048946748, 0.21301633114899024, 0.20099445402891383, 0.18965104840848251, 0.17894782379684265, 0.1688486506807117, 0.1593194385665806, 0.15032802090575398, 0.1418440465138397, 0.1338388771181065, 0.12628549068692166, 0.11915839021492483, 0.11243351765600859, 0.10608817271365145, 0.1001009362144075, 0.09445159780594047, 0.08912108773548504, 0.0840914124785247, 0.07934559400036117, 0.07486761244547772, 0.07064235206138222, 0.06665555017428357, 0.06289374904439725, 0.05934425043835604, 0.055995072765416265, 0.05283491063269021, 0.049853096683013534, 0.04703956558650386, 0.044384820064263864, 0.04187989882966793, 0.039516346338867726, 0.03728618424849544, 0.03518188448417711, 0.03319634382898044, 0.031322859945999953, 0.02955510875408862, 0.027887123080498573, 0.02631327251826298, 0.024828244420316208, 0.02342702596627826, 0.022104887241331937, 0.020857365270038408, 0.0196802489512277, 0.018569564843132096, 0.017521563750750056, 0.01653270807016756, 0.01559965984709051, 0.014719269509370239, 0.013888565235374961, 0.013104742922398573, 0.012365156721211, 0.011667310104778924, 0.011008847441091771, 0.010387546041516873, 0.00980130865799733, 0.009248156403586892, 0.008726222072621352, 0.008233743837797652, 0.007769059303018718, 0.007330599891823747, 0.006916885552563484, 0.006526519762403192, 0.0061581848132228985, 0.005810637363604114, 0.005482704241835128, 0.005173278485827994, 0.00488131560642167, 0.004605830061680414, 0.004345891930115022, 0.004100623771646331, 0.003869197665766152, 0.0036508324168148623, 0.0034447909170189526, 0.0032503776583021867, 0.003066936384559161, 0.0028938478764356824, 0.0027305278610729013, 0.0025764250398885137, 0.0024310192275884463, 0.0022938195962843044, 0.002164363018634632, 0.0020422125045553184, 0.0019269557260881884, 0.0018182036255929284, 0.0017155891024523144, 0.0016187657738729623, 0.0015274068056720187, 0.0014412038089910754, 0.0013598657993561072, 0.001283118214396035, 0.0012107019870600275, 0.0011423726710938875, 0.0010778996159296207, 0.0010170651880780936, 0.000959664036500041, 0.0009055023994306275, 0.0008543974503317248, 0.0008061766807195512, 0.0007606773178891194, 0.0007177457754242716, 0.0006772371347861504, 0.0006390146561118497, 0.0006029493166386391, 0.0005689193751921676, 0.0005368099612229218, 0.0005065126870731605, 0.0004779252820900111, 0.00045095124741144214, 0.00042549953021821935, 0.0004014842163955018, 0.0003788242404959512, 0.0003574431121239124, 0.00033726865772803154, 0.00031823277696490105, 0.00030027121283433106, 0.00028332333476206197, 0.0002673319339703194, 0.00025224303031863815, 0.0002380056901146664, 0.0002245718541760388, 0.0002118961756112495, 0.00019993586675103804, 0.00018865055469202008, 0.00017800214504941442, 0.0001679546933482269, 0.0001584742836812186, 0.00014952891423159116, 0.00014108838925096158, 0.00013312421714293206, 0.0001256095142567754, 0.00011851891416157911, 0.0001118284820373434, 0.00010551563383911071, 9.955906008538328e-05, 9.393865392363571e-05, 8.863544327085787e-05, 8.363152674341936e-05, 7.891001326595753e-05, 7.445496502228908e-05, 7.025134368143127e-05, 6.62849596118641e-05, 6.254242398967902e-05, 5.901110361185755e-05, 5.5679078251805114e-05, 5.2535100413311784e-05, 4.95685573751111e-05, 4.676943535600831e-05, 4.4128285719476604e-05, 4.163619305971284e-05, 3.928474515141999e-05, 3.706600450655532e-05, 3.497248160158876e-05, 3.299710959977108e-05, 3.1133220486006035e-05, 2.9374522570112488e-05, 2.7715079238195278e-05, 2.6149288911951346e-05, 2.4671866159939255e-05, 2.327782384279926e-05, 2.1962456256480623e-05, 2.0721323285770035e-05, 1.955023539702191e-05, 1.844523949438667e-05, 1.7402605584117223e-05, 1.641881418177678e-05, 1.549054442012788e-05, 1.461466286912928e-05, 1.3788212916453165e-05, 1.3008404826263101e-05, 1.2272606305228122e-05, 1.157833361065603e-05, 1.0923243170822048e-05, 1.030512367807102e-05, 9.721888623484925e-06, 9.17156925551271e-06, 8.652307937248456e-06, 8.162351857097302e-06, 7.70004713546111e-06, 7.263833233578723e-06, 6.852237675754556e-06, 6.463871080170153e-06, 6.097422500722958e-06, 5.751654968085058e-06, 5.425401308565704e-06, 5.11756022648717e-06, 4.827092595700445e-06, 4.553017910608679e-06, 4.294411013277047e-06, 4.050398956386684e-06, 3.820158066583744e-06, 3.602911140514265e-06, 3.397924850447992e-06, 3.2045072506046124e-06, 3.0220054368818984e-06, 2.8498033816413383e-06, 2.6873197851350117e-06, 2.5340061817149696e-06, 2.389345048814797e-06, 2.2528480651737104e-06, 2.1240544748860205e-06, 2.0025295324581744e-06, 1.8878630261843492e-06, 1.7796678875067768e-06, 1.6775788959617274e-06, 1.5812514371028143e-06, 1.490360349636643e-06, 1.4045988311463853e-06, 1.3236773840983757e-06, 1.2473228424491913e-06, 1.1752774804197936e-06, 1.1072981004408242e-06, 1.043155229612294e-06, 9.826323550832084e-07, 9.255251730238041e-07, 8.716409148019961e-07, 8.207976938808816e-07, 7.728238811866447e-07, 7.275575426020174e-07, 6.848458752398286e-07, 6.44544693293618e-07, 6.065179604332096e-07, 5.706373166652696e-07, 5.367816572738389e-07, 5.04836687370683e-07, 4.746945742810117e-07, 4.462535776634553e-07, 4.1941768746351174e-07, 3.940963218448527e-07, 3.7020400016723265e-07, 3.4766007677655927e-07, 3.263884485013283e-07, 3.0631731197709035e-07, 2.8737892138023275e-07, 2.6950934902343285e-07, 2.526482662002114e-07, 2.3673876225693227e-07, 2.2172713450863513e-07, 2.0756271276551244e-07, 1.9419766895252488e-07, 1.8158690658195017e-07, 1.6968785040034966e-07, 1.584603328178411e-07, 1.4786645472727788e-07, 1.3787045891666263e-07, 1.2843860393203105e-07, 1.1953904229300772e-07, 1.1114174193223058e-07, 1.0321835790695108e-07, 9.57421445931601e-08, 8.868784919097894e-08, 8.203168342691312e-08, 7.575116911140514e-08, 6.982509446449488e-08, 6.42334710248154e-08, 5.8957420052019726e-08, 5.397912932975767e-08, 4.9281791126017995e-08, 4.4849554763761884e-08, 4.0667457756117393e-08, 3.6721382600405644e-08, 3.299801939258338e-08, 2.9484782253712095e-08, 2.616983035072065e-08, 2.3041956459065875e-08, 2.009060648354705e-08, 1.7305815726783475e-08, 1.4678189185432673e-08, 1.2198856558866697e-08, 9.85945064730527e-09, 7.652072748745217e-09, 5.569271222167108e-09, 3.6040243510721583e-09, 1.749686271932644e-09, 0.0]
[45.04564393792667, 42.33693600940437, 39.85920568851596, 37.51348670754064, 35.36643143006773, 33.36765010330956, 31.48417941267985, 29.70728572729977, 28.030708235224495, 26.448755218355224, 24.956082524661543, 23.54765098545368, 22.2187062660882, 20.964762407555433, 19.781586629897173, 18.665185037041425, 17.611789134177258, 16.617843106802507, 15.679991817796889, 14.795069481910414, 13.960088979424711, 13.172231772913062, 12.428838393062302, 11.727399461440658, 11.06554721990834, 10.44104753807768, 9.851792371843498, 9.295792647526852, 8.77117154761218, 8.276158175413203, 7.809081577282363, 7.368365102185244, 6.952521079600272, 6.560145797778676, 6.189914765413373, 5.840578240722183, 5.51095701285358, 5.199938421374617, 4.906472600404749, 4.629568934717144, 4.368292715844948, 4.121761986904844, 3.889144565487448, 3.669655234565025, 3.462553091934317, 3.2671390492472527, 3.0827534721873717, 2.9087739538261936, 2.7446132136433725, 2.589717115118561, 2.4435627952032863, 2.3056568993586968, 2.1755339162014202, 2.0527546061360082, 1.9369045186697829, 1.8275925934050044, 1.724449839986045, 1.6271280925456266, 1.5352988344456133, 1.448652089345183, 1.3668953748531405, 1.2897527152323147, 1.2169637098234367, 1.148282654043723, 1.0834777099931796, 1.0223301238689335, 0.9646334875458578, 0.9101930418309088, 0.8588250190392998, 0.8103560226733538, 0.7646224421099527, 0.7214699003210192, 0.6807527327626272, 0.6423334956737051, 0.6060825021246535, 0.5718773842496827, 0.5396026801851592, 0.5091494443196946, 0.4804148795403013, 0.4533019902332735, 0.42771925486842727, 0.40358031706152797, 0.3808036940720251, 0.35931250175213647, 0.3390341950188101, 0.31990032297252763, 0.30184629783629785, 0.28481117693493985, 0.2687374569786334, 0.2535708799563896, 0.2392602499841631, 0.22575726048946748, 0.21301633114899024, 0.20099445402891383, 0.18965104840848251, 0.17894782379684265, 0.1688486506807117, 0.1593194385665806, 0.15032802090575398, 0.1418440465138397, 0.1338388771181065, 0.12628549068692166, 0.11915839021492483, 0.11243351765600859, 0.10608817271365145, 0.1001009362144075, 0.09445159780594047, 0.08912108773548504, 0.0840914124785247, 0.07934559400036117, 0.07486761244547772, 0.07064235206138222, 0.06665555017428357, 0.06289374904439725, 0.05934425043835604, 0.055995072765416265, 0.05283491063269021, 0.049853096683013534, 0.04703956558650386, 0.044384820064263864, 0.04187989882966793, 0.039516346338867726, 0.03728618424849544, 0.03518188448417711, 0.03319634382898044, 0.031322859945999953, 0.02955510875408862, 0.027887123080498573, 0.02631327251826298, 0.024828244420316208, 0.02342702596627826, 0.022104887241331937, 0.020857365270038408, 0.0196802489512277, 0.018569564843132096, 0.017521563750750056, 0.01653270807016756, 0.01559965984709051, 0.014719269509370239, 0.013888565235374961, 0.013104742922398573, 0.012365156721211, 0.011667310104778924, 0.011008847441091771, 0.010387546041516873, 0.00980130865799733, 0.009248156403586892, 0.008726222072621352, 0.008233743837797652, 0.007769059303018718, 0.007330599891823747, 0.006916885552563484, 0.006526519762403192, 0.0061581848132228985, 0.005810637363604114, 0.005482704241835128, 0.005173278485827994, 0.00488131560642167, 0.004605830061680414, 0.004345891930115022, 0.004100623771646331, 0.003869197665766152, 0.0036508324168148623, 0.0034447909170189526, 0.0032503776583021867, 0.003066936384559161, 0.0028938478764356824, 0.0027305278610729013, 0.0025764250398885137, 0.0024310192275884463, 0.0022938195962843044, 0.002164363018634632, 0.0020422125045553184, 0.0019269557260881884, 0.0018182036255929284, 0.0017155891024523144, 0.0016187657738729623, 0.0015274068056720187, 0.0014412038089910754, 0.0013598657993561072, 0.001283118214396035, 0.0012107019870600275, 0.0011423726710938875, 0.0010778996159296207, 0.0010170651880780936, 0.000959664036500041, 0.0009055023994306275, 0.0008543974503317248, 0.0008061766807195512, 0.0007606773178891194, 0.0007177457754242716, 0.0006772371347861504, 0.0006390146561118497, 0.0006029493166386391, 0.0005689193751921676, 0.0005368099612229218, 0.0005065126870731605, 0.0004779252820900111, 0.00045095124741144214, 0.00042549953021821935, 0.0004014842163955018, 0.0003788242404959512, 0.0003574431121239124, 0.00033726865772803154, 0.00031823277696490105, 0.00030027121283433106, 0.00028332333476206197, 0.0002673319339703194, 0.00025224303031863815, 0.0002380056901146664, 0.0002245718541760388, 0.0002118961756112495, 0.00019993586675103804, 0.00018865055469202008, 0.00017800214504941442, 0.0001679546933482269, 0.0001584742836812186, 0.00014952891423159116, 0.00014108838925096158, 0.00013312421714293206, 0.0001256095142567754, 0.00011851891416157911, 0.0001118284820373434, 0.00010551563383911071, 9.955906008538328e-05, 9.393865392363571e-05, 8.863544327085787e-05, 8.363152674341936e-05, 7.891001326595753e-05, 7.445496502228908e-05, 7.025134368143127e-05, 6.62849596118641e-05, 6.254242398967902e-05, 5.901110361185755e-05, 5.5679078251805114e-05, 5.2535100413311784e-05, 4.95685573751111e-05, 4.676943535600831e-05, 4.4128285719476604e-05, 4.163619305971284e-05, 3.928474515141999e-05, 3.706600450655532e-05, 3.497248160158876e-05, 3.299710959977108e-05, 3.1133220486006035e-05, 2.9374522570112488e-05, 2.7715079238195278e-05, 2.6149288911951346e-05, 2.4671866159939255e-05, 2.327782384279926e-05, 2.1962456256480623e-05, 2.0721323285770035e-05, 1.955023539702191e-05, 1.844523949438667e-05, 1.7402605584117223e-05, 1.641881418177678e-05, 1.549054442012788e-05, 1.461466286912928e-05, 1.3788212916453165e-05, 1.3008404826263101e-05, 1.2272606305228122e-05, 1.157833361065603e-05, 1.0923243170822048e-05, 1.030512367807102e-05, 9.721888623484925e-06, 9.17156925551271e-06, 8.652307937248456e-06, 8.162351857097302e-06, 7.70004713546111e-06, 7.263833233578723e-06, 6.852237675754556e-06, 6.463871080170153e-06, 6.097422500722958e-06, 5.751654968085058e-06, 5.425401308565704e-06, 5.11756022648717e-06, 4.827092595700445e-06, 4.553017910608679e-06, 4.294411013277047e-06, 4.050398956386684e-06, 3.820158066583744e-06, 3.602911140514265e-06, 3.397924850447992e-06, 3.2045072506046124e-06, 3.0220054368818984e-06, 2.8498033816413383e-06, 2.6873197851350117e-06, 2.5340061817149696e-06, 2.389345048814797e-06, 2.2528480651737104e-06, 2.1240544748860205e-06, 2.0025295324581744e-06, 1.8878630261843492e-06, 1.7796678875067768e-06, 1.6775788959617274e-06, 1.5812514371028143e-06, 1.490360349636643e-06, 1.4045988311463853e-06, 1.3236773840983757e-06, 1.2473228424491913e-06, 1.1752774804197936e-06, 1.1072981004408242e-06, 1.043155229612294e-06, 9.826323550832084e-07, 9.255251730238041e-07, 8.716409148019961e-07, 8.207976938808816e-07, 7.728238811866447e-07, 7.275575426020174e-07, 6.848458752398286e-07, 6.44544693293618e-07, 6.065179604332096e-07, 5.706373166652696e-07, 5.367816572738389e-07, 5.04836687370683e-07, 4.746945742810117e-07, 4.462535776634553e-07, 4.1941768746351174e-07, 3.940963218448527e-07, 3.7020400016723265e-07, 3.4766007677655927e-07, 3.263884485013283e-07, 3.0631731197709035e-07, 2.8737892138023275e-07, 2.6950934902343285e-07, 2.526482662002114e-07, 2.3673876225693227e-07, 2.2172713450863513e-07, 2.0756271276551244e-07, 1.9419766895252488e-07, 1.8158690658195017e-07, 1.6968785040034966e-07, 1.584603328178411e-07, 1.4786645472727788e-07, 1.3787045891666263e-07, 1.2843860393203105e-07, 1.1953904229300772e-07, 1.1114174193223058e-07, 1.0321835790695108e-07, 9.57421445931601e-08, 8.868784919097894e-08, 8.203168342691312e-08, 7.575116911140514e-08, 6.982509446449488e-08, 6.42334710248154e-08, 5.8957420052019726e-08, 5.397912932975767e-08, 4.9281791126017995e-08, 4.4849554763761884e-08, 4.0667457756117393e-08, 3.6721382600405644e-08, 3.299801939258338e-08, 2.9484782253712095e-08, 2.616983035072065e-08, 2.3041956459065875e-08, 2.009060648354705e-08, 1.7305815726783475e-08, 1.4678189185432673e-08, 1.2198856558866697e-08, 9.85945064730527e-09, 7.652072748745217e-09, 5.569271222167108e-09, 3.6040243510721583e-09, 1.749686271932644e-09, 0.0]
In [39]:

In [ ]:

Submit to AIcrowd 🚀

In [40]:
!DATASET_PATH=$AICROWD_DATASET_PATH aicrowd notebook submit -c iit-m-rl-assignment-2-taxi -a assets
WARNING: No assets directory at assets... Creating one...
No jupyter lab module found. Using jupyter notebook.
Using notebook: /content/Copy%20of%20IITM_Assignment_2_Taxi_Release.ipynb for submission...
No jupyter lab module found. Using jupyter notebook.
Scrubbing API keys from the notebook...
Collecting notebook...
No jupyter lab module found. Using jupyter notebook.
Validating the submission...
Executing install.ipynb...
[NbConvertApp] Converting notebook /content/submission/install.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 1056 bytes to /content/submission/install.nbconvert.ipynb
Executing predict.ipynb...
[NbConvertApp] Converting notebook /content/submission/predict.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | unhandled iopub msg: colab_request
[NbConvertApp] ERROR | Error while converting '/content/submission/predict.ipynb'
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/nbconvertapp.py", line 408, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/exporters/exporter.py", line 179, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/exporters/exporter.py", line 197, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/exporters/notebook.py", line 32, in from_notebook_node
    nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/exporters/exporter.py", line 139, in from_notebook_node
    nb_copy, resources = self._preprocess(nb_copy, resources)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/exporters/exporter.py", line 316, in _preprocess
    nbc, resc = preprocessor(nbc, resc)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/preprocessors/base.py", line 47, in __call__
    return self.preprocess(nb, resources)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/preprocessors/execute.py", line 381, in preprocess
    nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/preprocessors/base.py", line 69, in preprocess
    nb.cells[index], resources = self.preprocess_cell(cell, resources, index)
  File "/usr/local/lib/python2.7/dist-packages/nbconvert/preprocessors/execute.py", line 424, in preprocess_cell
    raise CellExecutionError.from_cell_and_msg(cell, out)
CellExecutionError: An error occurred while executing the following cell:
------------------
import matplotlib.pyplot as plt
#lamda =0 
extra_info_pi=extra_info[0.05]
diffs1 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs1.append(diff)

#lamda =0.25
extra_info_pi=extra_info[0.25]
diffs2 = []

for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs2.append(diff)


#lamda =0.5
extra_info_pi=extra_info[0.5]
diffs3 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs3.append(diff)
#lamda =0.75
extra_info_pi=extra_info[0.75]
diffs4 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs4.append(diff)

#lamda =1
extra_info_pi=extra_info[1.0]
diffs5 = []
for ii in range(0,len(extra_info_pi)):
    diff = np.linalg.norm(np.array(list(extra_info_pi[ii].values())) - np.array(list(extra_info_pi[len(extra_info_pi)-1].values())))/10
    diffs5.append(diff)
plt.plot(diffs1)
plt.plot(diffs2)
plt.plot(diffs3)
plt.plot(diffs4)
plt.plot(diffs5)
plt.xlabel('iterations')
plt.ylabel('Loss')
plt.legend(["lamdda 0.0", "lamdda 0.25","lamdda 0.50","lamdda 0.75","lamdda 1.0"])
plt.show()
------------------

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-18-4a3a38b75069> in <module>()
     30 
     31 #lamda =1
---> 32 extra_info_pi=extra_info[1.0]
     33 diffs5 = []
     34 for ii in range(0,len(extra_info_pi)):

KeyError: 1.0
KeyError: 1.0

╭───────────────────── Traceback (most recent call last) ──────────────────────╮
 /usr/local/bin/aicrowd:8 in <module>                                         
                                                                              
   5 from aicrowd.cli import cli                                              
   6 if __name__ == '__main__':                                               
   7 │   sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])     
 8 │   sys.exit(cli())                                                      
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:829 in __call__         
                                                                              
    826 │                                                                     
    827 │   def __call__(self, *args, **kwargs):                              
    828 │   │   """Alias for :meth:`main`."""                                 
  829 │   │   return self.main(*args, **kwargs)                             
    830                                                                       
    831                                                                       
    832 class Command(BaseCommand):                                           
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:782 in main             
                                                                              
    779 │   │   try:                                                          
    780 │   │   │   try:                                                      
    781 │   │   │   │   with self.make_context(prog_name, args, **extra) as c 
  782 │   │   │   │   │   rv = self.invoke(ctx)                             
    783 │   │   │   │   │   if not standalone_mode:                           
    784 │   │   │   │   │   │   return rv                                     
    785 │   │   │   │   │   # it's not safe to `ctx.exit(rv)` here!           
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:1259 in invoke          
                                                                              
   1256 │   │   │   │   Command.invoke(self, ctx)                             
   1257 │   │   │   │   sub_ctx = cmd.make_context(cmd_name, args, parent=ctx 
   1258 │   │   │   │   with sub_ctx:                                         
 1259 │   │   │   │   │   return _process_result(sub_ctx.command.invoke(sub 
   1260 │   │                                                                 
   1261 │   │   # In chain mode we create the contexts step by step, but afte 
   1262 │   │   # base command has been invoked.  Because at that point we do 
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:1259 in invoke          
                                                                              
   1256 │   │   │   │   Command.invoke(self, ctx)                             
   1257 │   │   │   │   sub_ctx = cmd.make_context(cmd_name, args, parent=ctx 
   1258 │   │   │   │   with sub_ctx:                                         
 1259 │   │   │   │   │   return _process_result(sub_ctx.command.invoke(sub 
   1260 │   │                                                                 
   1261 │   │   # In chain mode we create the contexts step by step, but afte 
   1262 │   │   # base command has been invoked.  Because at that point we do 
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:1066 in invoke          
                                                                              
   1063 │   │   """                                                           
   1064 │   │   _maybe_show_deprecated_notice(self)                           
   1065 │   │   if self.callback is not None:                                 
 1066 │   │   │   return ctx.invoke(self.callback, **ctx.params)            
   1067                                                                       
   1068                                                                       
   1069 class MultiCommand(Command):                                          
                                                                              
 /usr/local/lib/python3.7/dist-packages/click/core.py:610 in invoke           
                                                                              
    607 │   │   args = args[2:]                                               
    608 │   │   with augment_usage_errors(self):                              
    609 │   │   │   with ctx:                                                 
  610 │   │   │   │   return callback(*args, **kwargs)                      
    611 │                                                                     
    612 │   def forward(*args, **kwargs):  # noqa: B902                       
    613 │   │   """Similar to :meth:`invoke` but fills in default keyword     
                                                                              
 /usr/local/lib/python3.7/dist-packages/aicrowd/cmd/notebook.py:92 in         
 submit_subcommand                                                            
                                                                              
    89 │   │   output,                                                        
    90 │   │   notebook_name,                                                 
    91 │   │   no_verify,                                                     
  92 │   │   dry_run,                                                       
    93 │   )                                                                  
                                                                              
 /usr/local/lib/python3.7/dist-packages/aicrowd/notebook/submit.py:88 in      
 create_submission                                                            
                                                                              
   85 ):                                                                      
   86 │   bundle_submission(assets_dir, submission_zip_path, notebook_name)   
   87 │   if not no_verify:                                                   
 88 │   │   verify_submission()                                             
   89 │   if not dry_run:                                                     
   90 │   │   submit_to_aicrowd(                                              
   91 │   │   │   challenge=challenge,                                        
                                                                              
 /usr/local/lib/python3.7/dist-packages/aicrowd/notebook/submit.py:32 in      
 verify_submission                                                            
                                                                              
   29 │   for notebook in ["install.ipynb", "predict.ipynb"]:                 
   30 │   │   print(f"Executing {notebook}...")                               
   31 │   │   if execute_command(nbconvert_cmd.format(kernel, SUBMISSION_DIR, 
 32 │   │   │   raise RuntimeError(f"{notebook} failed to execute")         
   33                                                                         
   34                                                                         
   35 def bundle_submission(                                                  
╰──────────────────────────────────────────────────────────────────────────────╯
RuntimeError: predict.ipynb failed to execute
In [ ]:

571

Comments

You must login before you can post a comment.

Execute