VPython for Graph Visualization
Bruce Sherwood <[email protected]> Tue, 10 Jul 2012 20:46:30 -0600
| Newsgroups | gmane.comp.python.visualpython.user |
|---|---|
| Message-ID | <CAFDG03hqMkd4+SmMOSR2ESCXGfByyCDGOBU12RiuJQH+pWAAiA@mail.gmail.com> |
I asked a colleague who might possibly have done some graph visualization using VPython, and here is what he said: I haven't really done anything along those lines, although it would be quite straightforward in principle to do Kamada-Kawai or something similar. Attached is a simple program from my undergrad class to do simulated annealing on the traveling salesman problem, which shows how one could visualize a graph or network. > From: Mark Adam <[email protected]> > Date: Tue, Jul 10, 2012 at 3:29 PM > Subject: [Visualpython-users] VPython for Graph Visualization > To: networkx-discuss-/[email protected] > Cc: visualpython-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org > > > I'm wondering if there's been anyone who's explored using the 3-d > visualization extension of python for graph visualization? > > Cheers, > > mark > pangaia.sf.net > _______________________________________________ > Visualpython-users mailing list > Visualpython-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org > https://lists.sourceforge.net/lists/listinfo/visualpython-users > ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Visualpython-users mailing list Visualpython-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/visualpython-users
salesman.py
(text/x-python, 1.6 KB)
#!/usr/bin/env python
from __future__ import print_function,division
from math import sqrt,exp
from numpy import empty
from random import random,randrange
from visual import sphere,curve,display
N = 25
R = 0.02
Tmax = 1.0
Tmin = 1e-3
a = 1e-4
# Function to calculate the magnitude of a vector
def mag(x):
return sqrt(x[0]**2+x[1]**2)
# Function to calculate the total length of the tour
def distance():
s = 0.0
for i in range(N):
s += mag(r[i+1]-r[i])
return s
# Choose N city locations and calculate the length of the route and
# the initial value of the distance
r = empty([N+1,2],float)
for i in range(N):
r[i,0] = random()
r[i,1] = random()
r[N] = r[0]
D = distance()
# Set up the graphics
display(center=[0.5,0.5])
for i in range(N):
sphere(pos=r[i],radius=R)
l = curve(pos=r,radius=R/2)
# Main loop
t = 0
T = Tmax
while T>Tmin:
# Cooling
t += 1
T = Tmax*exp(-a*t)
# Update the visualization every 100 moves
if t%100==0:
l.pos = r
# Choose two cities to swap and make sure they are distinct
i,j = randrange(1,N),randrange(1,N)
while i==j:
i,j = randrange(1,N),randrange(1,N)
# Swap them and calculate the change in distance
oldD = D
r[i,0],r[j,0] = r[j,0],r[i,0]
r[i,1],r[j,1] = r[j,1],r[i,1]
D = distance()
deltaD = D - oldD
# If the move is rejected, swap them back again
if random()>exp(-deltaD/T):
r[i,0],r[j,0] = r[j,0],r[i,0]
r[i,1],r[j,1] = r[j,1],r[i,1]
D = oldD