-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- allow for deliberate meta-agents creation - allow for combinatorics - allow for dynamic agent creation fix methods-functions; add tests
- Loading branch information
Showing
12 changed files
with
612 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +0,0 @@ | ||
import logging | ||
|
||
# Configure logging | ||
logging.basicConfig( | ||
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
) | ||
|
||
# Example usage of logging | ||
logger = logging.getLogger(__name__) | ||
logger.info("Logging is configured and ready to use.") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,20 @@ | ||
import mesa | ||
from mesa.experimental.meta_agents import create_meta_agent | ||
|
||
|
||
def calculate_shapley_value(calling_agent, other_agent): | ||
""" | ||
Calculate the Shapley value of the two agents | ||
""" | ||
new_position = 1 - abs(calling_agent.position - other_agent.position) | ||
potential_utility = (calling_agent.power + other_agent.power) * 1.1 * new_position | ||
value_me = 0.5 * calling_agent.power + 0.5 * (potential_utility - other_agent.power) | ||
value_other = 0.5 * other_agent.power + 0.5 * ( | ||
potential_utility - calling_agent.power | ||
) | ||
|
||
# Determine if there is value in the alliance | ||
if value_me > calling_agent.power and value_other > other_agent.power: | ||
if other_agent.hierarchy > calling_agent.hierarchy: | ||
hierarchy = other_agent.hierarchy | ||
elif other_agent.hierarchy == calling_agent.hierarchy: | ||
hierarchy = calling_agent.hierarchy + 1 | ||
else: | ||
hierarchy = calling_agent.hierarchy | ||
|
||
return (potential_utility, new_position, hierarchy) | ||
else: | ||
return None | ||
|
||
|
||
class AllianceAgent(mesa.Agent): | ||
""" | ||
Agent has three attributes power (float), position (float) and hierarchy (int) | ||
Agent has three attributes power (float), position (float) and level (int) | ||
""" | ||
|
||
def __init__(self, model, power, position, hierarchy=0): | ||
def __init__(self, model, power, position, level=0): | ||
super().__init__(model) | ||
self.power = power | ||
self.position = position | ||
self.hierarchy = hierarchy | ||
|
||
def form_alliance(self): | ||
# Randomly select another agent of the same type | ||
other_agents = [ | ||
agent for agent in self.model.agents_by_type[type(self)] if agent != self | ||
] | ||
self.level = level | ||
|
||
# Determine if there is a beneficial alliance | ||
if other_agents: | ||
other_agent = self.random.choice(other_agents) | ||
shapley_value = calculate_shapley_value(self, other_agent) | ||
if shapley_value: | ||
class_name = f"MetaAgentHierarchy{shapley_value[2]}" | ||
meta = create_meta_agent( | ||
self.model, | ||
class_name, | ||
{other_agent, self}, | ||
meta_attributes={ | ||
"hierarchy": shapley_value[2], | ||
"power": shapley_value[0], | ||
"position": shapley_value[1], | ||
}, | ||
) | ||
""" | ||
For this demo model agent only need attributes. | ||
# Update the network if a new meta agent instance created | ||
if meta: | ||
self.model.network.add_node( | ||
meta.unique_id, | ||
size=(meta.hierarchy + 1) * 300, | ||
hierarchy=meta.hierarchy, | ||
) | ||
More complex models could have functions that define agent behavior. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.