Created
September 4, 2010 18:14
-
-
Save avances123/565375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import random | |
class Individuo: | |
def moverAContigua(self): | |
exito=False | |
while exito==False: | |
incx = random.choice([-1,0,1]) | |
incy = random.choice([-1,0,1]) | |
if self.x+incx > 0 and self.x+incx <= self.borde and self.y+incy > 0 and self.y+incy <= self.borde: | |
exito=True | |
if incx == 0 and incy == 0: | |
exito=False | |
# Aplicamos el incremento | |
self.x += incx | |
self.y += incy | |
self.movimientos += 1 | |
def moverACualquiera(self): | |
self.x=random.randrange(1,self.borde) | |
self.y=random.randrange(1,self.borde) | |
self.movimientos += 1 | |
def __init__(self,x,y,borde=100): | |
self.x=x | |
self.y=y | |
self.movimientos=0 | |
self.borde=borde |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import sys | |
import getopt | |
import psycopg2 | |
import random | |
from individuo import Individuo | |
def escribeEnFichero(): | |
logfile = open('test.csv', 'a') | |
logfile.write('line 2') | |
logfile.close() | |
def conectaADB(): | |
# Conectamos a la base de datos | |
try: | |
#conn = psycopg2.connect("dbname='busqueda' user='postgres' host='localhost' password='12345'"); | |
conn = psycopg2.connect("dbname='busqueda' user='fabio' host='localhost' password='12345'"); | |
except Exception, e: | |
#print "No puedo conectar a db" | |
print e | |
return | |
return conn | |
def escribeEnDB(conn,tipo,a_movimientos,b_movimientos,exito): | |
cur = conn.cursor() | |
tabla='test_encontrarse' | |
columnas=['tipo','a_movimientos','b_movimientos','exito'] | |
valores=['\'' + tipo + '\'',str(a_movimientos),str(b_movimientos),str(exito)] | |
statement = 'INSERT INTO ' + tabla + ' (' + ','.join(columnas) + ') VALUES (' + ','.join(valores) + ')' | |
#print statement | |
try: | |
cur.execute(statement) | |
except Exception, e: | |
print e.pgerror | |
conn.commit() | |
def test4(conn,limite): | |
""" | |
Test movimiento aleatorio total con los dos individuos moviendose | |
""" | |
tipo='test4' | |
# Situamos a los individuos inicialmente | |
a=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
b=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
# Bucle de busqueda | |
exito=False | |
while (not exito): | |
a.moverAContigua() | |
b.moverAContigua() | |
if a.movimientos >= limite: | |
break | |
if b.movimientos >= limite: | |
break | |
if (a.x == b.x and a.y == b.y): # Si estan en la misma casilla... exito | |
exito=True | |
escribeEnDB(conn,tipo,a.movimientos,b.movimientos,exito) | |
def test3(conn,limite): | |
""" | |
Test movimiento aleatorio a las casillas contiguas con un individuo quieto | |
""" | |
tipo='test3' | |
# Situamos a los individuos inicialmente | |
a=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
b=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
# Bucle de busqueda | |
exito=False | |
while (not exito): | |
a.moverAContigua() | |
if a.movimientos >= limite: | |
break | |
if (a.x == b.x and a.y == b.y): # Si estan en la misma casilla... exito | |
exito=True | |
escribeEnDB(conn,tipo,a.movimientos,b.movimientos,exito) | |
def test2(conn,limite): | |
""" | |
Test movimiento aleatorio total con los dos individuos moviendose | |
""" | |
tipo='test2' | |
# Situamos a los individuos inicialmente | |
a=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
b=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
# Bucle de busqueda | |
exito=False | |
while (not exito): | |
a.moverACualquiera() | |
b.moverACualquiera() | |
if a.movimientos >= limite: | |
break | |
if b.movimientos >= limite: | |
break | |
if (a.x == b.x and a.y == b.y): # Si estan en la misma casilla... exito | |
exito=True | |
escribeEnDB(conn,tipo,a.movimientos,b.movimientos,exito) | |
def test1(conn,limite): | |
""" | |
Test movimiento aleatorio total con un individuo quieto | |
""" | |
tipo='test1' | |
# Situamos a los individuos inicialmente | |
a=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
b=Individuo(random.randrange(1,borde),random.randrange(1,borde),borde) | |
# Bucle de busqueda | |
exito=False | |
while (not exito): | |
# Solo muevo uno | |
a.moverACualquiera() | |
if a.movimientos >= limite: | |
break | |
if (a.x == b.x and a.y == b.y): # Si estan en la misma casilla... exito | |
exito=True | |
escribeEnDB(conn,tipo,a.movimientos,b.movimientos,exito) | |
if __name__ == "__main__": | |
# Limite del tablero | |
borde=100 | |
# TODO hacerla global | |
conn=conectaADB() | |
for i in range(10000): | |
print i | |
test1(conn,20000) | |
test2(conn,20000) | |
test3(conn,20000) | |
test4(conn,20000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment