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

I know unicode contains all characters from most world aphabets..but what about digits? Are
Me again... I have an XML File that contains different categories which I would
Maybe world peace would be easier, but I have a problem with the widths
We've got a multi-agent Java environment where different agent would most likely produce all
A multitennant database for use by telsales agents, a table contains the prospect leads
I have a large file that contains world countries/regions that I'm seperating into smaller
I would like to declare a record in Delphi that contains the same layout
I would like to have an aspx page that contains something like.... <form id=form1
I have a solution that contains a good deal of projects, I would like
I have a web application (.war) that contains some static files (e.g. MS word

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.