Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3933732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:38:04+00:00 2026-05-19T23:38:04+00:00

The world contains agents at different locations, with only a single agent at any

  • 0

The world contains agents at different locations, with only a single agent at any location. Each agent knows where he’s at, but I also need to quickly check if there’s an agent at a given location. Hence, I also maintain a map from locations to agents. I have a problem deciding where this map belongs to: class World, class Agent (as a class attribute) or elsewhere.

In the following I put the lookup table, agent_locations, in class World. But now agents have to call world.update_agent_location every time they move. This is very annoying; what if I decide later to track other things about the agents, apart from their locations – would I need to add calls back to the world object all across the Agent code?

class World:
  def __init__(self, n_agents):
    # ...
    self.agents = []
    self.agent_locations = {}
    for id in range(n_agents):
      x, y = self.find_location()
      agent = Agent(self,x,y)
      self.agents.append(agent)
      self.agent_locations[x,y] = agent
  def update_agent_location(self, agent, x, y):
    del self.agent_locations[agent.x, agent.y]
    self.agent_locations[x, y] = agent
  def update(self): # next step in the simulation
    for agent in self.agents:
      agent.update() # next step for this agent
  # ...

class Agent:
  def __init__(self, world, x, y):
    self.world = world
    self.x, self.y = x, y
  def move(self, x1, y1):
    self.world.update_agent_location(self, x1, y1)
    self.x, self.y = x1, y1
  def update():
    # find a good location that is not occupied and move there
    for x, y in self.valid_locations():
      if not self.location_is_good(x, y):
        continue
      if self.world.agent_locations[x, y]: # location occupied
        continue
      self.move(x, y)

I can instead put agent_locations in class Agent as a class attribute. But that only works when I have a single World object. If I later decide to instantiate multiple World objects, the lookup tables would need to be world-specific.

I am sure there’s a better solution…

EDIT: I added a few lines to the code to show how agent_locations is used. Note that it’s only used from inside Agent objects, but I don’t know if that would remain the case forever.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:38:05+00:00Added an answer on May 19, 2026 at 11:38 pm

    It helps with OOP to talk about the objects in terms of is a and has a. A World has a list of Agents and a list of Locations. A Location has an Agent. An Agent has a Location and a World.

    class Agent:
        def __init__(self, world):
            self.location = None
            self.world = world
    
        def move(self, new_location):
            if self.location is not None:
                self.location.agent = None
            new_location.agent = self
            self.location = new_location
    
        def update(self):
            for new_location in self.world.locations:
                if self.location_is_good(new_location):
                    self.move(new_location)
    
        def location_is_good(self, location):
            if location.agent is not None:
                return False
    
    class Location:
        def __init__(self, x, y):
            self.x = x
            self.y = y
            self.agent = None
    

    Go through the mental exercise of adding a new attribute to a Location, such as terrain, and it’s easy to see the benefits of such encapsulation. Likewise, adding new things to Agent, such as a weapon, just requires a weapon-specific function similar to move(). Note that the World doesn’t need to get involved in the move() at all. The move is handled strictly between the Agent and the Location.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HttpRequestValidationException occurs when I try post when txtBulletin contains any HTML, like Hello<br />World
I want to create a simple multi-agent system consisting of three agents. For each
I tried get code html of a web page, but the web contains some
I created a view where the first column could be any of three different
I have a large file which contains 400000 lines, each line contains many number
I have a regular expression ^[a-zA-Z+#-.0-9]{1,5}$ which validates that the word contains alpha-numeric characters
I currently have a statement which reads if(Arrays.asList(results).contains(Word)); and I want to add at
Question: Is is possible, with regex, to match a word that contains the same
How would I reverse contains in a Linq-to-SQL query so that I can check
Hi I have a file try.SPEC. This file contains a word Version: 1.0.0.1 .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.