Advertisement
Guest User

Reconocimiento de patrones

a guest
Jul 15th, 2014
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.69 KB | None | 0 0
  1. import math
  2. import cPickle
  3. from Tkinter import *
  4. from tkMessageBox import *
  5. def crear():
  6.     db = []
  7.     cPickle.dump(db,open("memoria.mem","wb"))
  8.  
  9. matriz = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
  10.  
  11. memoria = cPickle.load(open("memoria.mem","rb"))
  12.  
  13. def imp(que,rango):#imprimir matriz
  14.     ac = 0
  15.     if ac==1:
  16.         c = 0
  17.         c1 = rango
  18.        
  19.         print "=================="
  20.         for i in range(0,rango):
  21.             a = que[c:c1]
  22.             k =  "       ".join(str(o)for o in a)
  23.             print ""
  24.             print k
  25.             c +=rango
  26.             c1+=rango
  27.         print "========================"
  28.     else:
  29.         pass
  30. def imp2(que,rango):#imprimir matriz auxiliar
  31.     ac = 1
  32.     if ac==1:
  33.         c = 0
  34.         c1 = rango
  35.        
  36.         print "=================="
  37.         for i in range(0,rango):
  38.             a = que[c:c1]
  39.             k =  "       ".join(str(o)for o in a)
  40.             print ""
  41.             print k
  42.             c +=rango
  43.             c1+=rango
  44.         print "========================"
  45.     else:
  46.         pass
  47.  
  48. def neurona(entrada,peso):#Valores de entradas y valores de "peso"
  49.     c = 0                                #conteo
  50.     multiplicados = []                   #buffer 1
  51.     for entrada1 in entrada:             #intera sobre la entradas
  52.         analisis1 = entrada1 * peso[c]   #multiplica las entradas por los pesos
  53.         multiplicados.append(analisis1)  #la aniade al buffer
  54.         c +=1                            #esto aumenta el conteo para ver el peso a medida que se repite el bucle
  55.     suma = 0                             #buffer de suma   
  56.     for i in multiplicados:              #intera sobre
  57.         suma+=i                          #va sumando a suma para sacar el resultado
  58.     peso = math.tanh(suma)               #calculo funcional para reprimir el valor de suma
  59.     if suma == len(entrada):             #si la suma es igual a la cantidad de valores entrada
  60.         return suma                      #devolver la suma
  61.     return peso
  62.  
  63. def final():
  64.     global candidatos,y
  65.     candidatos = {}
  66.     z = 0
  67.     for i in memoria:
  68.         peso = neurona(memoria[z],matriz)
  69.         print "peso:",peso
  70.         if peso <= 0.9999999999:
  71.             pass
  72.         else:
  73.             candidatos[peso] = memoria[z]
  74.         z+=1
  75.        
  76.     y = 0
  77.     for m in candidatos:
  78.         if m > y:
  79.             y = 0
  80.             y +=m
  81.         else:
  82.             pass
  83.            
  84.     if candidatos == {}:
  85.         print "No se que es"
  86.     else:
  87.         print "Comparado ",matriz," con ",candidatos[y]
  88.         print "asd:",matriz
  89.         print "RESULTA:"
  90.         imp2(candidatos[y],5)
  91.     print candidatos
  92. def aprender():
  93.     def memorizar(palabra,lon):
  94.         matriz=[]
  95.         c = 0
  96.         c1= 0
  97.         for i in range(0,len(palabra)):
  98.             for i in palabra:
  99.                 matriz.append(palabra[c1]*palabra[c])
  100.                 c+=1
  101.             c = 0
  102.             c1+=1
  103.         imp(matriz,lon)
  104.         return matriz
  105.     a =  memorizar(matriz,25)
  106.     memoria.append(a[0:25])
  107.     memoria.reverse()
  108.     print "Matriz aprendida:"
  109.     for t in memoria:
  110.         print t
  111.     cPickle.dump(memoria,open("memoria.mem","wb"))
  112.     resetear_tabla()
  113.  
  114. def analizar():
  115.     final()
  116.     resetear_tabla()
  117.     rellenar()
  118.  
  119.        
  120. #################INTERFAZ GRAFIC
  121. ventana = Tk()
  122. class  nume:
  123.     def una(self):
  124.         matriz[0] = 1
  125.         button1.config(bg="#5BADFF")
  126.         imp(matriz,5)
  127.     def dos(self):
  128.         matriz[1] = 1
  129.         button2.config(bg="#5BADFF")
  130.         imp(matriz,5)
  131.     def tres(self):
  132.         matriz[2] = 1
  133.         button3.config(bg="#5BADFF")
  134.         imp(matriz,5)
  135.     def cuatro(self):
  136.         matriz[3] = 1
  137.         button4.config(bg="#5BADFF")
  138.         imp(matriz,5)
  139.     def cinco(self):
  140.         matriz[4] = 1
  141.         button5.config(bg="#5BADFF")
  142.         imp(matriz,5)
  143.     def seis(self):
  144.         matriz[5] = 1
  145.         button6.config(bg="#5BADFF")
  146.         imp(matriz,5)
  147.     def siete(self):
  148.         matriz[6] = 1
  149.         button7.config(bg="#5BADFF")
  150.         imp(matriz,5)
  151.     def ocho(self):
  152.         matriz[7] = 1
  153.         button8.config(bg="#5BADFF")
  154.         imp(matriz,5)
  155.     def nueve(self):
  156.         matriz[8] = 1
  157.         button9.config(bg="#5BADFF")
  158.         imp(matriz,5)
  159.     def diez(self):
  160.         matriz[9] = 1
  161.         button10.config(bg="#5BADFF")
  162.         imp(matriz,5)
  163.     def once(self):
  164.         matriz[10] = 1
  165.         button11.config(bg="#5BADFF")
  166.         imp(matriz,5)
  167.     def doce(self):
  168.         matriz[11] = 1
  169.         button12.config(bg="#5BADFF")
  170.         imp(matriz,5)
  171.     def trece(self):
  172.         matriz[12] = 1
  173.         button13.config(bg="#5BADFF")
  174.         imp(matriz,5)
  175.  
  176.     def catorce(self):
  177.         matriz[13] = 1
  178.         button14.config(bg="#5BADFF")
  179.         imp(matriz,5)
  180.        
  181.     def quince(self):
  182.         matriz[14] = 1
  183.         button15.config(bg="#5BADFF")
  184.         imp(matriz,5)
  185.  
  186.     def diesiseis(self):
  187.         matriz[15] = 1
  188.         button16.config(bg="#5BADFF")
  189.         imp(matriz,5)
  190.  
  191.     def diesisiete(self):
  192.         matriz[16] = 1
  193.         button17.config(bg="#5BADFF")
  194.         imp(matriz,5)
  195.  
  196.     def diesiocho(self):
  197.         matriz[17] = 1
  198.         button18.config(bg="#5BADFF")
  199.         imp(matriz,5)
  200.        
  201.     def diesinueve(self):
  202.         matriz[18] = 1
  203.         button19.config(bg="#5BADFF")
  204.         imp(matriz,5)
  205.     def veinte(self):
  206.         matriz[19] = 1
  207.         button20.config(bg="#5BADFF")
  208.         imp(matriz,5)
  209.  
  210.     def veintiuno(self):
  211.         matriz[20] = 1
  212.         button21.config(bg="#5BADFF")
  213.         imp(matriz,5)
  214.     def veintidos(self):
  215.         matriz[21] = 1
  216.         button22.config(bg="#5BADFF")
  217.         imp(matriz,5)
  218.        
  219.     def veintitres(self):
  220.         matriz[22] = 1
  221.         button23.config(bg="#5BADFF")
  222.         imp(matriz,5)
  223.        
  224.     def veinticuatro(self):
  225.         matriz[23] = 1
  226.         button24.config(bg="#5BADFF")
  227.         imp(matriz,5)
  228.  
  229.     def veinticinco(self):
  230.         matriz[24] = 1
  231.         button25.config(bg="#5BADFF")
  232.         imp(matriz,5)
  233. core = nume()
  234. def resetear_tabla():
  235.     c = 0
  236.     for i in matriz:
  237.         matriz[c] = -1
  238.         c+=1
  239.     for t in botones:
  240.         t.config(bg="#FFFFFF")
  241. def borrarmem():
  242.     memoria = []
  243.     cPickle.dump(memoria,open("memoria.mem","wb"))
  244.  
  245. def rellenar():
  246.     c = 0
  247.     if candidatos == {}:
  248.         return 0
  249.     for i in candidatos[y]:
  250.        
  251.         if c == len(botones):
  252.             break
  253.         if i == 1:
  254.             botones[c].config(bg="#01D826")
  255.         if i == -1:
  256.             pass
  257.         c+=1
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269. ###botones interac
  270. ventana2 = Tk()
  271. ventana.title("Mem  by Rokerlauncher96")
  272. ventana.config(bg="#ECFFFF")
  273. ventana2.geometry("180x200+450+350")
  274. ventana2.title("Panel")
  275.  
  276. ventana.geometry("300x300+100+100")
  277. aprender=Button(ventana2,text='Aprender',command=aprender,width=455).pack()
  278. aprender=Button(ventana2,text='Analizar',command=analizar,width=455).pack()
  279. aprender=Button(ventana2,text='Resetear valores',command=resetear_tabla,width=455).pack()
  280. Label(ventana2,text="_____________________________").pack()
  281. aprender=Button(ventana2,text='Borrar memoria',command=borrarmem,width=455).pack()
  282. #######5
  283. button25=Button(ventana,text='25',command=core.veinticinco,bg="#FFFFFF")
  284. button25.place(relx=0.73, rely=0.77, relwidth=0.13, relheight=0.15)
  285. button24=Button(ventana,text='24',command=core.veinticuatro,bg="#FFFFFF")
  286. button24.place(relx=0.58, rely=0.77, relwidth=0.13, relheight=0.15)
  287. button23=Button(ventana,text='23',command=core.veintitres,bg="#FFFFFF")
  288. button23.place(relx=0.43, rely=0.77, relwidth=0.13, relheight=0.15)
  289. button22=Button(ventana,text='22',command=core.veintidos,bg="#FFFFFF")
  290. button22.place(relx=0.28, rely=0.77, relwidth=0.13, relheight=0.15)
  291. button21=Button(ventana,text='21',command=core.veintiuno,bg="#FFFFFF")
  292. button21.place(relx=0.12, rely=0.77, relwidth=0.13, relheight=0.15)
  293. #######4
  294. button20=Button(ventana,text='20',command=core.veinte,bg="#FFFFFF")
  295. button20.place(relx=0.73, rely=0.60, relwidth=0.13, relheight=0.15)
  296. button19=Button(ventana,text='19',command=core.diesinueve,bg="#FFFFFF")
  297. button19.place(relx=0.58, rely=0.60, relwidth=0.13, relheight=0.15)
  298. button18=Button(ventana,text='18',command=core.diesiocho,bg="#FFFFFF")
  299. button18.place(relx=0.43, rely=0.60, relwidth=0.13, relheight=0.15)
  300. button17=Button(ventana,text='17',command=core.diesisiete,bg="#FFFFFF")
  301. button17.place(relx=0.28, rely=0.60, relwidth=0.13, relheight=0.15)
  302. button16=Button(ventana,text='16',command=core.diesiseis,bg="#FFFFFF")
  303. button16.place(relx=0.12, rely=0.60, relwidth=0.13, relheight=0.15)
  304. #######3
  305. button15=Button(ventana,text='15',command=core.quince,bg="#FFFFFF")
  306. button15.place(relx=0.73, rely=0.43, relwidth=0.13, relheight=0.15)
  307. button14=Button(ventana,text='14',command=core.catorce,bg="#FFFFFF")
  308. button14.place(relx=0.58, rely=0.43, relwidth=0.13, relheight=0.15)
  309. button13=Button(ventana,text='13',command=core.trece,bg="#FFFFFF")
  310. button13.place(relx=0.43, rely=0.43, relwidth=0.13, relheight=0.15)
  311. button12=Button(ventana,text='12',command=core.doce,bg="#FFFFFF")
  312. button12.place(relx=0.28, rely=0.43, relwidth=0.13, relheight=0.15)
  313. button11=Button(ventana,text='11',command=core.once,bg="#FFFFFF")
  314. button11.place(relx=0.12, rely=0.43, relwidth=0.13, relheight=0.15)
  315. #######2
  316. diez = button10=Button(ventana,text='10',command=core.diez,bg="#FFFFFF")
  317. button10.place(relx=0.73, rely=0.26, relwidth=0.13, relheight=0.15)
  318. nueve = button9=Button(ventana,text='9',command=core.nueve,bg="#FFFFFF")
  319. button9.place(relx=0.58, rely=0.26, relwidth=0.13, relheight=0.15)
  320. ocho = button8=Button(ventana,text='8',command=core.ocho,bg="#FFFFFF")
  321. button8.place(relx=0.43, rely=0.26, relwidth=0.13, relheight=0.15)
  322. siete = button7=Button(ventana,text='7',command=core.siete,bg="#FFFFFF")
  323. button7.place(relx=0.28, rely=0.26, relwidth=0.13, relheight=0.15)
  324. seis = button6=Button(ventana,text='6',command=core.seis,bg="#FFFFFF")
  325. button6.place(relx=0.12, rely=0.26, relwidth=0.13, relheight=0.15)
  326. #######1
  327. cinco1 = button5=Button(ventana,text='5',command=core.cinco,bg="#FFFFFF")
  328. button5.place(relx=0.73, rely=0.09, relwidth=0.13, relheight=0.15)
  329. cuatro1 = button4=Button(ventana,text='4',command=core.cuatro,bg="#FFFFFF")
  330. button4.place(relx=0.58, rely=0.09, relwidth=0.13, relheight=0.15)
  331. tres1 = button3=Button(ventana,text='3',command=core.tres,bg="#FFFFFF")
  332. button3.place(relx=0.43, rely=0.09, relwidth=0.13, relheight=0.15)
  333. dos1 = button2=Button(ventana,text='2',command=core.dos,bg="#FFFFFF")
  334. button2.place(relx=0.28, rely=0.09, relwidth=0.13, relheight=0.15)
  335. uno1 = button1=Button(ventana,text='1',command=core.una,bg="#FFFFFF")
  336. button1.place(relx=0.12, rely=0.09, relwidth=0.13, relheight=0.15)
  337. botones = [button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11,button12,button13,button14,button15,button16,button17,button18,button19,button20,button21,button22,button23,button24,button25]
  338. ventana2.mainloop()
  339. ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement