def measurement__set_csv_writer(self, args):
        import os
        from src.bank import Bank
        from src.household import Household
        from src.firm import Firm
        from src.environment import Environment
        from src.transaction import Transaction
        from src.market import Market
        from src.runner import Runner
        from src.measurement import Measurement

        text = "This test checks measurement.set_csv_writer \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 measurement__set_csv_writer in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct household filename
        environment = Environment(environment_directory,  identifier)

        # Construct a runner
        runner = Runner(environment)

        # generate a bank
        bank = Bank()
        bank.identifier = "test_bank"
        environment.banks.append(bank)

        # generate a firm
        firm = Firm()
        firm.identifier = "test_firm"
        environment.firms.append(firm)

        # generate a household
        household = Household()
        household.identifier = "test_household"
        environment.households.append(household)

        #
        # TESTING
        #
        import csv
        file_new = open("__init__.py", "r")
        csv_writer = csv.writer(file_new, lineterminator='\n')
        measurement = Measurement(environment, runner)
        print("Measurement's csv_writer:")
        print(measurement.get_csv_writer())
        measurement.set_csv_writer(csv_writer)
        print("Measurement's csv_writer:")
        print(measurement.get_csv_writer())
    def measurement__read_xml_config_file(self, args):
        import os
        from src.bank import Bank
        from src.household import Household
        from src.firm import Firm
        from src.environment import Environment
        from src.transaction import Transaction
        from src.market import Market
        from src.runner import Runner
        from src.measurement import Measurement

        text = "This test checks measurement.read_xml_config_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 measurement__read_xml_config_file in run: %s',
                     environment_directory + identifier + ".xml")

        # Construct household filename
        environment = Environment(environment_directory,  identifier)

        # Construct a runner
        runner = Runner(environment)

        # generate a bank
        bank = Bank()
        bank.identifier = "test_bank"
        environment.banks.append(bank)

        # generate a firm
        firm = Firm()
        firm.identifier = "test_firm"
        environment.firms.append(firm)

        # generate a household
        household = Household()
        household.identifier = "test_household"
        environment.households.append(household)

        #
        # TESTING
        #
        measurement = Measurement(environment, runner)
        measurement.read_xml_config_file(environment.measurement_config)
        print("Identifier read from the config:")
        print(measurement.identifier)
Exemple #3
0
    def do_run(self, environment):
        # loop over all time steps and do the updating

        # For each update step

        measurement = Measurement(environment, self)
        measurement.open_file()

        for i in range(self.num_sweeps):

            self.current_step = i

            self.updater.do_update(environment)
            measurement.write_to_file()

        print("***\nThis run had %s sweeps and %s simulations") % (
            self.num_sweeps, environment.static_parameters['num_simulations'])
        print(
            "Check the output file that was written as csv in the measurements folder\n***"
        )

        # environment.print_parameters()

        # agent = Agent()
        # print(self.get_identifier())
        # print(self.get_num_sweeps())
        # print(environment.agents[0])
        # print(environment.agents[1])

        # parameters={'deposit_rate':-0.02}
        # agent.append_parameters(parameters)
        # print(agent.get_parameters())

        measurement.close_file()
Exemple #4
0
    def do_run(self, environment):
        # loop over all time steps and do the updating

        # For each update step

        measurement = Measurement(environment, self)
        measurement.open_file()

        for i in range(self.num_sweeps):

            self.current_step = i

            self.updater.do_update(environment)
            measurement.write_to_file()

        print("***\nThis run had %s sweeps and %s simulations" ) % (self.num_sweeps, environment.static_parameters['num_simulations'])
        print("Check the output file that was written as csv in the measurements folder\n***")

        # environment.print_parameters()

        # agent = Agent()
        # print(self.get_identifier())
        # print(self.get_num_sweeps())
        # print(environment.agents[0])
        # print(environment.agents[1])

        # parameters={'deposit_rate':-0.02}
        # agent.append_parameters(parameters)
        # print(agent.get_parameters())

        measurement.close_file()
Exemple #5
0
    def do_run(self, environment):
        # loop over all time steps and do the updating
        # For each update step

        measurement = Measurement(environment, self)
        measurement.open_file()

        for i in range(self.num_sweeps):

            self.current_step = i

            self.updater.do_update(environment, i)
            measurement.write_all_to_file()

        print("***\nThis run had %s sweeps and %s simulations") % (
            self.num_sweeps, environment.static_parameters['num_simulations'])
        print(
            "Check the output file that was written as csv in the measurements folder\n***"
        )

        measurement.close_file()
Exemple #6
0
    def do_run(self, environment):
        # loop over all time steps and do the updating

        # For each update step

        measurement = Measurement(environment, self)
        measurement.open_file()

        for i in range(self.num_sweeps):

                self.current_step = i

                self.updater.do_update(environment, i)
                measurement.write_to_file()

        print("***\nThis run had %s sweeps and %s simulations" ) % (self.num_sweeps, environment.static_parameters['num_simulations'])
        print("Check the output file that was written as csv in the measurements folder\n***")

        measurement.close_file()
Exemple #7
0
 def do_run(self, environment):
     # loop over all time steps and do the updating
     # We initialise the measurement class for writing outputs to csv
     # measurement = Measurement("Measurement", environment, self, {1: ["Step", "static", "self.runner.current_step"],
     # 2: ["Deposits", "dynamic", "self.environment.households[0].get_account", ["deposits"]]}, "TestMeasurement.csv")
     measurement = Measurement(environment, self)
     # And open the output file
     measurement.open_file()
     # For each update step
     for i in range(self.num_sweeps):
         print(environment.measurement_config)
         # the update step
         # append current step, this is mostly for measurements
         self.current_step = i
         # do the actual update
         self.updater.do_update(environment, i)
         # write the state of the system
         measurement.write_to_file()
         # HELPER, to be removed in production
         print(environment.households[0])
         print(environment.firms[0])
     # Close the output file at the end of the simulation
     measurement.close_file()
Exemple #8
0
    def do_run(self, environment):
        # loop over all time steps and do the updating

        # For each update step

        measurement = Measurement(environment, self)
        measurement.open_file()

        for i in range(self.num_sweeps):

                self.current_step = i

                self.updater.do_update(environment, i)
                measurement.write_to_file()

        print("***\nThis run had %s sweeps and %s simulations" ) % (self.num_sweeps, environment.static_parameters['num_simulations'])
        print("Check the output file that was written as csv in the measurements folder\n***")

        agent = Agent()
        for each_agent in environment.agents:
            print(agent.identifier)

        # agent = Agent()
        # print(agent.private_belief)
        # print(agent.calc_social_belief)
        # print(agent.investment_decision(environment))

        # for keys, values in agent.parameters.items():
        #     print keys, '-->', values

        # agent = Agent()
        # print(self.get_identifier())
        # print(self.get_num_sweeps())
        # print(environment.agents[0])
        # parameters={'deposit_rate':-0.02}
        # agent.append_parameters(parameters)
        # print(agent.get_parameters())

        measurement.close_file()
Exemple #9
0
    identifier = str(args[2])
    log_directory = str(args[3])
    measurement_directory = str(args[4])

    # 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 run: %s',
                 environment_directory + identifier + ".xml")

    environment = Environment()
    environment.initialize(environment_directory, identifier)
    runner = Runner()
    measurement = Measurement()

    #
    # UPDATE STEP
    #
    for i in range(environment.parameters.numSimulations):
        logging.info('  STARTED with run %s', str(i))
        environment.initialize(environment_directory, identifier)
        # check that environment file has been read correctly
        #environment.write_environment_file(identifier)
        runner.initialize(environment)
        measurement.initialize()  # clear the previous measurement

        # do the run
        runner.do_run(measurement, "info")
        # do the histograms, i.e. add the current measurement to the histogram
Exemple #10
0
 def do_run(self, environment):
     # loop over all time steps and do the updating
     # We initialise the measurement class for writing outputs to csv
     # measurement = Measurement("Measurement", environment, self, {1: ["Step", "static", "self.runner.current_step"],
     # 2: ["Deposits", "dynamic", "self.environment.households[0].get_account", ["deposits"]]}, "TestMeasurement.csv")
     measurement = Measurement(environment, self)
     # And open the output file
     measurement.open_file()
     # We start the shock class as well
     shock_class = Shock()
     # For each update step
     for i in range(self.num_sweeps):
         # Do the shock:
         # First we check if the shock occurs at the current sweep
         # Then we run the shock procedure at the start of the update
         for shock in environment.shocks:
             if int(shock[0]) <= i+1 and int(shock[1]) >= i+1:
                 shock_class.do_shock(environment, i, shock[2], "start")
         # the update step
         # append current step, this is mostly for measurements
         self.current_step = i
         # do the actual update
         self.updater.do_update(environment, i)
         # write the state of the system
         measurement.write_to_file()
         # Do the shock (revert the shock if necessary):
         # First we check if the shock occurs at the current sweep
         # Then we run the shock procedure at the end of the update
         for shock in environment.shocks:
             if int(shock[0]) <= i+1 and int(shock[1]) >= i+1:
                 shock_class.do_shock(environment, i, shock[2], "end")
         # HELPER, to be removed in production
         # for firm in environment.households:
         #    print(firm)
         # print(environment.households[0])
         # print(environment.firms[0])
         # capital = 0.0
         # for tranx in environment.firms[0].accounts:
         #     if tranx.type_ == "capital" and tranx.from_ == environment.firms[0]:
         #         capital = capital + tranx.amount
         #     if tranx.type_ == "capital" and tranx.to == environment.firms[0]:
         #         capital = capital - tranx.amount
         # print(environment.firms[0].get_account("deposits")+capital-environment.firms[0].get_account("loans"))
         # capital = 0.0
         # for tranx in environment.firms[1].accounts:
         #     if tranx.type_ == "capital" and tranx.from_ == environment.firms[1]:
         #         capital = capital + tranx.amount
         #     if tranx.type_ == "capital" and tranx.to == environment.firms[1]:
         #         capital = capital - tranx.amount
         # print(environment.firms[1].get_account("deposits")+capital-environment.firms[1].get_account("loans"))
         # capital = 0.0
         # for tranx in environment.firms[2].accounts:
         #     if tranx.type_ == "capital" and tranx.from_ == environment.firms[2]:
         #         capital = capital + tranx.amount
         #     if tranx.type_ == "capital" and tranx.to == environment.firms[2]:
         #         capital = capital - tranx.amount
         # print(environment.firms[2].get_account("deposits")+capital-environment.firms[2].get_account("loans"))
         print(environment.banks[0])
         # print(environment.firms[0])
     # Close the output file at the end of the simulation
     measurement.close_file()
Exemple #11
0
    log_directory = str(args[3])
    measurement_directory = str(args[4])

    # 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 run: %s", environment_directory + identifier + ".xml")

    environment = Environment(environment_directory, identifier)
    # environment.initialize(environment_directory,  identifier)
    runner = Runner()
    measurement = Measurement()

    #
    # UPDATE STEP
    #
    for i in range(environment.static_parameters["numSimulations"]):
        logging.info("  STARTED with run %s", str(i))
        environment.initialize(environment_directory, identifier)
        # check that environment file has been read correctly
        # environment.write_environment_file(identifier)
        runner.initialize(environment)
        measurement.initialize()  # clear the previous measurement

        # do the run
        runner.do_run(measurement, "info")
        # do the histograms, i.e. add the current measurement to the histogram
Exemple #12
0
 def convert(measurement):
     temperature, light_intensity, shutter_status, timestamp = measurement
     return Measurement(timestamp, temperature, shutter_status,
                        light_intensity)
Exemple #13
0
#        "@db:" + str(27017) + '/?authSource=admin')

client = InfluxDBClient(host='influxdb', port=8086)
# client.create_database('water1')
# client.create_database('water2')


def json_error(status, message, traceback, version):
    cherrypy.response.headers['Content-Type'] = 'application/json'
    return json.dumps({'status': status, 'message': message})


if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.response_headers.on': True,
            'tools.gzip.on': True,
            'error_page.default': json_error
        }
    }

    cherrypy.config.update({
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 5000
    })

    cherrypy.tree.mount(Measurement(client), '/api/v1/measurements', conf)
    cherrypy.engine.start()
    cherrypy.engine.block()