예제 #1
0
    def build_agents(self,
                     AgentClass,
                     number=None,
                     group_name=None,
                     agent_parameters=None):
        """ This method creates agents, the first parameter is the agent class.
        "num_agent_class" (e.G. "num_firm") should be difined in
        simulation_parameters.csv. Alternatively you can also specify number = 1.s

        Args::

         AgentClass:
            is the name of the AgentClass that you imported
        number (optional):
            number of agents to be created. or the colum name
            of the row in simulation_parameters.csv that contains this number. If not
            specified the column name is assumed to be 'num_' + agent_name
            (all lowercase). For example num_firm, if the class is called
            Firm or name = Firm.
         group_name (optional):
            to give the group a different name than the
            class_name.
        agent_parameters:
            a dictionary of agent parameters to be given to the agent

        Example::

         w.build_agents(Firm, number='num_firms')
         # 'num_firms' is a column in simulation_parameters.csv
         w.build_agents(Bank, 1)
         w.build_agents(CentralBank, number=1)
        """
        # TODO single agent groups get extra name without number
        # TODO when there is a group with a single agent the ask_agent has a confusingname
        if not (group_name):
            group_name = AgentClass.__name__.lower()
        if number and not (agent_parameters):
            try:
                num_agents_this_group = int(number)
            except ValueError:
                try:
                    num_agents_this_group = self.simulation_parameters[number]
                except KeyError:
                    SystemExit(
                        'build_agents ' + group_name + ': ' + number +
                        ' is not a number or a column name in simulation_parameters.csv'
                        'or the parameterfile you choose')
        elif not (number) and not (agent_parameters):
            try:
                num_agents_this_group = self.simulation_parameters[
                    'num_' + group_name.lower()]
            except KeyError:
                raise SystemExit('num_' + group_name.lower() +
                                 ' is not in simulation_parameters.csv')
        elif not (number) and agent_parameters:
            num_agents_this_group = len(agent_parameters)
        else:
            raise SystemExit(
                'build_agents ' + group_name + ': Either '
                'number_or_parameter_column or agent_parameters must be'
                'specied, NOT both.')
        if not (agent_parameters):
            agent_parameters = [None] * num_agents_this_group

        self.simulation_parameters['num_' +
                                   group_name.lower()] = num_agents_this_group

        self.num_agents += num_agents_this_group
        self.num_agents_in_group[group_name] = num_agents_this_group
        self.num_agents_in_group['all'] = self.num_agents
        self.agents_list[group_name] = []

        for idn in range(num_agents_this_group):
            agent = AgentClass(
                simulation_parameters=self.simulation_parameters,
                agent_parameters=agent_parameters[idn],
                name=agent_name(group_name, idn),
                idn=idn,
                group=group_name,
                trade_logging=self.trade_logging_mode,
                database=self.database_queue,
                logger=self.logger_queue)
            agent.init(self.simulation_parameters, agent_parameters)

            for good in self.perishable:
                agent._register_perish(good)
            for resource, units, product in self.resource_endowment[
                    group_name] + self.resource_endowment['all']:
                agent._register_resource(resource, units, product)

            agent._register_panel(self.possessins_to_track_panel[group_name],
                                  self.variables_to_track_panel[group_name])

            agent._register_aggregate(
                self.possessions_to_track_aggregate[group_name],
                self.variables_to_track_aggregate[group_name])

            try:
                agent._network_drawing_frequency = self._network_drawing_frequency
            except AttributeError:
                agent._network_drawing_frequency = None

            for good, duration in self.expiring:
                agent._declare_expiring(good, duration)

            self.agents_list[group_name].append(agent)
            self.agents_list['all'].append(agent)
        self._messages[group_name] = tuple(
            [] for _ in range(num_agents_this_group))
예제 #2
0
파일: __init__.py 프로젝트: chathika/abce
    def build_agents(self, AgentClass, number=None, group_name=None, agent_parameters=None):
        """ This method creates agents, the first parameter is the agent class.
        "num_agent_class" (e.G. "num_firm") should be difined in
        simulation_parameters.csv. Alternatively you can also specify number = 1.s

        Args::

         AgentClass:
            is the name of the AgentClass that you imported
        number (optional):
            number of agents to be created. or the colum name
            of the row in simulation_parameters.csv that contains this number. If not
            specified the column name is assumed to be 'num_' + agent_name
            (all lowercase). For example num_firm, if the class is called
            Firm or name = Firm.
         group_name (optional):
            to give the group a different name than the
            class_name.
        agent_parameters:
            a dictionary of agent parameters to be given to the agent

        Example::

         w.build_agents(Firm, number='num_firms')
         # 'num_firms' is a column in simulation_parameters.csv
         w.build_agents(Bank, 1)
         w.build_agents(CentralBank, number=1)
        """
        # TODO single agent groups get extra name without number
        # TODO when there is a group with a single agent the ask_agent has a confusingname
        if not (group_name):
            group_name = AgentClass.__name__.lower()
        if number and not (agent_parameters):
            try:
                num_agents_this_group = int(number)
            except ValueError:
                try:
                    num_agents_this_group = self.simulation_parameters[number]
                except KeyError:
                    SystemExit(
                        "build_agents "
                        + group_name
                        + ": "
                        + number
                        + " is not a number or a column name in simulation_parameters.csv"
                        "or the parameterfile you choose"
                    )
        elif not (number) and not (agent_parameters):
            try:
                num_agents_this_group = self.simulation_parameters["num_" + group_name.lower()]
            except KeyError:
                raise SystemExit("num_" + group_name.lower() + " is not in simulation_parameters.csv")
        elif not (number) and agent_parameters:
            num_agents_this_group = len(agent_parameters)
        else:
            raise SystemExit(
                "build_agents " + group_name + ": Either "
                "number_or_parameter_column or agent_parameters must be"
                "specied, NOT both."
            )
        if not (agent_parameters):
            agent_parameters = [None] * num_agents_this_group

        self.simulation_parameters["num_" + group_name.lower()] = num_agents_this_group

        self.num_agents += num_agents_this_group
        self.num_agents_in_group[group_name] = num_agents_this_group
        self.num_agents_in_group["all"] = self.num_agents
        self.agents_list[group_name] = []

        for idn in range(num_agents_this_group):
            agent = AgentClass(
                simulation_parameters=self.simulation_parameters,
                agent_parameters=agent_parameters[idn],
                name=agent_name(group_name, idn),
                idn=idn,
                group=group_name,
                trade_logging=self.trade_logging_mode,
                database=self.database_queue,
                logger=self.logger_queue,
            )
            agent.init(self.simulation_parameters, agent_parameters)

            for good in self.perishable:
                agent._register_perish(good)
            for resource, units, product in self.resource_endowment[group_name] + self.resource_endowment["all"]:
                agent._register_resource(resource, units, product)

            agent._register_panel(self.possessins_to_track_panel[group_name], self.variables_to_track_panel[group_name])

            agent._register_aggregate(
                self.possessions_to_track_aggregate[group_name], self.variables_to_track_aggregate[group_name]
            )

            try:
                agent._network_drawing_frequency = self._network_drawing_frequency
            except AttributeError:
                agent._network_drawing_frequency = None

            for good, duration in self.expiring:
                agent._declare_expiring(good, duration)

            self.agents_list[group_name].append(agent)
            self.agents_list["all"].append(agent)
        self._messages[group_name] = tuple([] for _ in range(num_agents_this_group))
예제 #3
0
    def build_agents(self, AgentClass,  number=None, group_name=None, agents_parameters=None):
        """ This method creates agents, the first parameter is the agent class.
        "num_agent_class" (e.G. "num_firm") should be difined in
        simulation_parameters.csv. Alternatively you can also specify number = 1.s

        Args::

         AgentClass: is the name of the AgentClass that you imported
         number (optional): number of agents to be created. or the colum name
         of the row in simulation_parameters.csv that contains this number. If not
         specified the column name is assumed to be 'num_' + agent_name
         (all lowercase). For example num_firm, if the class is called
         Firm or name = Firm.
         [group_name (optional): to give the group a different name than the
         class_name. (do not use this if you have not a specific reason]

        Example::

         w.build_agents(Firm, number='num_firms')
         # 'num_firms' is a column in simulation_parameters.csv
         w.build_agents(Bank, 1)
         w.build_agents(CentralBank, number=1)
        """
        #TODO single agent groups get extra name without number
        #TODO when there is a group with a single agent the ask_agent has a confusingname
        if not(group_name):
            group_name = AgentClass.__name__.lower()
        if number and not(agents_parameters):
            try:
                num_agents_this_group = int(number)
            except ValueError:
                try:
                    num_agents_this_group = self.simulation_parameters[number]
                except KeyError:
                    SystemExit('build_agents ' + group_name + ': ' + number +
                    ' is not a number or a column name in simulation_parameters.csv'
                    'or the parameterfile you choose')
        elif not(number) and not(agents_parameters):
            try:
                num_agents_this_group = self.simulation_parameters['num_' + group_name.lower()]
            except KeyError:
                raise SystemExit('num_' + group_name.lower() + ' is not in simulation_parameters.csv')
        elif not(number) and agents_parameters:
            num_agents_this_group = len(agents_parameters)
            self.simulation_parameters['num_' + group_name.lower()] = num_agents_this_group
        else:
            raise SystemExit('build_agents ' + group_name + ': Either '
                'number_or_parameter_column or agents_parameters must be'
                'specied, NOT both.')
        if not(agents_parameters):
            agents_parameters = [None for _ in range(num_agents_this_group)]

        self.num_agents += num_agents_this_group
        self.num_agents_in_group[group_name] = num_agents_this_group
        self.num_agents_in_group['all'] = self.num_agents
        self.agent_list[group_name] = []
        for idn in range(num_agents_this_group):
            agent = AgentClass(self.simulation_parameters, agents_parameters[idn], [idn, group_name, self._addresses, self.trade_logging_mode])
            agent.name = agent_name(group_name, idn)
            agent.start()
            self.agent_list[group_name].append(agent)