-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spatial_model_0.2.py
141 lines (125 loc) · 4.97 KB
/
Spatial_model_0.2.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 16 08:39:37 2014
@author: Jboeye
"""
import random as rnd
import Tkinter as tk
import numpy as np
class Visual:
'''This class arranges the visual output.'''
def __init__(self, max_x, max_y):
'''Initialize the visual class'''
self.zoom = 15
self.max_x = max_x
self.max_y = max_y
self.root = tk.Tk()
self.canvas = tk.Canvas(self.root,
width = self.max_x * self.zoom,
height = self.max_y * self.zoom) #create window
self.canvas.pack()
self.canvas.config(background = 'white')
#self.squares = np.empty((self.max_x, self.max_y),dtype=object)
#self.initialize_squares()
def create_individual(self,x,y):
'''Create circle for individual'''
radius = 0.1
return self.canvas.create_oval((x - radius) * self.zoom,
(y - radius) * self.zoom,
(x + radius) * self.zoom,
(y + radius) * self.zoom,
outline='black',
fill='black')
def move_drawing(self,drawing, x, y):
radius= 0.1
self.canvas.coords(drawing,(x - radius) * self.zoom,
(y - radius) * self.zoom,
(x + radius) * self.zoom,
(y + radius) * self.zoom)
def color_square(self, resources, x, y):
'''Changes the color of the square'''
color = (resources)/float(100)
if color < 0:
color = 0
elif color > 1:
color = 1
green = int(255 * color)
red = 255 - green
blue = 0
rgb = red, green, blue
hex_code = '#%02x%02x%02x' % rgb
self.canvas.itemconfigure(self.squares[x, y],fill=str(hex_code))
def initialize_squares(self):
'''returns a square (drawing object)'''
for x in xrange(self.max_x):
for y in xrange(self.max_y):
self.squares[x, y] = self.canvas.create_rectangle(self.zoom * x,
self.zoom * y,
self.zoom * x + self.zoom,
self.zoom * y + self.zoom,
outline = 'black',
fill = 'black')
class Individual:
'''Class that regulates individuals and their properties'''
def __init__(self,
x,
y,
resources,
drawing):
'''Initialization'''
self.x = x
self.y = y
self.resources = resources
self.drawing = drawing
self.age = 0
def move(self):
'''Calculates movement'''
dx = rnd.uniform(-1,1)
dy = rnd.uniform(-1,1)
self.x = self.x + dx
self.y = self.y + dy
class Metapopulation:
'''Contains the whole population, regulates daily affairs'''
def __init__(self,
max_x,
max_y):
'''Initialization'''
self.max_x = max_x
self.max_y = max_y
self.visual = Visual(self.max_x, self.max_y)
self.population = []
self.initialize_pop()
def initialize_pop(self):
'''Initialize individuals'''
startpop = 1000
start_resources = 10
for n in range(startpop):
x = rnd.uniform(0,self.max_x)
y = rnd.uniform(0,self.max_y)
drawing = self.visual.create_individual(x, y)
self.population.append(Individual(x, y,
start_resources,
drawing))
def a_day_in_the_life(self):
'''Replenish patches and draw visual'''
oldpop = self.population[:]
del self.population[:]
for indiv in oldpop:
if indiv.resources >= 0:
indiv.move()
self.visual.move_drawing(indiv.drawing,
indiv.x,
indiv.y)
if rnd.random() < 0.3:
indiv.resources += 5
indiv.age += 1
indiv.resources -= 2
self.population.append(indiv)
else:
self.visual.canvas.delete(indiv.drawing)
print len(self.population)
self.visual.canvas.update()
meta = Metapopulation(40,40)
for timer in range(40):
meta.a_day_in_the_life()
tk.mainloop()