-
Notifications
You must be signed in to change notification settings - Fork 939
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
- Loading branch information
1 parent
d1baeeb
commit 5031937
Showing
6 changed files
with
126 additions
and
88 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,58 +1,68 @@ | ||
import mesa | ||
|
||
|
||
def calculate_shapley_value(self, other_agent): | ||
""" | ||
Calculate the Shapley value of the two agents | ||
""" | ||
other_agent.hierarchy = other_agent.hierarchy | ||
self.hierarchy = self.hierarchy | ||
new_position = 1-abs(self.position - other_agent.position) | ||
potential_utility = (self.power+other_agent.power)*1.1 * new_position | ||
value_me = 0.5 * self.power + 0.5 * (potential_utility - other_agent.power) | ||
value_other = 0.5 * other_agent.power + 0.5 * (potential_utility - self.power) | ||
|
||
|
||
# Determine ig there is value in the alliance | ||
if value_me > self.power and value_other > other_agent.power: | ||
if other_agent.hierarchy>self.hierarchy: | ||
hierarchy = other_agent.hierarchy | ||
elif other_agent.hierarchy==self.hierarchy: | ||
hierarchy = self.hierarchy+1 | ||
else: | ||
hierarchy = self.hierarchy | ||
|
||
return (potential_utility, new_position, hierarchy) | ||
""" | ||
Calculate the Shapley value of the two agents | ||
""" | ||
other_agent.hierarchy = other_agent.hierarchy | ||
self.hierarchy = self.hierarchy | ||
new_position = 1 - abs(self.position - other_agent.position) | ||
potential_utility = (self.power + other_agent.power) * 1.1 * new_position | ||
value_me = 0.5 * self.power + 0.5 * (potential_utility - other_agent.power) | ||
value_other = 0.5 * other_agent.power + 0.5 * (potential_utility - self.power) | ||
|
||
# Determine ig there is value in the alliance | ||
if value_me > self.power and value_other > other_agent.power: | ||
if other_agent.hierarchy > self.hierarchy: | ||
hierarchy = other_agent.hierarchy | ||
elif other_agent.hierarchy == self.hierarchy: | ||
hierarchy = self.hierarchy + 1 | ||
else: | ||
return None | ||
hierarchy = self.hierarchy | ||
|
||
return (potential_utility, new_position, hierarchy) | ||
else: | ||
return None | ||
|
||
|
||
class alliance_agent(mesa.Agent): | ||
""" | ||
Agent has three attirbutes power (float), position (float) and hierarchy (int) | ||
""" | ||
|
||
def __init__(self, model, power, position, hierarchy=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] | ||
|
||
other_agents = [ | ||
agent for agent in self.model.agents_by_type[type(self)] if agent != self | ||
] | ||
|
||
# Determine if there is a beneficial alliance | ||
if other_agents: | ||
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 = self.create_metaagent(class_name, {other_agent, self}, hierarchy=shapley_value[2], | ||
power=shapley_value[0],position=shapley_value[1]) | ||
|
||
meta = self.create_metaagent( | ||
class_name, | ||
{other_agent, self}, | ||
hierarchy=shapley_value[2], | ||
power=shapley_value[0], | ||
position=shapley_value[1], | ||
) | ||
|
||
# Update the network if a new meta agent instance created | ||
if meta: | ||
self.model.network.add_node(meta.unique_id, size =(meta.hierarchy+1)*200) | ||
self.model.new_agents=True | ||
if meta: | ||
self.model.network.add_node( | ||
meta.unique_id, size=(meta.hierarchy + 1) * 200 | ||
) | ||
self.model.new_agents = True | ||
else: | ||
self.model.new_agents=False | ||
self.model.new_agents = False | ||
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