-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarloSAM.py
121 lines (94 loc) · 4.05 KB
/
MonteCarloSAM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 16:48:54 2019
@author: camil
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.cm as cm
import matplotlib.colors as col
from scipy.optimize import curve_fit
import pandas as pd
from math import factorial
import igraph
from igraph import *
import random
import time
def MonteCarloSAM(graph, nsteps, A, B):
## RUN MONTE CARLO SIMULATION: -----------------------------------------------
start = time.time()
# Monte Carlo Simulation:
kT = 2.48 # Gives units of kJ/mol
# Create simulated graph
edge_list = graph.get_edgelist()
sim_graph_0 = Graph()
full_EL = graph.get_edgelist() # Full list of possible edges to make or break
# Initialize monitoring variables:
prob_mon = np.zeros(nsteps)
ener_0_mon = np.zeros(nsteps)
ener_diff_mon = np.zeros(nsteps)
move_mon = np.zeros(nsteps)
break_mon = np.zeros(nsteps)
n_edges_mon = np.zeros(nsteps)
clique_mon = np.zeros((3,nsteps))
p_null = np.zeros(nsteps)
p_dimer = np.zeros(nsteps)
p_double_dimer = np.zeros(nsteps)
p_trimer1 = np.zeros(nsteps)
p_trimer2 = np.zeros(nsteps)
p_tetramer = np.zeros(nsteps)
for i in range(0,nsteps):
if np.mod(i,1000)==0:
print('Monte Carlo Step', i)
# Create simulated edge list:
sim_EL_0 = sim_graph_0.get_edgelist()
# Select random edge:
edge_sel = random.randint(0,np.shape(full_EL)[0]-1)
# Set initial energy:
ener_0 = EnerState(sim_graph_0,A,B)
# Calculate graph after move:
# Does selected edge exist in simulated graph?:
if not any([full_EL[edge_sel]==sim_EL_0[j] for j in range(0,np.shape(sim_EL_0)[0])]):
# If not, add this edge:
sim_EL_1 = list(sim_EL_0)
sim_EL_1.append(full_EL[edge_sel])
elif any([full_EL[edge_sel]==sim_EL_0[j] for j in range(0,np.shape(sim_EL_0)[0])]):
# If so, remove this edge:
sim_EL_1 = list(sim_EL_0)
sim_EL_1.remove(full_EL[edge_sel])
sim_graph_1 = Graph(sim_EL_1)
ener_1 = EnerState(sim_graph_1,A,B)
# Calculate the probability of making the move:
prob=np.exp(-(ener_1-ener_0)/kT)/(1+np.exp(-(ener_1-ener_0)/kT))
criterion = random.uniform(0, 1)
if criterion < prob:
sim_graph_0 = Graph(sim_EL_1)
# Monitoring variables:
prob_mon[i] = prob
ener_0_mon[i] = ener_0
ener_diff_mon[i] = ener_1-ener_0
move_mon[i] = criterion < prob
break_mon[i] = any([full_EL[edge_sel]==sim_EL_0[j] for j in range(0,np.shape(sim_EL_0)[0])])
n_edges_mon[i] = np.shape(sim_EL_0)[0]
clique_mon[0,i] = np.shape(sim_graph_0.maximal_cliques(min=1,max=1))[0]
clique_mon[1,i] = np.shape(sim_graph_0.maximal_cliques(min=2,max=2))[0]
clique_mon[2,i] = np.shape(sim_graph_0.maximal_cliques(min=3,max=3))[0]
np.shape(edge_list)[0]
# If graph is small (non-continuous):
if np.shape(edge_list)[0]<10:
p_null[i] = int(n_edges_mon[i]==0)
p_dimer[i] = int(n_edges_mon[i]==1)
p_double_dimer[i] = int(n_edges_mon[i]==2)
p_trimer1[i] = 0
p_trimer2[i] = 0
if np.any([j==(0,3) for j in sim_EL_1]):
if np.any([j==(0,2) for j in sim_EL_1])&np.any([j==(2,3) for j in sim_EL_1]):
p_trimer1[i] = 1
if np.any([j==(0,1) for j in sim_EL_1])&np.any([j==(1,3) for j in sim_EL_1]):
p_trimer2[i] = 1
p_tetramer[i] = int(n_edges_mon[i]==5)
end = time.time()
print 'Time Elapsed:', end-start
return clique_mon,n_edges_mon,prob_mon