mirror of
https://github.com/lukefleed/ShfitedPowGMRES.git
synced 2026-10-18 19:22:06 +00:00
Now algo 1 works, I hope
This commit is contained in:
@@ -1 +1,9 @@
|
||||
# ShfitedPowGMRES
|
||||
# ShfitedPowGMRES
|
||||
|
||||
```bash
|
||||
mkdir data && cd data
|
||||
wget https://snap.stanford.edu/data/web-BerkStan.txt.gz
|
||||
wget https://snap.stanford.edu/data/web-Stanford.txt.gz
|
||||
gunzip web-Stanford.txt.gz
|
||||
gunzip web-BerkStan.txt.gz
|
||||
```
|
||||
|
||||
+65
-54
@@ -2,14 +2,17 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import networkx as nx\n",
|
||||
"import time\n",
|
||||
"import math\n",
|
||||
"import scipy as sp\n",
|
||||
"import scipy.sparse"
|
||||
"from scipy.sparse import *\n",
|
||||
"from scipy.sparse.linalg import norm"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -21,7 +24,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -39,7 +42,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -71,12 +74,12 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n = G1.number_of_nodes()\n",
|
||||
"P = create_matrix(G1) "
|
||||
"n = G2.number_of_nodes()\n",
|
||||
"P = create_matrix(G2) "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -88,11 +91,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# define d as a nx1 sparse matrix, where n is the number of nodes in the graph. The vector is filled with d(i) = 1 if the i row of the matrix P is filled with zeros, other wise is 0\n",
|
||||
"\n",
|
||||
"# d is the vector of dangling nodes\n",
|
||||
"d = sp.sparse.lil_matrix((n,1))\n",
|
||||
"for i in range(n):\n",
|
||||
" if P[i].sum() == 0:\n",
|
||||
@@ -108,7 +113,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -129,7 +134,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -140,7 +145,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -152,7 +157,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -175,70 +180,76 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a = [0.85, 0.9, 0.95, 0.99]\n",
|
||||
"tau = 10**-8\n",
|
||||
"max_mv = 1000\n",
|
||||
"\n",
|
||||
"# this should return mv (the number of iteration needed for the convergence), and two vector of lenght len(a) called x and r. Where x is the vector of the pagerank and r is the residual vector\n",
|
||||
"# list of alpha values, from 0.85 to 0.99 with step 0.01\n",
|
||||
"a = []\n",
|
||||
"for i in range(85,100):\n",
|
||||
" a.append(i/100)\n",
|
||||
"\n",
|
||||
"tau = 10**-6\n",
|
||||
"max_mv = 100\n",
|
||||
"\n",
|
||||
"# this should return mv (the number of iteration needed for the convergence), and two vector called x and r. Where x is the vector of the pagerank and r is the residual vector\n",
|
||||
"\n",
|
||||
"def Algorithm1(Pt, v, tau, max_mv, a: list):\n",
|
||||
" u = Pt.dot(v) - v\n",
|
||||
" mv = 1\n",
|
||||
" # take time of the performance\n",
|
||||
" start_time = time.time()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" u = Pt.dot(v) - v \n",
|
||||
" mv = 1 # number of iteration\n",
|
||||
" r = sp.sparse.lil_matrix((n,1)) \n",
|
||||
" Res = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" x = sp.sparse.lil_matrix((n,1)) \n",
|
||||
"\n",
|
||||
" for i in range(len(a)):\n",
|
||||
" r = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" r[i] = a[i]*u\n",
|
||||
" Res = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" Res[i] = np.linalg.norm(r[i])\n",
|
||||
" r = a[i]*(u) \n",
|
||||
" normed_r = norm(r)\n",
|
||||
" Res[i] = normed_r \n",
|
||||
"\n",
|
||||
" if Res[i] > tau:\n",
|
||||
" x = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" x[i] = r[i] + v\n",
|
||||
" \n",
|
||||
" x = r + v \n",
|
||||
"\n",
|
||||
" print(\"STARTING THE WHILE LOOP\\n\")\n",
|
||||
"\n",
|
||||
" # take the maximum value of the sparse matrix Res\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" while max(Res) > tau and mv < max_mv:\n",
|
||||
" u = Pt.dot(u)\n",
|
||||
" mv += 1\n",
|
||||
" u = Pt*u # should it be the same u of the beginning?\n",
|
||||
" mv += 1 \n",
|
||||
" print(\"mv = \", mv)\n",
|
||||
" print(\"max(Res) = \", max(Res))\n",
|
||||
"\n",
|
||||
" for i in range(len(a)):\n",
|
||||
" if Res[i] >= tau:\n",
|
||||
" r = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" r[i] = a[i]*u\n",
|
||||
" Res = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" Res[i] = np.linalg.norm(r[i])\n",
|
||||
" if Res[i] >= tau: \n",
|
||||
" r = (a[i]**(mv+1))*(u)\n",
|
||||
" Res[i] = norm(r)\n",
|
||||
"\n",
|
||||
" if Res[i] > tau:\n",
|
||||
" x = sp.sparse.lil_matrix((len(a),1))\n",
|
||||
" x[i] = r[i] + x[i]\n",
|
||||
" x = r + x\n",
|
||||
"\n",
|
||||
" print(\"\\nEND OF THE WHILE LOOP\\n\")\n",
|
||||
"\n",
|
||||
" if mv == max_mv:\n",
|
||||
" print(\"The algorithm didn't converge in \", max_mv, \" iterations\")\n",
|
||||
" else:\n",
|
||||
" print(\"The algorithm converged in \", mv, \" iterations\")\n",
|
||||
"\n",
|
||||
" print(\"\\nThe execution time is %s seconds\" % (time.time() - start_time))\n",
|
||||
" \n",
|
||||
" return mv, x, r "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "ValueError",
|
||||
"evalue": "shape mismatch in assignment",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m/tmp/ipykernel_128897/741213869.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# launch the algorithm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAlgorithm1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtau\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_mv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/tmp/ipykernel_128897/1942976890.py\u001b[0m in \u001b[0;36mAlgorithm1\u001b[0;34m(Pt, v, tau, max_mv, a)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlil_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mu\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mRes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msparse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlil_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mRes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3/dist-packages/scipy/sparse/_lil.py\u001b[0m in \u001b[0;36m__setitem__\u001b[0;34m(self, key, x)\u001b[0m\n\u001b[1;32m 330\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_set_intXint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 331\u001b[0m \u001b[0;31m# Everything else takes the normal path.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 332\u001b[0;31m \u001b[0mIndexMixin\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__setitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 333\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 334\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_mul_scalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3/dist-packages/scipy/sparse/_index.py\u001b[0m in \u001b[0;36m__setitem__\u001b[0;34m(self, key, x)\u001b[0m\n\u001b[1;32m 130\u001b[0m if not ((broadcast_row or x.shape[0] == i.shape[0]) and\n\u001b[1;32m 131\u001b[0m (broadcast_col or x.shape[1] == i.shape[1])):\n\u001b[0;32m--> 132\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'shape mismatch in assignment'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 133\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 134\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mValueError\u001b[0m: shape mismatch in assignment"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# launch the algorithm\n",
|
||||
"mv, x, r = Algorithm1(Pt, v, tau, max_mv, a)"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user