From c9c55ac60ba4fe5f07b43493e15db683be4ca105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Sun, 9 Dec 2018 14:02:43 +0100 Subject: [PATCH 1/2] --- coalition.py | 34 ++++++++++++++++++++++++++++++++++ coalition.yml | 5 +++++ 2 files changed, 39 insertions(+) create mode 100644 coalition.py create mode 100644 coalition.yml diff --git a/coalition.py b/coalition.py new file mode 100644 index 0000000..d87c457 --- /dev/null +++ b/coalition.py @@ -0,0 +1,34 @@ +from soil.agents import FSM, state, default_state +from random import randint + +MAX_WEALTH = 10e6 + +class CoalitionAgent(FSM): + defaults = { + 'wealth': -1, + 'wealth_threshold': 1000, + } + + def __init__(self, *args, **kwargs): + super(CoalitionAgent, self).__init__(*args, **kwargs) + if self['wealth'] == -1: + self['wealth'] = randint(0, self.env.get('max_wealth', MAX_WEALTH)) + + + @default_state + @state + def looking_for_coalitions(self): + for agent in self.get_agents(): + if agent['wealth'] > self['wealth_threshold']: + self.join_coalition(agent) + return self.idle + + @state + def idle(self): + # Do nothing + pass + + def join_coalition(self, other): + # Add your methods here, like adding edges between users... + # You'll probably want to check if you've already joined the user first + self.env.add_edge(self, other) diff --git a/coalition.yml b/coalition.yml new file mode 100644 index 0000000..ec4f33d --- /dev/null +++ b/coalition.yml @@ -0,0 +1,5 @@ +name: CoalitionExample +agent_type: coalition.CoalitionAgent +network_params: + generator: empty_graph + n: 100 \ No newline at end of file From 1eef636a589af65022228b3a594ad8d058dd120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Sun, 9 Dec 2018 16:34:59 +0100 Subject: [PATCH 2/2] Update example --- coalition.py | 23 ++++++++++++++--------- coalition.yml | 3 ++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/coalition.py b/coalition.py index d87c457..103342f 100644 --- a/coalition.py +++ b/coalition.py @@ -1,34 +1,39 @@ from soil.agents import FSM, state, default_state from random import randint -MAX_WEALTH = 10e6 +MAX_WEALTH = 2000 + class CoalitionAgent(FSM): + defaults = { 'wealth': -1, 'wealth_threshold': 1000, } - + def __init__(self, *args, **kwargs): super(CoalitionAgent, self).__init__(*args, **kwargs) if self['wealth'] == -1: - self['wealth'] = randint(0, self.env.get('max_wealth', MAX_WEALTH)) - - + self['wealth'] = int(randint(0, self.env.get('max_wealth', MAX_WEALTH))) + + @default_state @state def looking_for_coalitions(self): - for agent in self.get_agents(): + for agent in self.get_agents(state_id=self.looking_for_coalitions.id): if agent['wealth'] > self['wealth_threshold']: self.join_coalition(agent) + friends = list(friend.id for friend in self.get_neighboring_agents()) + self.info('List of friends: {}'.format(friends)) return self.idle - + @state def idle(self): # Do nothing pass - + def join_coalition(self, other): - # Add your methods here, like adding edges between users... + # Adill.detect.badobjectsdd your methods here, like adding edges between users... # You'll probably want to check if you've already joined the user first + self.info('Joining {}'.format(other.id)) self.env.add_edge(self, other) diff --git a/coalition.yml b/coalition.yml index ec4f33d..b2d3ac2 100644 --- a/coalition.yml +++ b/coalition.yml @@ -1,5 +1,6 @@ +--- name: CoalitionExample agent_type: coalition.CoalitionAgent network_params: generator: empty_graph - n: 100 \ No newline at end of file + n: 100