Esempio n. 1
0
    def consume_rationed(self, environment, time):
        # We want the consumption to be done in random pairs
        # We use rationing from market clearing class to do that
        # Price is static for this example, otherwise we can't use rationing
        # and need some other market clearing
        price = 10.0
        environment.variable_parameters["price_of_goods"] = price
        # We need a list of agents and their demand or supply
        # Supply is denoted with positive float, demand with negative float
        for_rationing = []
        # Firms give us their supply, we assume that since the goods are
        # perishable their supply is all they have in stock
        from src.helper import Helper
        helper = Helper()
        for firm in environment.firms:
            # amount = round(helper.leontief([firm.get_account("labour")], [1/firm.productivity]), 0)
            amount = helper.cobb_douglas(
                firm.get_account("labour"), firm.get_account("capital"),
                firm.total_factor_productivity, firm.labour_elasticity,
                firm.capital_elasticity) * price
            for_rationing.append([firm, amount])
        # Households give use their demand, we assume that they want to
        # consume the part of their wealth (cash and deposits) that they
        # do not want to save (determined through propensity to save)
        # We denote demand in units of the goods, so we divide the cash
        # households want to spend by price to get the demand
        for household in environment.households:
            demand = 0.0
            # demand = -round(((household.get_account("deposits") * (1 - household.propensity_to_save)) / price), 0)
            demand = -((household.get_account("deposits") *
                        (1 - household.propensity_to_save)) / price)
            # demand = -household.get_account("deposits")/price
            for_rationing.append([household, demand])
        # We import the market clearing class

        # Put the appropriate settings, i.e.
        # tolerance of error, resolution of search
        # and amplification for exponential search
        # This does not matter for rationing
        # But in principle we need to initialize
        # with these values
        market = Market("market")

        # And we find the rationing, ie the amounts
        # of goods sold between pairs of agents
        # TESTING THE ABSTRACT RATIONING
        # The matching function means that all pairs will have the same priority

        def matching_agents_basic(agent_one, agent_two):
            return 1.0

        # The below function means that all pairs are allowed

        def allow_match_basic(agent_one, agent_two):
            return True

        # We find the actual trades
        rationed = market.rationing_abstract(for_rationing,
                                             matching_agents_basic,
                                             allow_match_basic)
        # Then we go through the rationing
        # and move the goods and cash appropriately
        for ration in rationed:
            #
            #             A (from)    L (to)
            # bank        loan        deposit
            # household   goods       loan
            # firm        deposit     goods
            #
            environment.new_transaction("goods", "", ration[1].identifier,
                                        ration[0].identifier, ration[2], 0, 0,
                                        -1)
            random_bank = random.choice(environment.banks)
            environment.new_transaction("deposits", "", ration[0].identifier,
                                        random_bank.identifier,
                                        ration[2] * price,
                                        random_bank.interest_rate_deposits, 0,
                                        -1)
            environment.new_transaction("loans", "", random_bank.identifier,
                                        ration[1].identifier,
                                        ration[2] * price,
                                        random_bank.interest_rate_loans, 0, -1)
            # We print the action of selling to the screen
            print(
                "{}s sold {}d units of goods at a price {}f to {}s at time {}d."
                .format(ration[0].identifier, ration[2], price,
                        ration[1].identifier, time))
        logging.info("  goods consumed on step: %s", time)
Esempio n. 2
0
    def market__rationing_abstract(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

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

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

        # 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
        #

        def matching_agents_basic(agent_one, agent_two):
            import difflib
            seq = difflib.SequenceMatcher(a=agent_one.lower(),
                                          b=agent_two.lower())
            return seq.ratio()

        def matching_agents_basic_inv(agent_one, agent_two):
            import difflib
            seq = difflib.SequenceMatcher(a=agent_one.lower(),
                                          b=agent_two.lower())
            return 1 - seq.quick_ratio()

        def allow_match_basic(agent_one, agent_two):
            return True

            def allow_match_basic(agent_one, agent_two):
                if ((agent_one == 'aaaaaa' and agent_two == 'aaaabb')
                        or (agent_one == 'aaaabb' and agent_two == 'aaaaaa')):
                    return False
                else:
                    return True

        market = Market("market")
        rationed = market.rationing_abstract(
            [["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]],
            matching_agents_basic, allow_match_basic)
        print(
            "Pairs found through abstract rationing prioritising similar names:"
        )
        print(rationed)
        rationed = market.rationing_abstract(
            [["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]],
            matching_agents_basic_inv, allow_match_basic)
        print(
            "Pairs found through abstract rationing prioritising dissimilar names:"
        )
        print(rationed)
        rationed = market.rationing_abstract(
            [["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]],
            matching_agents_basic_inv, allow_match_basic)
        print(
            "Pairs found through abstract rationing prioritising similar names with 'aaaaaa'>'aaaabb' not allowed:"
        )
        print(rationed)
Esempio n. 3
0
    def market__rationing_abstract(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

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

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

        # 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
        #

        def matching_agents_basic(agent_one, agent_two):
            import difflib
            seq = difflib.SequenceMatcher(a=agent_one.lower(), b=agent_two.lower())
            return seq.ratio()

        def matching_agents_basic_inv(agent_one, agent_two):
            import difflib
            seq = difflib.SequenceMatcher(a=agent_one.lower(), b=agent_two.lower())
            return 1-seq.quick_ratio()

        def allow_match_basic(agent_one, agent_two):
            return True

            def allow_match_basic(agent_one, agent_two):
                if ((agent_one == 'aaaaaa' and agent_two == 'aaaabb') or (agent_one == 'aaaabb' and agent_two == 'aaaaaa')):
                    return False
                else:
                    return True

        market = Market("market")
        rationed = market.rationing_abstract([["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]], matching_agents_basic, allow_match_basic)
        print("Pairs found through abstract rationing prioritising similar names:")
        print(rationed)
        rationed = market.rationing_abstract([["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]], matching_agents_basic_inv, allow_match_basic)
        print("Pairs found through abstract rationing prioritising dissimilar names:")
        print(rationed)
        rationed = market.rationing_abstract([["aaaaaa", 5], ["bbbbbb", 7], ["aaaabb", -3], ["aabbbb", -4]], matching_agents_basic_inv, allow_match_basic)
        print("Pairs found through abstract rationing prioritising similar names with 'aaaaaa'>'aaaabb' not allowed:")
        print(rationed)