1
0
mirror of https://github.com/balkian/gists.git synced 2024-11-14 14:22:28 +00:00
gists/repos/b9c4f5cec28ba28af9e186194a63c0b4/coalition.py

40 lines
1.1 KiB
Python
Raw Normal View History

2018-12-09 13:02:43 +00:00
from soil.agents import FSM, state, default_state
from random import randint
2018-12-09 15:34:59 +00:00
MAX_WEALTH = 2000
2018-12-09 13:02:43 +00:00
class CoalitionAgent(FSM):
2018-12-09 15:34:59 +00:00
2018-12-09 13:02:43 +00:00
defaults = {
'wealth': -1,
'wealth_threshold': 1000,
}
2018-12-09 15:34:59 +00:00
2018-12-09 13:02:43 +00:00
def __init__(self, *args, **kwargs):
super(CoalitionAgent, self).__init__(*args, **kwargs)
if self['wealth'] == -1:
2018-12-09 15:34:59 +00:00
self['wealth'] = int(randint(0, self.env.get('max_wealth', MAX_WEALTH)))
2018-12-09 13:02:43 +00:00
@default_state
@state
def looking_for_coalitions(self):
2018-12-09 15:34:59 +00:00
for agent in self.get_agents(state_id=self.looking_for_coalitions.id):
2018-12-09 13:02:43 +00:00
if agent['wealth'] > self['wealth_threshold']:
self.join_coalition(agent)
2018-12-09 15:34:59 +00:00
friends = list(friend.id for friend in self.get_neighboring_agents())
self.info('List of friends: {}'.format(friends))
2018-12-09 13:02:43 +00:00
return self.idle
2018-12-09 15:34:59 +00:00
2018-12-09 13:02:43 +00:00
@state
def idle(self):
# Do nothing
pass
2018-12-09 15:34:59 +00:00
2018-12-09 13:02:43 +00:00
def join_coalition(self, other):
2018-12-09 15:34:59 +00:00
# Adill.detect.badobjectsdd your methods here, like adding edges between users...
2018-12-09 13:02:43 +00:00
# You'll probably want to check if you've already joined the user first
2018-12-09 15:34:59 +00:00
self.info('Joining {}'.format(other.id))
2018-12-09 13:02:43 +00:00
self.env.add_edge(self, other)