Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. std::random_device rd;
  2. std::mt19937 gen(rd());
  3. std::uniform_real_distribution<> dis(0, N-1);
  4.  
  5. No. of rec = 99
  6. No. of rec = 101
  7. No. of rec = 101
  8. No. of rec = 103
  9. No. of rec = 102
  10.  
  11. No. of rec = 98
  12. No. of rec = 309
  13. No. of rec = 1433
  14. No. of rec = 3730
  15. No. of rec = 6590
  16.  
  17. import networkx as nx
  18. import numpy as np
  19. import matplotlib.pyplot as plt
  20. import random
  21.  
  22. N = 10000
  23. m = 2
  24. epsilon = 0.001
  25. eta = 1.
  26. gamma = -2.1
  27.  
  28.  
  29. def power_law(x0, x1, gamma):
  30. pl = []
  31. for i in range (N):
  32. pl.append(((x1**(gamma+1) - x0**(gamma+1))*random.uniform(0,1) + x0**(gamma+1.0))**(1.0/(gamma + 1.0)))
  33. return pl
  34.  
  35.  
  36. def graph_activity(act,m,N,G):
  37.  
  38. active_nodes=[]
  39.  
  40. c=0
  41. for i in act:
  42. if random.uniform(0,1)<i:
  43. active_nodes.append(c)
  44. G.add_node(c)
  45. c+=1
  46.  
  47. for i in active_nodes:
  48.  
  49. count=0
  50. while count<m:
  51. target=random.randint(0,N-1)
  52. if target!=i and target not in G.neighbors(i):
  53. G.add_edge(i,target)
  54. count+=1
  55.  
  56. return 0
  57.  
  58.  
  59.  
  60. def SIR(status,l,mu,G):
  61.  
  62. list_inf=set()
  63. list_rec=set()
  64.  
  65.  
  66. for i in range(status.size):
  67. if status[i]==1:
  68. for j in G.neighbors(i):
  69. if status[j]==0:
  70. if random.uniform(0,1)<l:
  71. list_inf.add(j)
  72.  
  73.  
  74. if random.uniform(0,1)<mu:
  75. list_rec.add(i)
  76.  
  77. for i in list_inf:
  78. status[i]=1
  79. for i in list_rec:
  80. status[i]=2
  81.  
  82.  
  83. def seeds(N,status,p):
  84. number=int(N*p)
  85.  
  86. list_nodes=[]
  87. for i in range(N):
  88. list_nodes.append(i)
  89.  
  90. random.shuffle(list_nodes)
  91.  
  92.  
  93. for i in range(number):
  94. status[list_nodes[i]]=1
  95.  
  96.  
  97.  
  98. list_l=list(np.arange(0,1,0.2))
  99.  
  100. recovered=[]
  101. for l in list_l:
  102. act=power_law(0.001,1,gamma)
  103. status=np.zeros(N,int)
  104. seeds(N,status,0.01)
  105.  
  106. for i in range(500):
  107. G=nx.Graph()
  108. graph_activity(act,m,N,G)
  109. SIR(status, l, 0.01, G)
  110. #print (2*G.number_of_edges()/G.number_of_nodes(), 2*m*average_a)
  111.  
  112.  
  113. count=0
  114. for j in range(N):
  115. if status[j]==2:
  116. count+=1
  117.  
  118. print ("No. of rec = ", count)
  119.  
  120. recovered.append(count)
  121.  
  122. #include <iostream>
  123. #include <igraph/igraph.h>
  124. #include <vector>
  125. #include <random>
  126. #include <ctime>
  127. using namespace std;
  128.  
  129. int N = 10000;
  130. int m = 2;
  131. double epsilon = 0.001;
  132. double eta = 1.0;
  133. double g = -2.1;
  134.  
  135. double random_number(){
  136. std::random_device rd;
  137. std::mt19937 gen(rd());
  138. std::uniform_real_distribution<> dis(0, 1);//uniform distribution between 0 and 1
  139. return dis(gen); // random number between 0 and 1
  140. }
  141.  
  142. vector<double> power_law(double x0, double x1, double _gamma){
  143. vector<double> pl;
  144. for (int i = 0; i < N; i++){
  145. double r = random_number();
  146. pl.push_back( pow((((pow(x1, g+1)) - (pow(x0,g+1)))*r + (pow(x0, g+1))),(1/(g+1))));
  147. }
  148. return pl;
  149. }
  150.  
  151. vector<int> neighbours(igraph_vector_t *v) {
  152. vector<int> neigh;
  153. long int i;
  154. for (i=0; i<igraph_vector_size(v); i++) {
  155. neigh.push_back(VECTOR(*v)[i]);
  156. }
  157. return neigh;
  158. }
  159.  
  160. igraph_t activate_graph(vector<double> activity, igraph_t g){
  161. vector<int> active_nodes;
  162. igraph_vector_t v;
  163. igraph_vector_init(&v, 8);
  164.  
  165. for (int i = 0; i < activity.size(); i++){
  166. if (random_number() < activity[i]){
  167. active_nodes.push_back(i);
  168. }
  169. }
  170.  
  171. for (int i = 0; i < active_nodes.size(); i++){
  172. int count = 0;
  173. while (count < m){
  174. int target = -1;
  175. while (target < 0) {
  176. std::random_device rd;
  177. std::mt19937 gen(rd());
  178. std::uniform_real_distribution<> dis(0, N-1);//uniform distribution between 0 and 1
  179. target = dis(gen);
  180. }
  181. igraph_neighbors(&g, &v, active_nodes[i], IGRAPH_ALL);
  182. vector<int> n = neighbours(&v);
  183. if (target != active_nodes[i] && !(std::find(n.begin(), n.end(), active_nodes[i]) != n.end())) {
  184. igraph_add_edge(&g, active_nodes[i], target);
  185. count++;
  186. }
  187. }
  188. }
  189. return g;
  190. }
  191.  
  192. vector<int> seeds(int N, double p, vector<int> status){
  193. int number = (int)(N*p);
  194. vector<int> list_nodes;
  195. for (int i = 1; i < N-1; i++){
  196. list_nodes.push_back(i);
  197. }
  198. std::random_device rd; // Warning: implementation of this is allowed to be stupid and
  199. // return the same value every time. Does not work in mingw 4.8
  200. // Can't speak for 4.9 or greater
  201. std::mt19937 randomizer(rd());
  202.  
  203. shuffle ( list_nodes.begin(), list_nodes.end(), randomizer); // shuffle the vector
  204. list_nodes.resize(number);
  205.  
  206. for (int i = 0; i < list_nodes.size(); i++){
  207. status[list_nodes[i]] = 1;
  208. }
  209.  
  210. return status;
  211. }
  212.  
  213. vector<int> sir(vector<int> status_list, igraph_t g, double Lambda){
  214. set<int> list_inf;
  215. set<int> list_rec;
  216.  
  217. igraph_vector_t v;
  218. igraph_vector_init(&v, 8);
  219.  
  220. double mu = 0.01;
  221. for (int i = 0; i < status_list.size(); i++) {
  222. if (status_list[i] == 1) {
  223. igraph_neighbors(&g, &v, i, IGRAPH_ALL);
  224. vector<int> neigh = neighbours(&v);
  225. for (int j = 0; j < neigh.size(); j++) {
  226. if (status_list[neigh[j]] == 0) {
  227. if (random_number() < Lambda) {
  228. list_inf.insert(j);
  229. }
  230. }
  231. }
  232. if (random_number() < mu) {
  233. list_rec.insert(i);
  234. }
  235. }
  236.  
  237. }
  238. srand((NULL));
  239. vector<int> vec_inf;
  240. vector<int> vec_rec;
  241.  
  242. std::copy(list_inf.begin(), list_inf.end(), std::back_inserter(vec_inf));
  243. std::copy(list_rec.begin(), list_rec.end(), std::back_inserter(vec_rec));
  244.  
  245. for (int i = 0; i < vec_inf.size(); i++) {
  246. status_list[vec_inf[i]] = 1;
  247. }
  248. for (int i = 0; i < vec_rec.size(); i++) {
  249. status_list[vec_rec[i]] = 2;
  250. }
  251. return status_list;
  252. }
  253.  
  254. int main() {
  255. static vector<int> status_list;
  256. double Lambda = 0;
  257. vector<double> recovered;
  258. igraph_t graph;
  259. while (Lambda < 1.0) {
  260.  
  261. vector<double> act = power_law(epsilon, 1, g);
  262. status_list = vector<int>(N, 0);
  263. status_list = seeds(N, 0.01, status_list);
  264.  
  265. for (int i = 0; i < 500; i++) {
  266. igraph_empty(&graph, N, false);
  267. graph = activate_graph(act, graph);
  268. status_list = sir(status_list, graph, Lambda);
  269. }
  270. int count = 0;
  271. for (int j = 0; j < N; j++) {
  272. if (status_list[j] == 2) {
  273. count++;
  274. }
  275. }
  276.  
  277. recovered.push_back(count);
  278. cout << "No. of rec = " << count << endl;
  279. igraph_destroy(&graph);
  280. Lambda += 0.2;
  281. }
  282. return 0;
  283. }
Add Comment
Please, Sign In to add comment