Skip to content

Commit

Permalink
new experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
kfoynt committed Sep 28, 2019
1 parent 10c14f5 commit 5eddc7d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
22 changes: 22 additions & 0 deletions localgraphclustering/approximate_PageRank.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from .cpp import *
import warnings

import time

def approximate_PageRank(G,
ref_nodes,
timeout: float = 100,
Expand Down Expand Up @@ -122,12 +124,19 @@ def approximate_PageRank(G,
if ys != None:
warnings.warn("\"acl\" doesn't support initial solutions, please use \"l1reg\" instead.")
if cpp:

# start = time.time()

n = G.adjacency_matrix.shape[0]
(length,xids,values) = aclpagerank_cpp(n,G.ai,G.aj,alpha,rho,ref_nodes,iterations)
# TODO, implement this in the C++ function
if normalize:
for i in range(len(xids)): # we can't use degrees because it could be weighted
values[i] /= (G.ai[xids[i]+1]-G.ai[xids[i]])

# end = time.time()
# print(" Elapsed time acl with rounding: ", end - start)

return (xids,values)
else:
return acl_list(ref_nodes, G, alpha = alpha, rho = rho, max_iter = iterations, max_time = timeout)
Expand All @@ -147,6 +156,9 @@ def approximate_PageRank(G,
#print("Uses the Fast Iterative Soft Thresholding Algorithm (FISTA).")
# TODO fix the following warning
# warnings.warn("The normalization of this routine hasn't been adjusted to the new system yet")

# start = time.time()

algo = proxl1PRaccel_cpp if method == "l1reg" else proxl1PRrand_cpp
if cpp:
if ys == None:
Expand All @@ -159,11 +171,21 @@ def approximate_PageRank(G,
p = fista_dinput_dense(ref_nodes, G, alpha = alpha, rho = rho, epsilon = epsilon, max_iter = iterations, max_time = timeout)
# convert result to a sparse vector
# nonzeros = np.count_nonzero(p)


# end = time.time()
# print(" Elapsed time l1reg with rounding: ", end - start)

start = time.time()

idx = np.nonzero(p)[0]
vals = p[idx]

if normalize:
vals = np.multiply(G.dn[idx], vals)

# end = time.time()
# print(" Elapsed time conversion l1-reg. with rounding: ", end - start)

# it = 0
# for i in range(len(p)):
Expand Down
13 changes: 12 additions & 1 deletion localgraphclustering/cpp/proxl1PRrand_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import ctypes
from .utility import determine_types, standard_types
from . import _graphlib

import time

# Load the functions
def _setup_proxl1PRrand_args(vtypestr, itypestr, fun):
Expand Down Expand Up @@ -68,6 +68,9 @@ def _get_proxl1PRrand_cpp_types_fun(ai,aj):

def proxl1PRrand_cpp(ai,aj,a,ref_node,d,ds,dsinv,y=None,alpha = 0.15,rho = 1.0e-5,epsilon = 1.0e-4,maxiter = 10000,max_time = 100,normalized_objective=True):
float_type,vtype,itype,ctypes_vtype,ctypes_itype,fun = _get_proxl1PRrand_cpp_types_fun(ai,aj)

# start = time.time()

n = len(ai) - 1
if type(ref_node) is not list:
ref_node = np.array([ref_node],dtype = ctypes_vtype)
Expand All @@ -80,11 +83,19 @@ def proxl1PRrand_cpp(ai,aj,a,ref_node,d,ds,dsinv,y=None,alpha = 0.15,rho = 1.0e-
new_y = np.zeros(n,dtype=float_type)
else:
new_y = np.array(y,dtype=float_type)

# end = time.time()
# print(" Elapsed time inside initialization l1-reg. with rounding: ", end - start)

# start2 = time.time()

not_converged=fun(n,ai,aj,a,alpha,rho,ref_node,len(ref_node),d,ds,dsinv,epsilon,grad,p,new_y,maxiter,0,max_time, normalized_objective)

if y != None:
for i in range(n):
y[i] = new_y[i]

# end2 = time.time()
# print(" Elapsed time inside l1-reg. with rounding: ", end2 - start2)

return (not_converged,grad,p)
53 changes: 41 additions & 12 deletions localgraphclustering/src/lib/proxl1PRrand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,19 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
uniform_int_distribution<long long int> dist(std::llround(std::pow(2,0)), std::llround(std::pow(2,63)));
// return dist(e2);

// timeStamp1 = clock();

vtype not_converged = 0;
vtype* candidates = new vtype[num_nodes];
bool* visited = new bool[num_nodes];
for (vtype i = 0; i < num_nodes; ++i) visited[i] = false;

// timeStamp2 = clock();

// cout << "time initialization visited: " << (float)(timeStamp2 - timeStamp1)/ CLOCKS_PER_SEC << endl;

// timeStamp1 = clock();

// initialize seed nodes as candidates
double maxNorm = 0;
vtype candidates_size = num_seeds;
Expand All @@ -260,6 +268,10 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
// cout << "seed[" << i << "]: " << seed[i] << endl;
}

// timeStamp2 = clock();

// cout << "time initialization seed nodes and candidates: " << (float)(timeStamp2 - timeStamp1)/ CLOCKS_PER_SEC << endl;

// for (vtype i = 0; i < num_nodes; ++i) {
// cout << "grad[" << i << "]: " << grad[i] << endl;
// }
Expand All @@ -282,23 +294,29 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
// }
// }

for(vtype i = 0; i < num_seeds; i ++){
grad[seed[i]] = -alpha*dsinv[seed[i]]/num_seeds;
}

// timeStamp1 = clock();

//Find nonzero indices in y and dsinv
unordered_map<vtype,vtype> indices;
unordered_set<vtype> nz_ids;
for (vtype i = 0; i < num_nodes; i ++) {
if (y[i] != 0 && dsinv[i] != 0) {
indices[i] = 0;
q[i] = y[i];
}
if (y[i] != 0 || grad[i] != 0) {
nz_ids.insert(i);
}

vtype temp_int;

for(vtype i = 0; i < num_seeds; i ++){
temp_int = seed[i];
grad[temp_int] = -alpha*dsinv[temp_int]/num_seeds;
nz_ids.insert(temp_int);
indices[temp_int] = 0;
q[temp_int] = y[temp_int];
}

// for (vtype i = 0; i < num_nodes; i ++) indices[i] = 0;

// timeStamp2 = clock();

// cout << "time initialization indices: " << (float)(timeStamp2 - timeStamp1)/ CLOCKS_PER_SEC << endl;

// timeStamp1 = clock();

for(auto it = nz_ids.begin() ; it != nz_ids.end(); ++it){
vtype i = *it;
Expand All @@ -319,6 +337,10 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
}
}

// timeStamp2 = clock();

// cout << "time initialization gradient: " << (float)(timeStamp2 - timeStamp1)/ CLOCKS_PER_SEC << endl;

// for (vtype i = 0; i < num_nodes; ++i) {
// cout << "1st grad[" << i << "]: " << grad[i] << endl;
// }
Expand All @@ -331,6 +353,9 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
vtype numiter = 1;
// some constant
// maxiter *= 100;

// timeStamp1 = clock();

while (maxNorm > threshold) {

// for (vtype i = 0; i < num_nodes; ++i) {
Expand Down Expand Up @@ -407,6 +432,10 @@ vtype graph<vtype,itype>::proxl1PRrand(vtype num_nodes, vtype* seed, vtype num_s
// cout << "last grad[" << i << "]: " << grad[i] << endl;
// }

// timeStamp2 = clock();

// cout << "time loop: " << (float)(timeStamp2 - timeStamp1)/ CLOCKS_PER_SEC << endl;

delete [] candidates;
delete [] visited;
return not_converged;
Expand Down

0 comments on commit 5eddc7d

Please sign in to comment.