示例#1
0
文件: model.py 项目: foton263/swarm
    def build_environment_from_json(self):
        """Build env from jsondata."""
        jsondata = JsonData.load_json_file(filename)
        # Create a instance of JsonData to store object that
        # needs to be sent to UI
        self.render = JsonData()
        self.render.objects = {}
        # First create the agents in the environment
        for name in jsondata.keys():
            obj = eval(name.capitalize())
            self.render.objects[name] = self.create_environment_object(
                jsondata, obj)

        self.hub = self.render.objects['hub'][0]
        self.total_food_units = 0
        self.foods = []
        try:
            self.site = self.render.objects['sites'][0]
            for i in range(self.num_agents * 1):
                f = Food(i,
                         location=self.site.location,
                         radius=self.site.radius)
                f.agent_name = None
                self.grid.add_object_to_grid(f.location, f)
                self.total_food_units += f.weight
                f.phenotype = dict()
                self.foods.append(f)
        except KeyError:
            pass
示例#2
0
    def build_environment_from_json(self):
        """Build env from jsondata."""
        jsondata = JsonData.load_json_file(filename)
        # Create a instance of JsonData to store object that
        # needs to be sent to UI
        self.render = JsonData()
        self.render.objects = {}

        for name in jsondata.keys():
            obj = eval(name.capitalize())
            self.render.objects[name] = self.create_environment_object(
                jsondata, obj)

        self.hub = self.render.objects['hub'][0]
        try:
            self.foods = []
            for site in self.render.objects['sites']:
                self.site = site  # self.render.objects['sites'][0]

                for i in range(self.num_agents):
                    f = Food(
                        i, location=self.site.location,
                        radius=self.site.radius)
                    f.agent_name = None
                    self.grid.add_object_to_grid(f.location, f)
                    self.foods.append(f)
        except KeyError:
            pass

        if self.viewer:
            self.ui = UI(
                (self.width, self.height), [self.hub], self.agents,
                [self.site], food=self.foods)
示例#3
0
    def __init__(self, N, width, height, grid=10, seed=None):
        if seed is None:
            super(XMLEnvironmentModel, self).__init__(seed=None)
        else:
            super(XMLEnvironmentModel, self).__init__(seed)

        self.num_agents = N

        self.grid = Grid(width, height, grid)

        self.schedule = SimultaneousActivation(self)

        self.site = Sites(id=1, location=(45, 45), radius=5, q_value=0.5)

        self.grid.add_object_to_grid(self.site.location, self.site)

        self.hub = Hub(id=1, location=(0, 0), radius=11)

        self.grid.add_object_to_grid(self.hub.location, self.hub)

        self.agents = []

        # Create agents
        for i in range(self.num_agents):
            a = XMLTestAgent(i, self)
            self.schedule.add(a)
            x = 45
            y = 45

            a.location = (x, y)
            self.grid.add_object_to_grid((x, y), a)
            a.operation_threshold = 2  # self.num_agents // 10
            self.agents.append(a)

        # Add equal number of food source
        for i in range(5):  # self.num_agents * 2):
            f = Food(i, location=(45, 45), radius=3)
            f.agent_name = None
            self.grid.add_object_to_grid(f.location, f)