Unsupported operand type(s) for +: 'float' and 'tuple'

2011-06-10 Thread Francesc Segura
Hello all, I'm new to this and I'm having problems on summing two
values at python.

I get the following error:

Traceback (most recent call last):
  File "C:\edge-bc (2).py", line 168, in 
if (costGG <= cost + T0):
TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

I'm working with networkx and my program is this one:


import networkx as NX
import pylab as P
from math import exp, log
import random
try:
import matplotlib.pyplot as plt
except:
raise

##N=27
N=30
ITERATIONS = 60
T0 = 0.5
RO = 0.99
NK = 20

def costG(G):

bc = NX.edge_betweenness_centrality(G,normalized=True)
edges = NX.edges(G)

for i in range(len(edges)):
total = 0
cost = 0
factor = 1

liedges = list(edges[i])
linode1 = list(liedges[0])
linode2 = list(liedges[1])

distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]-
linode1[1])%N)^2)

edgecentrality = bc[edges[i]]

factor = (distance-19790)*(-0.55586)

cost = distance*edgecentrality*factor

total = total + cost

return(total)

def avedistance(G):




return (AvgDist)

def costGeasy(G):
bc = NX.edge_betweenness_centrality(G,normalized=True)
total = 0
for i in range(len(bc)):
total=total+bc.values()[i]

return (total)


G = NX.grid_2d_graph(N,N,True)

for i in range(N):
for j in range(N):
G.add_edge((i,j),((i+1) % N ,(j+1) % N))
G.add_edge((i,j),((i-1) % N ,(j+1) % N))


NODES=NX.number_of_nodes(G)
nod=NX.nodes(G)
EDGES=NX.number_of_edges(G)
edg=NX.edges(G)

pos={}
for i in range(NODES):
pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0))

NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Inicial graph, Toroidal 27x27, degree 8")
plt.savefig("initial_grid_malla.png")
plt.show()

pos=NX.spring_layout(G,iterations=100)
NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Inicial graph, Toroidal 27x27, degree 8")
plt.savefig("initial_grid_tor.png")
plt.show()

initGr = G
best_graph = G
best_cost = costG(G)
average_distance = avedistance(G)
initCost = best_cost
initGHist = NX.degree_histogram(G)

##print NX.info(initGr)
##print 'Diameter =  %f ' % (NX.diameter(G))
##print 'Avg Clust Coeff. NetworkX =  %-6.4f ' %
(NX.average_clustering(G))
##print 'Avg Dist. NetworkX =  %-6.4f ' % (average_distance)
##print 'Distribucio de Graus'
##print initGHist
##print 'Cost inicial'
##print initCost

T0 = initCost*0,1

for y in range(NK):
for x in range(ITERATIONS):
cost = costG(G)
if (cost < (best_cost)):
best_graph = G
best_cost = cost
GG = G
u = random.randint(0,NODES-1)
while GG.degree(nod[u]) <= 1:
u = random.randint(0,NODES-1)
v = random.randint(0,GG.degree(nod[u])-1)
GG.remove_edge(nod[u],GG[nod[u]].keys()[v])
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
adj=G.adjacency_list()
while ((nod[b] in adj[a]) or (b == a)):
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
GG.add_edge(nod[a],nod[b])
while (NX.is_connected(GG) == 0):
GG = G
u = random.randint(0,NODES-1)
while GG.degree(nod[u]) <= 1:
u = random.randint(0,NODES-1)
v = random.randint(0,GG.degree(nod[u])-1)
GG.remove_edge(nod[u],GG[nod[u]].keys()[v])
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
adj=GG.adjacency_list()
while ((nod[b] in adj[a]) or (b == a)):
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
GG.add_edge(nod[a],nod[b])

costGG = costG(GG)
if (costGG <= cost):
G = GG
else:
if (costGG <= cost + T0):
G = GG

T0 = T0 * RO
print 'IT %d' % y
print 'BEST %f ' % best_cost


best_graph = G
print NX.info(best_graph)
print 'Diameter =  %f ' % (NX.diameter(best_graph))
print 'Avg Clust Coeff. NetworkX =  %-6.4f ' %
(NX.average_clustering(best_graph))
average_distance = avedistance(best_graph)
print 'Avg Dist. NetworkX =  %-6.4f ' % (average_distance)
print 'Distribucio de Graus'
print NX.degree_histogram(best_graph)
print 'Millor Cost'
print best_cost

NX.write_edgelist(best_graph,'optimal-graph.dat')

pos={}
for i in range(NODES):
pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0))


NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Final graph, Toroidal 27x27, degree 8")
plt.savefig("final_grid_malla.png")
plt.show()

pos=NX.spring_layout(G,iterations=100)
NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Final graph, Toroidal 27x27, degree 8")
plt.savefig("final_grid_tor.png")
plt.show()
-- 
http://mail.python.org/mailman/listinfo/python-list


Unsupported operand type(s) for +: 'float' and 'tuple'

2011-06-10 Thread Francesc Segura
Hello all, I'm new to this and I'm having problems on summing two
values at python.

I get the following error:

Traceback (most recent call last):
  File "C:\edge-bc (2).py", line 168, in 
if (costGG <= cost + T0):
TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

I'm working with networkx and my program is this one:


import networkx as NX
import pylab as P
from math import exp, log
import random
try:
import matplotlib.pyplot as plt
except:
raise

##N=27
N=30
ITERATIONS = 60
T0 = 0.5
RO = 0.99
NK = 20

def costG(G):

bc = NX.edge_betweenness_centrality(G,normalized=True)
edges = NX.edges(G)

for i in range(len(edges)):
total = 0
cost = 0
factor = 1

liedges = list(edges[i])
linode1 = list(liedges[0])
linode2 = list(liedges[1])

distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]-
linode1[1])%N)^2)

edgecentrality = bc[edges[i]]

factor = (distance-19790)*(-0.55586)

cost = distance*edgecentrality*factor

total = total + cost

return(total)

def avedistance(G):




return (AvgDist)

def costGeasy(G):
bc = NX.edge_betweenness_centrality(G,normalized=True)
total = 0
for i in range(len(bc)):
total=total+bc.values()[i]

return (total)


G = NX.grid_2d_graph(N,N,True)

for i in range(N):
for j in range(N):
G.add_edge((i,j),((i+1) % N ,(j+1) % N))
G.add_edge((i,j),((i-1) % N ,(j+1) % N))


NODES=NX.number_of_nodes(G)
nod=NX.nodes(G)
EDGES=NX.number_of_edges(G)
edg=NX.edges(G)

pos={}
for i in range(NODES):
pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0))

NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Inicial graph, Toroidal 27x27, degree 8")
plt.savefig("initial_grid_malla.png")
plt.show()

pos=NX.spring_layout(G,iterations=100)
NX.draw(G,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Inicial graph, Toroidal 27x27, degree 8")
plt.savefig("initial_grid_tor.png")
plt.show()

initGr = G
best_graph = G
best_cost = costG(G)
average_distance = avedistance(G)
initCost = best_cost
initGHist = NX.degree_histogram(G)

##print NX.info(initGr)
##print 'Diameter =  %f ' % (NX.diameter(G))
##print 'Avg Clust Coeff. NetworkX =  %-6.4f ' %
(NX.average_clustering(G))
##print 'Avg Dist. NetworkX =  %-6.4f ' % (average_distance)
##print 'Distribucio de Graus'
##print initGHist
##print 'Cost inicial'
##print initCost

T0 = initCost*0,1

for y in range(NK):
for x in range(ITERATIONS):
cost = costG(G)
if (cost < (best_cost)):
best_graph = G
best_cost = cost
GG = G
u = random.randint(0,NODES-1)
while GG.degree(nod[u]) <= 1:
u = random.randint(0,NODES-1)
v = random.randint(0,GG.degree(nod[u])-1)
GG.remove_edge(nod[u],GG[nod[u]].keys()[v])
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
adj=G.adjacency_list()
while ((nod[b] in adj[a]) or (b == a)):
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
GG.add_edge(nod[a],nod[b])
while (NX.is_connected(GG) == 0):
GG = G
u = random.randint(0,NODES-1)
while GG.degree(nod[u]) <= 1:
u = random.randint(0,NODES-1)
v = random.randint(0,GG.degree(nod[u])-1)
GG.remove_edge(nod[u],GG[nod[u]].keys()[v])
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
adj=GG.adjacency_list()
while ((nod[b] in adj[a]) or (b == a)):
a = random.randint(0,NODES-1)
b = random.randint(0,NODES-1)
GG.add_edge(nod[a],nod[b])

costGG = costG(GG)
if (costGG <= cost):
G = GG
else:
if (costGG <= cost + T0):
G = GG

T0 = T0 * RO
print 'IT %d' % y
print 'BEST %f ' % best_cost


best_graph = G
print NX.info(best_graph)
print 'Diameter =  %f ' % (NX.diameter(best_graph))
print 'Avg Clust Coeff. NetworkX =  %-6.4f ' %
(NX.average_clustering(best_graph))
average_distance = avedistance(best_graph)
print 'Avg Dist. NetworkX =  %-6.4f ' % (average_distance)
print 'Distribucio de Graus'
print NX.degree_histogram(best_graph)
print 'Millor Cost'
print best_cost

NX.write_edgelist(best_graph,'optimal-graph.dat')

pos={}
for i in range(NODES):
pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0))


NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Final graph, Toroidal 27x27, degree 8")
plt.savefig("final_grid_malla.png")
plt.show()

pos=NX.spring_layout(G,iterations=100)
NX.draw(best_graph,pos,node_color='r',node_size=20,with_labels=False,width=1)
plt.title("Final graph, Toroidal 27x27, degree 8")
plt.savefig("final_grid_tor.png")
plt.show()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unsupported operand type(s) for +: 'float' and 'tuple'

2011-06-10 Thread Francesc Segura
On 10 jun, 13:38, Tim Chase  wrote:
> On 06/10/2011 05:30 AM, Francesc Segura wrote:
>
> > Hello all, I'm new to this and I'm having problems on summing two
> > values at python.
>
> > I get the following error:
>
> > Traceback (most recent call last):
> >    File "C:\edge-bc (2).py", line 168, in
> >      if (costGG<= cost + T0):
> > TypeError: unsupported operand type(s) for +: 'float' and 'tuple'
>
> > I'm working with networkx and my program is this one:
> ...
> > T0 = initCost*0,1
>
> Here, you're setting T0 to the tuple "(initCost*0, 1)" == "(0,
> 1)".  I think you mean to use a period instead of a comma.
>
> You then try to add that to a float (cost), and Python doesn't
> like that.  I wouldn't either :)
>
> -tkc

Thanks a lot, I am a noob retard!
-- 
http://mail.python.org/mailman/listinfo/python-list