scientific-programming-exer.../ex_34.py
2019-01-09 14:19:33 +01:00

43 lines
1.2 KiB
Python

import numpy as np
import random
class RankPageCalculator(object):
def __init__(self, connections):
self._connections = connections
self._dim = len(connections)
self._hits = np.arange(0, self._dim, 1)
self._current = random.randint(0, self._dim - 1)
def run(self, n, alpha):
for i in range(n):
if(np.random.uniform(0, 1) < alpha):
self._current = random.randint(0, self._dim - 1)
self._hits[self._current] += 1
continue
uniform = np.random.uniform(0, 1, self._dim)
uniform[1 != self._connections[self._current]] = 0
self._current = uniform.argmax()
self._hits[self._current] += 1
return self._hits
adj = np.array([[0, 1, 1, 0, 0, 0, 0, 0, 0]
, [0, 0, 1, 0, 1, 0, 0, 0, 0]
, [1, 1, 0, 1, 0, 0, 0, 0, 0]
, [0, 0, 1, 0, 1, 0, 0, 0, 0]
, [0, 1, 0, 1, 0, 0, 0, 0, 0]
, [0, 0, 0, 0, 1, 0, 1, 0, 1]
, [0, 0, 0, 0, 0, 1, 0, 0, 0]
, [0, 0, 0, 0, 0, 0, 1, 0, 0]
, [0, 0, 0, 0, 0, 1, 1, 1, 0]], dtype=np.int16)
print(RankPageCalculator(adj).run(100000, 0.05))
print(RankPageCalculator(adj).run(100000, 0.3))
adj[4, 5] = 1
adj[5, 4] = 1
print(RankPageCalculator(adj).run(100000, 0.05))
print(RankPageCalculator(adj).run(100000, 0.3))