示例#1
0
    def agent__get_identifier(self, args):
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_identifier \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__get_identifier in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents.append(agent)

        #
        # TESTING
        #

        text = "Identifier: "
        text = text + agent.get_identifier()
        print(text)
    def transaction__purge_accounts(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config
        from sample_transaction import Transaction

        text = "This test checks transaction.purge_accounts \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s',
                            datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log",
                            level=logging.INFO)
        logging.info(
            'START logging for test transaction__purge_accounts in run: %s',
            environment_directory + identifier + ".xml")

        # Construct a config
        config = Config()

        agent_one = Agent("agent_one", {"test": "parameters"},
                          {"test": "variables"})
        agent_two = Agent("agent_two", {"test": "parameters"},
                          {"test": "variables"})

        config.agents = []
        config.agents.append(agent_one)
        config.agents.append(agent_two)

        #
        # TESTING
        #

        print("Before purging the accounts")
        transaction = Transaction()
        transaction.this_transaction("type", "asset", "agent_one", "agent_two",
                                     0, 2, 3, 4)
        transaction.add_transaction(config)
        transaction = Transaction()
        transaction.this_transaction("type", "asset", "agent_one", "agent_two",
                                     1, 2, 3, 4)
        transaction.add_transaction(config)
        print(config.get_agent_by_id("agent_one"))
        print(config.get_agent_by_id("agent_two"))
        print("After clearing one bank's accounts")
        transaction.purge_accounts(config)
        print(config.get_agent_by_id("agent_one"))
        print(config.get_agent_by_id("agent_two"))
    def transaction__write_transaction(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config
        from sample_transaction import Transaction

        text = "This test checks transaction.write_transaction \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s',
                            datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log",
                            level=logging.INFO)
        logging.info(
            'START logging for test transaction__write_transaction in run: %s',
            environment_directory + identifier + ".xml")

        # Construct a config
        config = Config()

        agent_one = Agent("agent_one", {"test": "parameters"},
                          {"test": "variables"})
        agent_two = Agent("agent_two", {"test": "parameters"},
                          {"test": "variables"})

        config.agents = []
        config.agents.append(agent_one)
        config.agents.append(agent_two)

        #
        # TESTING
        #

        print("Creating a transaction")
        transaction = Transaction()
        print("Assigning values")
        transaction.this_transaction("type", "asset", "agent_one", "agent_two",
                                     1, 2, 3, 4)
        print("Adding the transaction to the books")
        transaction.add_transaction(config)
        print("Printing transaction:")
        print(transaction.write_transaction())
示例#4
0
    def agent__get_transactions_from_file(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_transactions_from_file \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__clear_accounts in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "firm_test_config_id"
        config.agents = []
        config.agents.append(agent)
        #config.agent.get_parameters_from_file(firmFilename, config)

        #
        # TESTING
        #

        config.agents[0].accounts = []
        print("Printing agent:\n")
        print(config.agents[0])
        print("Reading transactions from the config file.\n")
        print("Printing agent: \n")
        config.agents[0].get_transactions_from_file(firm_directory + listing[0], config)
        print(config.agents[0])
示例#5
0
    def agent__get_parameters_from_file(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_parameters_from_file \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__get_parameters_from_file in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        #
        # TESTING
        #

        # test whether the parameters are read properly
        text = "Identifier has been read as follows: \n"
        text = text + "Identifier: "
        text = text + agent.identifier
        text = text + "\n"
        text = text + "Productivity: "
        text = text + str(agent.parameters["productivity"])
        print(text)
示例#6
0
track_number = 0  # track_number = 0,1,2,...,18
episode_count = 10
max_steps = 50

reward = 0
done = False
step = 0

# Generate a Torcs environment
env = TorcsEnv(vision=vision,
               throttle=throttle,
               brake=brake,
               gear_change=gear_change,
               port=port,
               track_number=track_number)
agent = Agent(env)

print("TORCS Experiment Start.")
for i in range(episode_count):
    print("Episode : " + str(i))

    if np.mod(i, 3) == 0:
        # Sometimes you need to relaunch TORCS because of the memory leak error
        ob = env.reset(relaunch=True)
    else:
        ob = env.reset()

    total_reward = 0.
    for j in range(max_steps):
        action = agent.act(ob)
from gym_torcs import TorcsEnv
from sample_agent import Agent
import numpy as np

vision = True
episode_count = 10
max_steps = 50
reward = 0
done = False
step = 0

# Generate a Torcs environment
env = TorcsEnv(vision=vision, throttle=False)

agent = Agent(1)  # steering only

print("TORCS Experiment Start.")
for i in range(episode_count):
    print("Episode : " + str(i))

    if np.mod(i, 3) == 0:
        # Sometimes you need to relaunch TORCS because of the memory leak error
        ob = env.reset(relaunch=True)
    else:
        ob = env.reset()

    # env.reset() return current state including
    #

    total_reward = 0.
    for j in range(max_steps):
示例#8
0
# -*- coding: utf-8 -*-
from Gym_LineTracer import LineTracerEnv, APPWINDOW
from sample_agent import Agent

import wx
from threading import Thread

# Define Agent And Environment
agent = Agent(2)
env = LineTracerEnv()


class SimulationLoop(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.start()  # start the thread

    def run(self):
        observation = env.reset()
        for t in range(1000):
            env.render()
            print(observation)
            action = agent.act()
            observation, reward, done, info = env.step(action)
            if done:
                print("Episode finished after {} timesteps".format(t + 1))
                break
            wx.Yield()

        env.monitor.close()
示例#9
0
 def initialize_agents(self):
     for agent_iterator in range(0,
                                 int(self.model_parameters['num_agents'])):
         temp_agent = Agent(str(agent_iterator), {}, {})
         self.agents.append(temp_agent)
reward = 0
done = False

#change directory to file path
os.chdir(os.path.dirname(os.path.abspath(__file__)))

if __name__ == "__main__":
    log_level = logging.INFO

    logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
                        level=log_level,
                        datefmt='%Y-%m-%d %H:%M:%S')

    logging.info("simstar env init")
    env = SimstarEnv(synronized_mode=True, speed_up=5, hz=1)
    agent = Agent(dim_action=3)

    logging.info("entering main loop")
    for ee in range(max_episodes):

        episode_total_reward = 0
        logging.info("reset environment. eposde number: %d", ee + 1)
        time.sleep(2)
        observation = env.reset()
        for ii in range(max_steps):
            action = agent.act(observation, reward, done)
            action[2] = 0.0
            print(ii, action)
            debug_action = [0.0, 1.0, 0.0]
            action = debug_action
            observation, reward, done, _ = env.step(action)
示例#11
0
from gym_torcs import TorcsEnv
from sample_agent import Agent
import numpy as np

vision = True
episode_count = 10
max_steps = 50
reward = 0
done = False
step = 0

# Generate a Torcs environment
env = TorcsEnv(vision=vision, throttle=False)

agent = Agent(1)  # steering only


print("TORCS Experiment Start.")
for i in range(episode_count):
    print("Episode : " + str(i))

    if np.mod(i, 3) == 0:
        # Sometimes you need to relaunch TORCS because of the memory leak error
        ob = env.reset(relaunch=True)
    else:
        ob = env.reset()

    total_reward = 0.
    for j in range(max_steps):
        action = agent.act(ob, reward, done, vision)
示例#12
0
def compute_gradient(traj, baseline, max_current_steps):
    gradient = np.zeros((n_action, n_states))
    for i in range(avg_episode):
        single_gradient = np.zeros((n_action,n_states))
        for j in range(max_current_steps):
            if j < traj[i+1][3]:
                single_traj = traj[i+1]
                single_gradient = single_gradient +  single_traj[0][j] * (single_traj[4][j] - baseline[j])   
        gradient = gradient + single_gradient
    return gradient / avg_episode

# Generate a Torcs environment
print ("Creating Torcs environment")
env = TorcsEnv(vision=vision, throttle=False)
print("Torcs env created--------------------")
agent = Agent(3)  # now we use steering only, but we might use throttle and gear

#Init theta vector
theta = np.random.normal(0, 0.01,(n_action,n_states))

performance = np.array([0])

#Baselines sum
baseline_n = 0
baseline_d = 0

#max of steps per trajectory
max_current_steps = 0

#Vector to compute gradient
traj = np.array([[0, 0, 0, 0, 0]])
示例#13
0
    def agent__purge_accounts(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.purge_accounts \n"
        text = text + "  Checking if after the purge_accounts the total amount    \n"
        text = text + "  of transactions in the firm stays the same.  \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__purge_accounts in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents = []
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        #
        # TESTING
        #

        account = 0.0
        tranx = 0

        for transaction in agent.accounts:
            account = account + transaction.amount
            tranx = tranx + 1

        print(tranx)
        print(account)

        from sample_transaction import Transaction
        transaction = Transaction()
        transaction.this_transaction("deposits", "", agent.identifier,
                                     agent.identifier, 0.0,  0.09,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        # environment.households[0:1][0] is only for testing purposes DO NOT USE IN PRODUCTION
        # what it does is is takes the first household in environment, but if there are no
        # households (which happens in testing) it doesn't break down

        account = 0.0
        tranx = 0

        for transaction in agent.accounts:
            account = account + transaction.amount
            tranx = tranx + 1

        print(tranx)
        print(account)

        agent.accounts[0].purge_accounts(config)

        account = 0.0
        tranx = 0

        for transaction in agent.accounts:
            account = account + transaction.amount
            tranx = tranx + 1

        print(tranx)
        print(account)
示例#14
0
    def agent__get_account_num_transactions(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_account_num_transactions \n"
        text = text + "  The purpose of this method is to count the numbers of transaction for   \n"
        text = text + "  accounts firms hold. Our standard frm has 3 transactions by default. \n"
        text = text + "  But we double count as we have only one agent, so you should see 6. \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__get_account_num_transactions in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents = []
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        from sample_transaction import Transaction
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("loans", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("cash", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 200.0
        transaction = Transaction()
        transaction.this_transaction("goods", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        #
        # TESTING
        #

        num_transactions = 0.0          # counting all types in account together
        print(agent)
        # and checking if the number of transaction
        # is increasing by one
        for type in ["loans",  "cash",  "goods"]:
                        if type == "loans":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L = " + str(num_transactions))
                        if type == "cash":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L+M = " + str(num_transactions))
                        if type == "goods":
                                num_transactions = num_transactions + agent.get_account_num_transactions(type)
                                print("L+M+G = " + str(num_transactions))
示例#15
0
    def agent__get_account(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.get_account \n"
        text = text + "  The purpose of this method is to establish an account for our firm which contains  \n"
        text = text + "  all kinds of assets and liabilities. The method simply adds all kinds of assets  \n"
        text = text + "  and stores them in one volume. As our firms holds 250.0 assets \n"
        text = text + "  and 250 liabilites the total volume of our account should be 500.0 \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__get_account in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents = []
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        agent_helper = Agent("helperagent id", {"test": "parameters"}, {"test": "variables"})
        agent_helper.identifier = "helper_agent"
        config.agents.append(agent_helper)

        from sample_transaction import Transaction
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("loans", "", agent.identifier, agent_helper.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 100.0
        transaction = Transaction()
        transaction.this_transaction("cash", "", agent.identifier, agent_helper.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 250.0
        transaction = Transaction()
        transaction.this_transaction("goods", "", agent.identifier, agent_helper.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)

        #
        # TESTING
        #
        print(agent.accounts)
        account = 0.0                                           # counting all types in account together
        print(agent.__str__())                                             # and checking how much is the total
        # volume of the account
        for type in ["loans",  "cash",  "goods"]:
                        if type == "loans":
                                account = account + agent.get_account(type)
                                print("L = " + str(account))
                        if type == "cash":
                                account = account + agent.get_account(type)
                                print("L+M = " + str(account))
                        if type == "goods":
                                account = account + agent.get_account(type)
                                print("L+M+G = " + str(account))
示例#16
0
    def agent__check_consistency(self, args):
        import os
        from sample_agent import Agent
        from sample_config import Config  # needed for the bankDirectory

        text = "This test checks agent.check_consistency \n"
        self.print_info(text)
        #
        # INITIALIZATION
        #
        environment_directory = str(args[0])
        identifier = str(args[1])
        log_directory = str(args[2])

        # Configure logging parameters so we get output while the program runs
        logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
                            filename=log_directory + identifier + ".log", level=logging.INFO)
        logging.info('START logging for test agent__check_consistency in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct firm filename
        config = Config()
        list_env = os.listdir(environment_directory)
        config.read_xml_config_file(environment_directory + list_env[0])
        # get the firm_directory from the environment
        firm_directory = config.firm_directory
        # and loop over all firms in the directory
        listing = os.listdir(firm_directory)
        firmFilename = firm_directory + listing[0]

        # generate an agent
        agent = Agent("baseagent id", {"test": "parameters"}, {"test": "variables"})
        agent.identifier = "test_agent"
        config.agents = []
        config.agents.append(agent)
        agent.get_parameters_from_file(firmFilename, config)

        from sample_transaction import Transaction
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("loans", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 150.0
        transaction = Transaction()
        transaction.this_transaction("cash", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        amount = 200.0
        transaction = Transaction()
        transaction.this_transaction("goods", "", agent.identifier, agent.identifier,
                                     amount,  0.0,  0, -1)
        # environment.firms[0] is only for testing purposes DO NOT USE IN PRODUCTION
        transaction.add_transaction(config)
        #
        # TESTING
        #

        print(agent.check_consistency())