-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
34 lines (30 loc) · 1.21 KB
/
Copy pathsearch.py
File metadata and controls
34 lines (30 loc) · 1.21 KB
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
from score_functions import *
from BN import *
import numpy as np
def acceptance_probability(old_score, new_score, temperature):
if new_score > old_score:
return 1.0
else:
return np.exp((new_score - old_score) / temperature)
#Implementing the simulated annealing algorithm
def bn_simulated_annealing(data,init_network,temperature = 1000,cooling_rate = 0.95,max_iterations = 1000):
variable_values = get_variable_values(data)
current_network = init_network.copy()
current_score = compute_score(current_network,data,variable_values)
print(current_score)
visited_graphs = set()
for i in range(max_iterations):
if (i % 10 == 0):
#print("iteration",i)
pass
new_network = get_new_network(current_network)
new_score = compute_score(new_network,data,variable_values)
prob = acceptance_probability(current_score, new_score, temperature)
rand = np.random.rand()
if rand < prob:
current_network = new_network
current_score = new_score
#print(current_score)
temperature *= cooling_rate
print(current_score)
return current_network