forked from zawagner22/cross-entropy-for-combinatorics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
68 lines (53 loc) · 1.76 KB
/
score.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
import numpy as np
from numba import njit
import math
@njit
def bfs(Gdeg,edgeListG):
#simple breadth first search algorithm, from each vertex
N = Gdeg.size
distMat1 = np.zeros((N,N))
conn = True
for s in range(N):
visited = np.zeros(N,dtype=np.int8)
# Create a queue for BFS. Queues are not suported with njit yet so do it manually
myQueue = np.zeros(N,dtype=np.int8)
dist = np.zeros(N,dtype=np.int8)
startInd = 0
endInd = 0
# Mark the source node as visited and enqueue it
myQueue[endInd] = s
endInd += 1
visited[s] = 1
while endInd > startInd:
pivot = myQueue[startInd]
startInd += 1
for i in range(Gdeg[pivot]):
if visited[edgeListG[pivot][i]] == 0:
myQueue[endInd] = edgeListG[pivot][i]
dist[edgeListG[pivot][i]] = dist[pivot] + 1
endInd += 1
visited[edgeListG[pivot][i]] = 1
if endInd < N:
conn = False #not connected
for i in range(N):
distMat1[s][i] = dist[i]
return distMat1, conn
@njit
def score_graph(adjMatG, edgeListG, Gdeg):
"""
Reward function for Conjecture 2.3, using numba
"""
N = Gdeg.size
INF = 100000
distMat, conn = bfs(Gdeg,edgeListG)
#G has to be connected
if not conn:
return -INF
diam = np.amax(distMat)
sumLengths = np.zeros(N,dtype=np.int8)
sumLengths = np.sum(distMat,axis=0)
evals = np.linalg.eigvalsh(distMat)
evals = -np.sort(-evals)
proximity = np.amin(sumLengths)/(N-1.0)
ans = -(proximity + evals[math.floor(2*diam/3) - 1])
return ans