Python - Como pasar una funcion como parametro de otra_ Por favor ayuda!!!

 
Vista:
sin imagen de perfil

Como pasar una funcion como parametro de otra_ Por favor ayuda!!!

Publicado por laura (2 intervenciones) el 18/10/2015 04:28:10
Hola a todos.. necesito una ayudita con el siguiente codigo... estoy implementando el algoritmo A* para resolver el juego de 8, para esto estoy usando tres heuristicas distintas, cada una esta definida en una funcion independiente, lo que quiero hacer es a la funcion del A* pasarle como parametro la funcion heuristica que quiero implementar, pero cuando corro el codigo pues se me queda como si entrara en un ciclo y no me funciona... he hecho una depuracion del mismo y no veo dnd esta causando el problema.. por favor si alguien pudiera iluminarme un poco con esto... muchas gracias de antemano..

Aqui les va mi codigo...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def heur(o):
 
    #Heuristica 1. Numero de nodos fuera de lugar
 
    def heur1(start,end,g):
        h1=0
        for rec in range(3):
            if start[rec][0]!=end[rec][0] and start[rec][0]!=0:
                h1+=1
            if start[rec][1]!=end[rec][1] and start[rec][1]!=0:
                h1+=1
            if start[rec][2]!=end[rec][2] and start[rec][2]!=0:
                h1+=1
        global f
        f=g+h1
        return f
 
    #Heuristica 2.Distancia Manhattan
 
    def heur2(estado,meta,g):
        d=e=f=l=h2=0
        for d in range(3):
            for p in range(3):
                valor=estado[d][e]
                while meta[f][l]!= valor:
                    l+=1
                    if l>2:
                        l-=3
                        f+=1
                        if f>2:
                          f-=3
                x2=f
                y2=l
                h2+=abs(x2-d)+abs(y2-e)
                e+=1
                if e>2:
                    e-=1
                    d+=1
                    if d>2:
                          d-=1
 
        global hn
        hn=h2
        f=g+h2
 
        print "esta es h2",h2
        print f
        return f
 
    #Recorrido en sentido de las manecillas del reloj
 
    def sigele(s,t,lista):
        global sigel
 
        if s==0 and t!=2:
            sigel=lista[s][t+1]
        if s==0 and t==2:
            sigel=lista[s+1][t]
        if s==1 and t==2:
            sigel=lista[s+1][t]
        if s==2 and t==2:
            sigel=lista[s][t-1]
        if s==2 and t==1:
            sigel=lista[s][t-1]
        if s==2 and t==0:
            sigel=lista[s-1][t]
        if s==1 and t==0:
            sigel=lista[s-1][t]
 
        return sigel
 
    #Heuristica 3.Funcion h2+3sec(n)        
 
    def heur3(estado,meta):
            i=j=n=m=h3=0
            print estado
            for k in range(9):
                elemento=estado[i][j]
                if elemento!=0 or i!=1 and j!=1:
                    elemento1= sigele(i,j,estado)
                    while meta[n][m]!=elemento:
                        m+=1
                        if m==3:
                            n+=1
                            m-=3
                        if n==3:
                            n-=3
                    sig = sigele(n,m,meta)
                if elemento1!=sig:
                    h3+=2
 
                if (i==1 and j==1 and estado[i][j]!=0):
                    h3+=1
 
                i+=1
                if i==3:
                    i-=3
                    j+=1
                    if j==3:
                        j-=3
            f=hn+3*h3
 
            print "heuristica",f
            return f
 
    if s==1:
        heur1(start,end,g)
    elif s==2:
        heur2(start,end,g)
    elif s==3:
        heur3(start,end)
 
 
#Algoritmo A*
 
def Astar(heur):
    while len(abierta)!=0:
        for M in abierta:
            X= abierta.pop(0)
            cerrada.append(X)
            if X==end:
                print cerrada
                print fn
                break
 
            busc0(X)
            mov=[]
            posmov(col,fil,mov)
            intercambio(col,fil,mov,X)
            result=[]
            for n in hijos:
                fn=heur
                result.append(fn)
            minimo=min(result)
            m=result.index(minimo)
            S=hijos[m]
            global g
            g+=1
            for i in abierta:
                if i==S:
                    abierta.remove(i)
            for i in cerrada:
                if i==S:
                    cerrada.remove(i)
            abierta.append(S)
 
print "resultados de la primera heuristica"
Astar(heur(1))
print "resultados de la segunda heuristica"
Astar(heur(2))
print "resultados de la tercera heuristica"
Astar(heur(3))
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder