mesa / mesa/mesa-frames

[Enhancement] : agents selection in AgentsDf would be better if it returns agent_idx/name as key

Open
#162 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
42
Forks
18
Avg merge
1m
Merged PRs (30d)
1

Description

when selecting agent from agentsDF the return comes as agent to value mapping
```python
w = model.agents["wealth"]
for k,v in w.items():
print(k)
```

Code to reproduce

```python
class ExampleAgentSet1(AgentSetPolars):
def __init__(self, model: ModelDF):
super().__init__(model)
self["wealth"] = pl.Series("wealth", [1, 2, 3, 4])
self["age"] = pl.Series("age", [10, 20, 30, 40])

def add_wealth(self, amount: int) -> None:
self["wealth"] += amount

def step(self) -> None:
self.add_wealth(1)

class ExampleAgentSet2(AgentSetPolars):
def __init__(self, model: ModelDF):
super().__init__(model)
self["wealth"] = pl.Series("wealth", [10, 20, 30, 40])
self["age"] = pl.Series("age", [11, 22, 33, 44])

def add_wealth(self, amount: int) -> None:
self["wealth"] += amount

def step(self) -> None:
self.add_wealth(2)

class ExampleAgentSet3(AgentSetPolars):
def __init__(self, model: ModelDF):
super().__init__(model)
self["age"] = pl.Series("age", [1, 2, 3, 4])
self["wealth"] = pl.Series("wealth", [1, 2, 3, 4])

def age_agents(self, amount: int) -> None:
self["age"] += amount

def step(self) -> None:
self.age_agents(1)

class ExampleModel(ModelDF):
def __init__(self, agents: AgentsDF):
super().__init__()
self.agents = agents

def step(self):
self.agents.do("step")

@pytest.fixture
def fix1_AgentSetPolars() -> ExampleAgentSet1:
return ExampleAgentSet1(ModelDF())

@pytest.fixture
def fix2_AgentSetPolars() -> ExampleAgentSet2:
return ExampleAgentSet2(ModelDF())

@pytest.fixture
def fix3_AgentSetPolars() -> ExampleAgentSet3:
return ExampleAgentSet3(ModelDF())

@pytest.fixture
def fix_AgentsDF(fix1_AgentSetPolars: ExampleAgentSet1,fix2_AgentSetPolars : ExampleAgentSet2, fix3_AgentSetPolars: ExampleAgentSet3) -> AgentsDF:
model = ModelDF()
agents = AgentsDF(model)
agents.add([fix1_AgentSetPolars,fix2_AgentSetPolars,fix3_AgentSetPolars])
return agents

@pytest.fixture
def fix1_model(fix_AgentsDF: AgentsDF) -> ExampleModel:
return ExampleModel(fix_AgentsDF)

model = fix1_model
w = model.agents["wealth"]
for k,v in w.items():
print(k)
```

please let me know if I am setting it up wrong

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the AgentsDF selection entry point used by model.agents["wealth"] and inspect how the supplied AgentSetPolars objects are mapped during selection. Run the reproduction to confirm the current keys, then define the expected agent_idx/name keys and add coverage for the returned mapping before verifying the example behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.