En Fri, 10 Jun 2011 07:30:28 -0300, Francesc Segura <frseg...@gmail.com>
escribió:

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 <module>
    if (costGG <= cost + T0):
TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

I see Tim Chase already told you about this error. Let me make a few
comments about the rest.

try:
    import matplotlib.pyplot as plt
except:
    raise

I guess the above try/except was left from some earlier debugging attempt
- such an except clause is useless, just omit it.

T0 = 0.5
RO = 0.99

Perhaps those names make sense in your problem at hand, but usually I try
to use more meaningful ones. 0 and O look very similar in some fonts.

    for i in range(len(edges)):
        total = 0
        cost = 0
        factor = 1
       liedges = list(edges[i])
        linode1 = list(liedges[0])
        linode2 = list(liedges[1])

list(something) creates a new list out of the elements from `something`.
You're just iterating here, so there is no need to duplicate those lists.
In addition, Python is not C: the `for` statement iterates over a
collection, you don't have to iterate over the indices and dereference
each item:

for liedges in edges:
    linode1 = liedges[0]
    linode2 = liedges[1]


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

That doesn't evaluate what you think it does. ^ is the "bitwise xor"
operator, and I bet you want **, the "power" operator.


        total = total + cost
    return(total)

return is not a function but a statement; those () are unnecesary and
confusing.
And I think you want to initialize total=0 *before* entering the loop;
also, initializing cost and factor is unnecesary.

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)

bc = NX.edge_betweenness_centrality(G,normalized=True)
values = bc.values()
total = sum(values)
return total

==>

return sum(bc.values())


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

In Python version 2.x, 1/3 evals to 0, but that's a mistake; it is fixed
in the 3.x version. If you put this line at the top of your script:
  from __future__ import division
then 1/3 returns 0.3333...
When you actually want integer division, use //, like 1//3

So we can rewrite the above as:

  from __future__ import division
...
for node in nod:
    pos[node] = (node[0] / N, node[1] / N)

Another way, not relying on true division:

divisor = float(N)
for node in nod:
    pos[node] = (node[0] / divisor, node[1] / divisor)

or even:

pos = dict((node, (node[0] / divisor, node[1] / divisor)) for node in nod)


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

Again, I think this doesn't do what you think it does. GG = G means "let's
use the name GG for the object currently known as G". GG is not a "copy"
of G, just a different name for the very same object. Later operations
like GG.remove_edge(...) modify the object - and you'll see the changes in
G, and in best_graph, because those names all refer to the same object.
I think you'll benefit from reading this:
http://effbot.org/zone/python-objects.htm

        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])

As above, I'd avoid using indexes, take two random nodes using
random.sample instead, and avoid adjacency_list():

while True:
    a, b = random.sample(nod, 2)
    if b not in G[a]:
      break
GG.add_edge(a, b)

(mmm, I'm unsure of the adjacency test, I've used networkx some time ago
but I don't have it available right now)

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to