1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32: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 networkx as nx
from soil.agents import BaseAgent
from soil.agents import BaseAgent, FSM, state
from scipy.spatial import cKDTree as KDTree
global betweenness_centrality_global
@ -9,7 +9,7 @@ global degree_centrality_global
betweenness_centrality_global = None
degree_centrality_global = None
class TerroristSpreadModel(BaseAgent):
class TerroristSpreadModel(FSM):
"""
Settings:
information_spread_intensity
@ -38,12 +38,14 @@ class TerroristSpreadModel(BaseAgent):
self.terrorist_additional_influence = environment.environment_params['terrorist_additional_influence']
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)
elif self.state['id'] == 1: # Terrorist
elif self['id'] == self.terrorist.id: # Terrorist
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
else:
raise Exception('Invalid state id: {}'.format(self['id']))
if 'min_vulnerability' in environment.environment_params:
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)
return [ neighbour for neighbour in _list if isinstance(neighbour, TerroristSpreadModel) ]
def step(self):
if self.state['id'] == 0: # Civilian
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):
@state
def civilian(self):
if self.count_neighboring_agents() > 0:
neighbours = []
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 )
if self.mean_belief >= 0.8:
self.state['id'] = 1
return self.terrorist
# self.state['radicalism'] = self.mean_belief
def leader_behaviour(self):
@state
def leader(self):
self.mean_belief = self.mean_belief ** ( 1 - self.terrorist_additional_influence )
if self.count_neighboring_agents(state_id=[1,2]) > 0:
for neighbour in self.get_neighboring_agents(state_id=[1,2]):
if self.count_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]) > 0:
for neighbour in self.get_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]):
if neighbour.betweenness_centrality > self.betweenness_centrality:
self.state['id'] = 1
return self.terrorist
# self.state['radicalism'] = self.mean_belief
def terrorist_behaviour(self):
if self.count_neighboring_agents(state_id=[1,2]) > 0:
neighbours = self.get_neighboring_agents(state_id=[1,2])
@state
def terrorist(self):
if self.count_neighboring_agents(state_id=[self.terrorist.id, self.leader.id]) > 0:
neighbours = self.get_neighboring_agents(state_id=[self.terrorist.id, self.leader.id])
influence = sum( neighbour.degree_centrality 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.mean_belief = mean_belief * self.vulnerability + self.initial_belief * ( 1 - self.vulnerability )
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
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:
max_betweenness_centrality = neighbour
if max_betweenness_centrality == self:
self.state['id'] = 2
# self.state['radicalism'] = self.mean_belief
return self.leader
def add_edge(self, G, source, target):
G.add_edge(source.id, target.id, start=self.env._now)
@ -189,7 +180,7 @@ class HavenModel(BaseAgent):
civilian_haven = False
if self.state['id'] == 0:
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
if civilian_haven:
@ -224,13 +215,18 @@ class TerroristNetworkModel(TerroristSpreadModel):
self.weight_social_distance = environment.environment_params['weight_social_distance']
self.weight_link_distance = environment.environment_params['weight_link_distance']
def step(self):
if self.state['id'] == 1 or self.state['id'] == 2:
self.update_relationships()
super().step()
@state
def terrorist(self):
self.update_relationships()
return super().terrorist()
@state
def leader(self):
self.update_relationships()
return super().leader()
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)
step_neighbours = self.social_search(self.global_topology, self.id, self.sphere_influence)
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)
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
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)
break

View File

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

View File

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