Created
November 20, 2014 14:10
-
-
Save joaomeirelles/5bf730da169b97b39e75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 24-10-14: v01 alometria para rendimento CN | |
# 30-10-14 v02 adicionado o mecanismo de interacao e de morte JM | |
# 05-11-14 v02 adicionado o mecanismo de contador de encontros JM | |
# 05-11-14 v02 adicionado o tamanho da cidade como um fator da populacao e o BS JM | |
import math | |
import psycopg2 | |
import numpy as np | |
import random as rn | |
import matplotlib.pyplot as plt | |
runs = 10 | |
Npop = 100 | |
#Area = Npop**(0.8) | |
Area = 0 | |
#Lcity = np.sqrt(Area) | |
Lcity = 0 | |
Tempo = 100 | |
encontros = 0 | |
passo = 1 | |
#----------------------------------------------------------------------- | |
def cria_pop(populacao): | |
p = [] | |
for i in range(populacao): | |
#ag={'latag': rn.randrange(Lcity) ,'lonag': rn.randrange(Lcity), \ ##DETERMINAR SE INT OU FLOAT | |
ag={'latag': round(rn.uniform(0, Lcity)) ,'lonag': round(rn.uniform(0,Lcity)), \ | |
#ag={'latag': rn.uniform(0, Lcity) ,'lonag': rn.uniform(0,Lcity), \ | |
#ag={'latag': Lcity/2 ,'lonag': Lcity/2, \ | |
'gag': 1, 'yag': 500,'tag': 10,'azero': 0,'lag': passo} | |
p.append(ag) | |
return p | |
#----------------------------------------------------------------------- | |
def plota_city(p,cor): | |
for ag in p: | |
plt.plot(ag['latag'],ag['lonag'],cor) | |
plt.ylim([-1,Lcity+1]) | |
plt.xlim([-1,Lcity+1]) | |
#----------------------------------------------------------------------- | |
def ag_walk(p): | |
for ag in p: | |
pss = [-passo, passo] | |
ag['latag'] = (ag['latag'] + rn.choice(pss)) % Lcity | |
ag['lonag'] = (ag['lonag'] + rn.choice(pss)) % Lcity | |
ag['yag'] = ag['yag'] - 1 | |
if ag['yag']==0 : #die | |
p.remove(ag) #die | |
#----------------------------------------------------------------------- | |
def ag_interact(p): | |
#for ag in p: | |
for i in xrange(len(pop)): | |
for j in xrange(len(pop)): | |
if (i != j): | |
if ((pop[i]['latag']-pop[j]['latag']) ==pop[i]['azero'] ): | |
if ((pop[i]['lonag']-pop[j]['lonag']) ==pop[i]['azero']): | |
pop[i]['yag'] = pop[i]['yag'] + pop[i]['gag'] | |
#print("interacao~!") | |
global encontros | |
encontros = encontros + 1 | |
#pop[j]['yag'] = pop[j]['yag'] + g | |
#if ((ag['latag']-ag['latag']) + (ag['lonag']-ag['lonag']) == 0): | |
# ag['yag'] = ag['yag'] + g | |
#print(ag['latag']) | |
# testar encontro | |
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
#plota_city(pop,'ob') | |
data = [] | |
data_pop = [] | |
data_encontros = [] | |
teorico_encontros = [] | |
teorico_area = [] | |
for population in range (Npop+1): | |
pop = cria_pop(population) | |
Area = population**(0.97) | |
teoricoencontros = population**(1.15) | |
#Lcity = np.sqrt(Area) | |
Lcity = Area | |
#print Area | |
for t in range(Tempo): | |
ag_walk(pop) | |
ag_interact(pop) | |
data_pop.append(population) | |
data_encontros.append(encontros) | |
teorico_encontros.append(teoricoencontros) | |
teorico_area.append(Area) | |
#print Area | |
print (population, Area, encontros) | |
#run={'encontros':encontros, 'pop':population} | |
#data.append(run) | |
encontros = 0 | |
#for ag in pop: | |
# print(pop[0]['latag']) | |
# print(pop[1]['latag']) | |
# print(len(pop)) | |
# print(ag['yag']) | |
#print encontros | |
#data.append(run) | |
# run={'encontros':encontros, 'pop':p} | |
# print run | |
# data.append(run) | |
#print ('pop', p, " ", 'encontros', encontros) | |
#print data_pop | |
#print data_encontros | |
#print encontros | |
#for t in range(Tempo): | |
# ag_walk(pop) | |
# ag_interact(pop) | |
#print encontros | |
plt.scatter(data_pop, data_encontros, label='encontros') | |
plt.loglog(data_pop, teorico_encontros, label='teorico encontros') | |
plt.loglog(data_pop, teorico_area, label='teorico area') | |
plt.legend(loc=2) | |
plt.show() | |
#print encontros | |
#plota_city(pop,'or') | |
#plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment