<jupyter_start><jupyter_text>Imports<jupyter_code>import sys
import numpy as np
from control.controller_decentralized import Decentralized
from control.controller_decentralized_RL import DecentralizedRL
from control.controller_centralized_RL import CentralizedRL
from agents.deep_policy_gradient import DeepPolicyGradient
from agents.policy_gradient import PolicyGradient
from agents.deep_q import DQN
from agents.agent import Agent
from agents.random_agent import RandomAgent
from environments.env_wrapper import EnvWrappper<jupyter_output><empty_output><jupyter_text>Environment Setup<jupyter_code>def set_env(environment_name):
    print('Initializing environment...')

    if environment_name == 'taxi':
        sys.path.append('../environments/MultiTaxiEnv')
        from environments.MultiTaxiEnv.taxi_environment import TaxiEnv
        env = TaxiEnv(2)
        # Make sure it works with our API:
        env.agents = env.taxis_names
        env.action_spaces = {
            agent_name: env.action_space for agent_name in env.agents
        }
        env.observation_spaces = {
            agent_name: env.observation_space for agent_name in env.agents
        }
        env.possible_agents = [agent for agent in env.agents]
        needs_conv = False

    elif environment_name == 'cleanup':
        sys.path.append('../environments/cleanup')
        from environments.cleanup.social_dilemmas.envs.cleanup import CleanupEnv
        env = CleanupEnv(num_agents=5, render=True)
        env.action_spaces = {
            agent_name: env.action_space for agent_name in env.agents
        }
        env.observation_spaces = {
            agent_name: env.observation_space for agent_name in env.agents
        }
        env.possible_agents = [agent for agent in env.agents.keys()]
        needs_conv = True
        
    # Petting Zoo:
    elif environment_name == 'particle':
        from pettingzoo.mpe import simple_spread_v2
        env = simple_spread_v2.parallel_env(max_cycles=np.inf)
        needs_conv = False
        
    elif environment_name == 'piston':
        from pettingzoo.butterfly import pistonball_v4
        env = pistonball_v4.parallel_env(continuous=False)
        needs_conv = True
        
    else:
        print("Invalid Enviornment Name!")
        return
        
    return EnvWrappper(env, needs_conv=needs_conv)<jupyter_output><empty_output><jupyter_text>Choose EnvironmentIn the name variable enter one of the following options:    - MultiTaxi = 'taxi'    - Cleanup = 'cleanup'    - Particle = 'particle'    - PistonBall = 'piston'<jupyter_code>###
name = "taxi"
environment = set_env(name)<jupyter_output>Initializing environment...<jupyter_text>Decentralized  Agents Setup<jupyter_code>def create_decentralized_agent(policy_name, env):
    num_obs = env.get_num_obs()
    num_actions = env.get_num_actions()
    needs_conv = env.get_needs_conv()
    mapping_fn = lambda x: x.flatten() if not needs_conv else None
    
    if policy_name == 'pg':
        return Agent(PolicyGradient(num_actions, num_obs, mapping_fn=mapping_fn))
    
    elif policy_name == 'dpg':
        return Agent(DeepPolicyGradient(num_obs, num_actions, is_conv=needs_conv,
                                        mapping_fn=mapping_fn)) 
        
    elif policy_name == 'dqn':
        return Agent(DQN(num_obs, num_actions, is_conv=needs_conv,
                                        mapping_fn=mapping_fn)) 
        
    print("Invalid Policy!")       
    return<jupyter_output><empty_output><jupyter_text>Choose AgentsIn the policy variable enter one of the following options:    - Policy Gradient = 'pg'    - Deep Policy Gradient = 'dpg'    - Deep Q-Network = 'dqn'<jupyter_code>decentralized_policy = "pg"
env_agents = environment.get_env_agents()
decentralized_agents = {agent_name: create_decentralized_agent(decentralized_policy, environment)
          for agent_name in env_agents}<jupyter_output><empty_output><jupyter_text>Run Test<jupyter_code>controller = DecentralizedRL(environment, decentralized_agents)
controller.run(render=True, max_iteration=10, max_episode_len=25, num_episodes=10, batch_size=0)<jupyter_output>Training...
Starting training...
Finished Training
+---------+
|X: |F: :[35mX[0m|
|[41m_[0m: | : : |
| : : : : |
| | : | : |
|[34;1mX[0m|[43m_[0m:G|X: |
+---------+
Taxi1-YELLOW: Fuel: 100, Location: (4,1), Collided: False
Taxi2-RED: Fuel: 100, Location: (1,0), Collided: False
Passenger1: Location: (4, 0), Destination: (0, 4)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|[41mX[0m: |F: :[35mX[0m|
| : | : : |
| : : : : |
| | : | : |
|[34;1mX[0m|[43m_[0m:G|X: |
+---------+
  (west ,north)
Taxi1-YELLOW: Fuel: 100, Location: (4,1), Collided: False
Taxi2-RED: Fuel: 99, Location: (0,0), Collided: False
Passenger1: Location: (4, 0), Destination: (0, 4)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|[41mX[0m: |F: :[35mX[0m|
| : | : : |
| : : : : |
| | : | : |
|[34;1mX[0m|[43m_[0m:G|X: |
+---------+
  (west ,north)
Taxi1-YELLOW: Fuel: 100, Location: (4,1), Co[...]<jupyter_text>Centralized  Agent Setup<jupyter_code>def create_centralized_agent(policy_name, env):
    needs_conv = env.get_needs_conv()
    num_obs = env.get_num_obs() if needs_conv else\
        (1, env.get_num_obs()[::-1][0] * (len(env.get_env_agents())))
    num_actions = (env.get_num_actions()) ** (len(env.get_env_agents()))    
    mapping_fn = lambda x: x.flatten() if not needs_conv else None
    
    if policy_name == 'pg':
        return Agent(PolicyGradient(num_actions, num_obs, mapping_fn=mapping_fn))
    
    elif policy_name == 'dpg':
        return Agent(DeepPolicyGradient(num_obs, num_actions, is_conv=needs_conv,
                                        mapping_fn=mapping_fn)) 
        
    elif policy_name == 'dqn':
        return Agent(DQN(num_obs, num_actions, is_conv=needs_conv,
                                        mapping_fn=mapping_fn)) 
        
    print("Invalid Policy!")       
    return<jupyter_output><empty_output><jupyter_text>Choose Central AgentIn the policy variable enter one of the following options:    - Policy Gradient = 'pg'    - Deep Policy Gradient = 'dpg'    - Deep Q-Network = 'dqn'<jupyter_code>centralized_policy = "pg"
decision_maker = create_centralized_agent(centralized_policy, environment)
env_agents = environment.get_env_agents()
centralized_agents = {agent_name: None  for agent_name in env_agents}<jupyter_output><empty_output><jupyter_text>Run Test<jupyter_code>controller = CentralizedRL(environment, centralized_agents, decision_maker)
controller.run(render=True, max_iteration=10, max_episode_len=25, num_episodes=10, batch_size=0)<jupyter_output>Training...
Starting training...
Finished Training
+---------+
|X: |F:[43m_[0m:[35mX[0m|
| : | : : |
| : : : : |
|[41m_[0m| : | : |
|X| :G|[34;1mX[0m: |
+---------+
Taxi1-YELLOW: Fuel: 100, Location: (0,3), Collided: False
Taxi2-RED: Fuel: 100, Location: (3,0), Collided: False
Passenger1: Location: (4, 3), Destination: (0, 4)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|X: |F:[43m_[0m:[35mX[0m|
| : | : : |
| : : : : |
|[41m_[0m| : | : |
|X| :G|[34;1mX[0m: |
+---------+
  (pickup ,east)
Taxi1-YELLOW: Fuel: 100, Location: (0,3), Collided: False
Taxi2-RED: Fuel: 100, Location: (3,0), Collided: False
Passenger1: Location: (4, 3), Destination: (0, 4)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|X: |F:[43m_[0m:[35mX[0m|
| : | : : |
| : : : : |
|[41m_[0m| : | : |
|X| :G|[34;1mX[0m: |
+---------+
  (pickup ,east)
Taxi1-YELLOW: Fuel: 100, Location: (0,3),[...]<jupyter_text>Economic Control<jupyter_code>from control.controller_economic import EconomicControl
sys.path.append('environments/corners')
from environments.corners.corners_env import CornersEnv
from environments.corners.corners_agents import EconomicGoalAgent
env = CornersEnv()

tasks = [[0,0], [0, 4], [4, 0], [4, 4], [2,2]] # The goal position
agents = {agent: EconomicGoalAgent() for agent in env.agents}
controller = EconomicControl(env, agents, tasks)
controller.run(float('inf'))<jupyter_output>Step 1:
╔═══════════╗
║ E       B ║
║           ║
║   D       ║
║           ║
║ C       A ║
╚═══════════╝

Step 2:
╔═══════════╗
║ E       B ║
║           ║
║     D     ║
║           ║
║ C       A ║
╚═══════════╝

Finished<jupyter_text>Random Agent<jupyter_code>environment = set_env('taxi')
spaces = environment.get_env().action_spaces
agents = {
    agent_name: RandomAgent(spaces[agent_name])
    for agent_name in spaces
}
controller = Decentralized(environment, agents)
controller.run(True, 20)<jupyter_output>+---------+
|[34;1mX[0m: |F: :X|
|[43m_[0m: | :[41m_[0m: |
| : : : : |
| | : | : |
|[35mX[0m| :G|X: |
+---------+
Taxi1-YELLOW: Fuel: 100, Location: (1,0), Collided: False
Taxi2-RED: Fuel: 100, Location: (1,3), Collided: False
Passenger1: Location: (0, 0), Destination: (4, 0)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|[43m[34;1mX[0m[0m: |F: :X|
| : | : : |
| : : :[41m_[0m: |
| | : | : |
|[35mX[0m| :G|X: |
+---------+
  (north ,south)
Taxi1-YELLOW: Fuel: 99, Location: (0,0), Collided: False
Taxi2-RED: Fuel: 99, Location: (2,3), Collided: False
Passenger1: Location: (0, 0), Destination: (4, 0)
Done: False, {'taxi_1': False, 'taxi_2': False, '__all__': False}
Passengers Status's: [2]
+---------+
|[43m[43;1mX[0m[0m: |F: :X|
| : | : : |
| : : :[41m_[0m: |
| | : | : |
|[35mX[0m| :G|X: |
+---------+
  (pickup ,pickup)
Taxi1-YELLOW: Fuel: 99, Location: (0,0), Collided: False
Taxi2-RED: Fuel: 99, Location: (2,3[...]
{'fraction_non_alphanumeric': 0.16994998468919056, 'fraction_numerical': 0.029907114422782485, 'mean_word_length': 3.5785046728971963, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '420352', 'license': 'MIT', 'n_tokens_mistral': 3893, 'n_tokens_neox': 3586, 'n_words': 721, 'path': 'example_colab.ipynb', 'repo_name': 'sarah-keren/MAC', 'size': '9797'}