コード例 #1
0
ファイル: crawl_utils.py プロジェクト: RobZelluf/AMDM-project
def reassign_partitions(graph, partitioning, k, min_score):
    repartitioning = partitioning.copy()
    for loops in range(3):
        reassigned = 0
        for v1 in range(graph.num_vertices):
            votes = [0] * k
            for v2 in graph.edge_dict[v1]:
                votes[partitioning[v2]] += 1
            if votes.index(max(votes)) != repartitioning[v1]:
                reassigned += 1
            repartitioning[v1] = votes.index(max(votes))
        score = score_partitioning(graph, repartitioning)
        if score < min_score:
            min_score = score
            partitioning = repartitioning.copy()
        print("Reassigned: ", reassigned)
        print(score)
    return partitioning
コード例 #2
0
for v in range(graph.num_vertices):
    print(v)
    print(min_score)
    partition = list()
    partitioning = np.zeros(graph.num_vertices, dtype=int)
    partitioning[v] = 1
    partition.append(v)
    # search for a one cut
    for i in range(2):
        additions = list()
        for j in range(len(partition)):
            additions += graph.edge_dict[partition[j]]
        partition += list(additions)
        for p in partition:
            partitioning[p] = 1
        score = score_partitioning(graph, partitioning)
        if score < min_score:
            min_score = score
            min_where = v
            final_partitioning = partitioning
            print(score_partitioning(graph, final_partitioning))
        score

print(min_score)
print(min_where)
final_score = score_partitioning(graph, final_partitioning)
print(final_score)

print(np.where(final_partitioning==1))

# write_file('web_', graph, final_partitioning, 2)
コード例 #3
0
output_num = int(input("Output number:"))
output_name = DIRs[output_num]
filename_output = "outputs/" + output_name

# Input prompt for filename
DIRs = [x for x in os.listdir("data/")]
i = 0
for DIR in DIRs:
    print(i, DIR)
    i += 1

data_num = int(input("Output number:"))
data_name = DIRs[data_num]

graph, _ = read_file(data_name)

partitioning = []
with open(filename_output, "r") as f:
    for line in f.readlines():
        if line[0] == "#" or line[0] == " ":
            continue

        line = line.split(" ")
        partitioning.append(int(line[1]))

print("Score:", score_partitioning(graph, partitioning))



コード例 #4
0
    return partition


start = datetime.datetime.now()
#
# init_vertex = 0
# p = partitions[init_vertex]
# while p != 0:
#     init_vertex += 1
#     p = partitions[init_vertex]

for k in range(num_partitions):
    init_vertex = 0
    while partitioning[init_vertex] != -1:
        init_vertex += 1

    partition = create_partition(graph, partitioning, init_vertex, 100, 10000)
    print(partition)
    print(len(set(partition)))
    for v in partition:
        partitioning[v] = k

for v in range(graph.num_vertices):
    if partitioning[v] == -1:
        partitioning[v] = random.randint(0, num_partitions - 1)

print(score_partitioning(graph, partitioning))

end = datetime.datetime.now()
print("Crawled in ", (end - start).total_seconds(), "seconds")
コード例 #5
0
ファイル: PageRank.py プロジェクト: RobZelluf/AMDM-project
from utils import read_file, write_file, Graph, score_partitioning, random_partition
import numpy as np
import random
import datetime
import pickle as p

graph = read_file()

k = 5
partitioning = random_partition(graph, k)
curr_score = score_partitioning(graph, partitioning)

for i in range(k):
    print("Num", i, " - ", len([x for x in partitioning if x == i]))

vertex = graph.get_random_vertex()
curr_cluster = int(partitioning[vertex])

alpha = 0.1
jumps = 1000
max_cluster_size = (graph.num_vertices - (graph.num_vertices /
                                          (k * 2))) / (k - 1)

iterations = 999999

for it in range(iterations):
    counts = np.zeros((k, graph.num_vertices))
    jump = 0
    while jump < jumps:
        if random.random() > alpha:
            vertex = graph.get_connected_vertex(vertex)
コード例 #6
0
ファイル: new_crawler.py プロジェクト: RobZelluf/AMDM-project
from utils import read_file, write_file, Graph, score_partitioning, random_partition

import numpy as np
import random
import datetime

graph = read_file('ca-GrQc.txt')
num_partitions = 2

print("Random Partition Score: ",
      score_partitioning(graph, random_partition(graph, num_partitions)))


def one_crawl(graph, v, steps):
    vertices_reached = list()
    for i in range(steps):
        v = graph.get_connected_vertex(v)
        vertices_reached.append(v)
    return vertices_reached


def init_vertices(graph, init_vert=0):
    visit_count = np.zeros(graph.num_vertices)
    init_verts = list()
    init_verts.append(init_vert)
    for k in range(num_partitions - 1):
        print(k + 1, "vertices initialized out of ", num_partitions)
        for i in range(10000):
            verts = one_crawl(graph, init_vert, 40)
            for v in verts:
                visit_count[v] += 1