-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (81 loc) · 3.07 KB
/
main.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
import numpy as np
import random
from matplotlib import pyplot as plt
from Person import Person
# Factors that affect the population
# 1. Starting Population
# 2. Infant mortality
# 3. Food
# 4. Fertility x and Fertility y
# 5. Healthcare
# 6. Agriculture
# 7. Chance of disaster
# 8. Age of death
startingPopulation = 50
infantMortality = 25
food = 0
agriculture = 5
disasterChance = 10
fertilityX = 18
fertilityY = 35
youthMortality = 45
ageOfDeath = 80
healthcare = 95
warDeaths = 2
populationList = []
# This function will run every year. It will produce food for the next year and if not enough able people produce
# food, people will die from starvation.
def harvest(food, agriculture):
ablePeople = 0
for person in populationList:
if person.age > 18:
ablePeople += 1
food += ablePeople * agriculture
if food < len(populationList):
del populationList[0: int(len(populationList) - food)]
else:
food -= len(populationList)
# This function will increase the population based on the given conditions
def reproduce(fertilityX, fertilityY):
for person in populationList:
if person.age > fertilityX and person.age < fertilityY and random.randint(0,5) == 1 and random.randint(0,100) > 5:
populationList.append(Person(0))
# This function will kickstart the simulation by adding the starting population
# to the populationList
def beginSim():
for x in range(startingPopulation):
populationList.append(Person(random.randint(18,50)))
# Begins Simulation by creating 50 people
beginSim()
popTrack = []
def runYear(food, agriculture, fertilityX, fertilityY, infantMortality, healthcare, warDeaths):
harvest(food, agriculture)
reproduce(fertilityX, fertilityY)
for person in populationList:
if person.age>ageOfDeath:
populationList.remove(person)
else:
person.age += 1
# There's a 10% chance of disaster happening
if random.randint(0,100)<disasterChance:
del populationList[0:int(random.uniform(0.05,0.1)*len(populationList))]
# There's a 5% chance of people dying due to diseases (even before 80)
if random.randint(0,100)>healthcare:
del populationList[0:int(random.uniform(0.05,0.1)*len(populationList))]
# War related deaths
if random.randint(0,100)<warDeaths:
del populationList[0:int(random.uniform(0.01, 0.02)*len(populationList))]
popTrack.append(len(populationList))
infantMortality *= 0.9
healthcare *= 1.1
warDeaths *= 0.9
return infantMortality, healthcare, warDeaths
while len(populationList)<100000 and len(populationList) > 1:
infantMortality, healthcare, warDeaths = runYear(food, agriculture, fertilityX, fertilityY, infantMortality, healthcare, warDeaths)
popTrack_np = np.array(popTrack)
from matplotlib import pyplot as plt
plt.plot(range(len(popTrack_np)), popTrack_np)
plt.title("Population vs Year")
plt.xlabel("Year")
plt.ylabel("Population")
plt.show()