1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 23:42:29 +00:00

TerroristNetworkModel to FSM

This commit is contained in:
Tasio Mendez 2018-05-18 15:15:53 +02:00
parent 1083112473
commit a0613f8a58
3 changed files with 46 additions and 46 deletions

View File

@ -1,6 +1,6 @@
import random import random
import networkx as nx import networkx as nx
from soil.agents import BaseAgent from soil.agents import BaseAgent, FSM, state
from scipy.spatial import cKDTree as KDTree from scipy.spatial import cKDTree as KDTree
global betweenness_centrality_global global betweenness_centrality_global
@ -9,7 +9,7 @@ global degree_centrality_global
betweenness_centrality_global = None betweenness_centrality_global = None
degree_centrality_global = None degree_centrality_global = None
class TerroristSpreadModel(BaseAgent): class TerroristSpreadModel(FSM):
""" """
Settings: Settings:
information_spread_intensity information_spread_intensity
@ -38,12 +38,14 @@ class TerroristSpreadModel(BaseAgent):
self.terrorist_additional_influence = environment.environment_params['terrorist_additional_influence'] self.terrorist_additional_influence = environment.environment_params['terrorist_additional_influence']
self.prob_interaction = environment.environment_params['prob_interaction'] self.prob_interaction = environment.environment_params['prob_interaction']
if self.state['id'] == 0: # Civilian if self['id'] == self.civilian.id: # Civilian
self.initial_belief = random.uniform(0.00, 0.5) self.initial_belief = random.uniform(0.00, 0.5)
elif self.state['id'] == 1: # Terrorist elif self['id'] == self.terrorist.id: # Terrorist
self.initial_belief = random.uniform(0.8, 1.00) self.initial_belief = random.uniform(0.8, 1.00)
elif self.state['id'] == 2: # Leader elif self['id'] == self.leader.id: # Leader
self.initial_belief = 1.00 self.initial_belief = 1.00
else:
raise Exception('Invalid state id: {}'.format(self['id']))
if 'min_vulnerability' in environment.environment_params: if 'min_vulnerability' in environment.environment_params:
self.vulnerability = random.uniform( environment.environment_params['min_vulnerability'], environment.environment_params['max_vulnerability'] ) self.vulnerability = random.uniform( environment.environment_params['min_vulnerability'], environment.environment_params['max_vulnerability'] )
@ -72,15 +74,8 @@ class TerroristSpreadModel(BaseAgent):
_list = super().get_agents(state_id, limit_neighbors=True) _list = super().get_agents(state_id, limit_neighbors=True)
return [ neighbour for neighbour in _list if isinstance(neighbour, TerroristSpreadModel) ] return [ neighbour for neighbour in _list if isinstance(neighbour, TerroristSpreadModel) ]
def step(self): @state
if self.state['id'] == 0: # Civilian def civilian(self):
self.civilian_behaviour()
elif self.state['id'] == 1: # Terrorist
self.terrorist_behaviour()
elif self.state['id'] == 2: # Leader
self.leader_behaviour()
def civilian_behaviour(self):
if self.count_neighboring_agents() > 0: if self.count_neighboring_agents() > 0:
neighbours = [] neighbours = []
for neighbour in self.get_neighboring_agents(): for neighbour in self.get_neighboring_agents():
@ -93,37 +88,33 @@ class TerroristSpreadModel(BaseAgent):
self.mean_belief = mean_belief * self.vulnerability + self.initial_belief * ( 1 - self.vulnerability ) self.mean_belief = mean_belief * self.vulnerability + self.initial_belief * ( 1 - self.vulnerability )
if self.mean_belief >= 0.8: if self.mean_belief >= 0.8:
self.state['id'] = 1 return self.terrorist
# self.state['radicalism'] = self.mean_belief @state
def leader(self):
def leader_behaviour(self):
self.mean_belief = self.mean_belief ** ( 1 - self.terrorist_additional_influence ) self.mean_belief = self.mean_belief ** ( 1 - self.terrorist_additional_influence )
if self.count_neighboring_agents(state_id=[1,2]) > 0: if self.count_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]) > 0:
for neighbour in self.get_neighboring_agents(state_id=[1,2]): for neighbour in self.get_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]):
if neighbour.betweenness_centrality > self.betweenness_centrality: if neighbour.betweenness_centrality > self.betweenness_centrality:
self.state['id'] = 1 return self.terrorist
# self.state['radicalism'] = self.mean_belief @state
def terrorist(self):
def terrorist_behaviour(self): if self.count_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]) > 0:
if self.count_neighboring_agents(state_id=[1,2]) > 0: neighbours = self.get_neighboring_agents(state_id=[self.terrorist.id, self.leader.id])
neighbours = self.get_neighboring_agents(state_id=[1,2])
influence = sum( neighbour.degree_centrality for neighbour in neighbours ) influence = sum( neighbour.degree_centrality for neighbour in neighbours )
mean_belief = sum( neighbour.mean_belief * neighbour.degree_centrality / influence for neighbour in neighbours ) mean_belief = sum( neighbour.mean_belief * neighbour.degree_centrality / influence for neighbour in neighbours )
self.initial_belief = self.mean_belief self.initial_belief = self.mean_belief
self.mean_belief = mean_belief * self.vulnerability + self.initial_belief * ( 1 - self.vulnerability ) self.mean_belief = mean_belief * self.vulnerability + self.initial_belief * ( 1 - self.vulnerability )
self.mean_belief = self.mean_belief ** ( 1 - self.terrorist_additional_influence ) self.mean_belief = self.mean_belief ** ( 1 - self.terrorist_additional_influence )
if self.count_neighboring_agents(state_id=2) == 0 and self.count_neighboring_agents(state_id=1) > 0: if self.count_neighboring_agents(state_id=self.leader.id) == 0 and self.count_neighboring_agents(state_id=self.terrorist.id) > 0:
max_betweenness_centrality = self max_betweenness_centrality = self
for neighbour in self.get_neighboring_agents(state_id=1): for neighbour in self.get_neighboring_agents(state_id=self.terrorist.id):
if neighbour.betweenness_centrality > max_betweenness_centrality.betweenness_centrality: if neighbour.betweenness_centrality > max_betweenness_centrality.betweenness_centrality:
max_betweenness_centrality = neighbour max_betweenness_centrality = neighbour
if max_betweenness_centrality == self: if max_betweenness_centrality == self:
self.state['id'] = 2 return self.leader
# self.state['radicalism'] = self.mean_belief
def add_edge(self, G, source, target): def add_edge(self, G, source, target):
G.add_edge(source.id, target.id, start=self.env._now) G.add_edge(source.id, target.id, start=self.env._now)
@ -189,7 +180,7 @@ class HavenModel(BaseAgent):
civilian_haven = False civilian_haven = False
if self.state['id'] == 0: if self.state['id'] == 0:
for neighbour_agent in self.get_neighboring_agents(): for neighbour_agent in self.get_neighboring_agents():
if isinstance(neighbour_agent, TerroristSpreadModel) and neighbour_agent.state['id'] == 0: if isinstance(neighbour_agent, TerroristSpreadModel) and neighbour_agent['id'] == neighbor_agent.civilian.id:
civilian_haven = True civilian_haven = True
if civilian_haven: if civilian_haven:
@ -224,13 +215,18 @@ class TerroristNetworkModel(TerroristSpreadModel):
self.weight_social_distance = environment.environment_params['weight_social_distance'] self.weight_social_distance = environment.environment_params['weight_social_distance']
self.weight_link_distance = environment.environment_params['weight_link_distance'] self.weight_link_distance = environment.environment_params['weight_link_distance']
def step(self): @state
if self.state['id'] == 1 or self.state['id'] == 2: def terrorist(self):
self.update_relationships() self.update_relationships()
super().step() return super().terrorist()
@state
def leader(self):
self.update_relationships()
return super().leader()
def update_relationships(self): def update_relationships(self):
if self.count_neighboring_agents(state_id=0) == 0: if self.count_neighboring_agents(state_id=self.civilian.id) == 0:
close_ups = self.link_search(self.global_topology, self.id, self.vision_range) close_ups = self.link_search(self.global_topology, self.id, self.vision_range)
step_neighbours = self.social_search(self.global_topology, self.id, self.sphere_influence) step_neighbours = self.social_search(self.global_topology, self.id, self.sphere_influence)
search = list(set(close_ups).union(step_neighbours)) search = list(set(close_ups).union(step_neighbours))
@ -240,7 +236,7 @@ class TerroristNetworkModel(TerroristSpreadModel):
social_distance = 1 / self.shortest_path_length(self.global_topology, self.id, agent.id) social_distance = 1 / self.shortest_path_length(self.global_topology, self.id, agent.id)
spatial_proximity = ( 1 - self.get_distance(self.global_topology, self.id, agent.id) ) spatial_proximity = ( 1 - self.get_distance(self.global_topology, self.id, agent.id) )
prob_new_interaction = self.weight_social_distance * social_distance + self.weight_link_distance * spatial_proximity prob_new_interaction = self.weight_social_distance * social_distance + self.weight_link_distance * spatial_proximity
if agent.state['id'] == 0 and random.random() < prob_new_interaction: if agent['id'] == agent.civilian.id and random.random() < prob_new_interaction:
self.add_edge(self.global_topology, self, agent) self.add_edge(self.global_topology, self, agent)
break break

View File

@ -1,6 +1,6 @@
name: TerroristNetworkModel_sim name: TerroristNetworkModel_sim
load_module: TerroristNetworkModel load_module: TerroristNetworkModel
max_time: 200 max_time: 100
num_trials: 1 num_trials: 1
network_params: network_params:
generator: random_geometric_graph generator: random_geometric_graph
@ -12,19 +12,19 @@ network_agents:
- agent_type: TerroristNetworkModel - agent_type: TerroristNetworkModel
weight: 0.8 weight: 0.8
state: state:
id: 0 # Civilians id: civilian # Civilians
- agent_type: TerroristNetworkModel - agent_type: TerroristNetworkModel
weight: 0.1 weight: 0.1
state: state:
id: 2 # Leaders id: leader # Leaders
- agent_type: TrainingAreaModel - agent_type: TrainingAreaModel
weight: 0.05 weight: 0.05
state: state:
id: 2 # Terrorism id: terrorist # Terrorism
- agent_type: HavenModel - agent_type: HavenModel
weight: 0.05 weight: 0.05
state: state:
id: 0 # Civilian id: civilian # Civilian
environment_params: environment_params:
# TerroristSpreadModel # TerroristSpreadModel
@ -51,11 +51,11 @@ visualization_params:
HavenModel: home HavenModel: home
TerroristNetworkModel: person TerroristNetworkModel: person
colors: colors:
- attr_id: 0 - attr_id: civilian
color: '#40de40' color: '#40de40'
- attr_id: 1 - attr_id: terrorist
color: red color: red
- attr_id: 2 - attr_id: leader
color: '#c16a6a' color: '#c16a6a'
background_image: 'map_4800x2860.jpg' background_image: 'map_4800x2860.jpg'
background_opacity: '0.9' background_opacity: '0.9'

View File

@ -60,6 +60,10 @@
return false; return false;
} }
String.prototype.type = function() {
return "string";
}
var lastFocusNode; var lastFocusNode;
var _helpers = { var _helpers = {
set_node: function(node, property, time) { set_node: function(node, property, time) {