Galvanizing Growth Python Simulation

import pandas as pd
from random import random
from math import floor, log
import matplotlib
from matplotlib import pyplot as plt
from numpy.random import normal
group = {}
growth = {}
countries = [('China', 137e7, 274e6), ('India', 127e7, 254e6), ('United States', 324e6, 648e5), ('Indonesia', 258e6, 516e5), ('Brazil', 206e6, 412e5), ('Pakistan', 202e6, 404e5), ('Nigeria', 186e6, 372e5), ('Bangladesh', 156e6, 312e5), ('Russia', 142e6, 284e5), ('Japan', 127e6, 254e5), ('Mexico', 123e6, 246e5), ('Philippines', 103e6, 206e5), ('Ethiopia', 102e6, 204e5), ('Vietnam', 953e5, 1906e4), ('Egypt', 947e5, 1894e4), ('Iran', 828e5, 1656e4), ('DR Congo', 813e5, 1626e4), ('Germany', 807e5, 1614e4), ('Turkey', 803e5, 1606e4), ('Thailand', 682e5, 1364e4), ('France', 668e5, 1336e4), ('United Kingdom', 644e5, 1288e4), ('Italy', 62e6, 124e5), ('Burma', 569e5, 1138e4), ('South Africa', 543e5, 1086e4), ('Tanzania', 525e5, 105e5), ('South Korea', 509e5, 1018e4), ('Spain', 486e5, 972e4), ('Colombia', 472e5, 944e4), ('Kenya', 468e5, 936e4), ('Ukraine', 442e5, 884e4), ('Argentina', 439e5, 878e4), ('Algeria', 403e5, 806e4), ('Poland', 385e5, 77e5), ('Uganda', 383e5, 766e4), ('Iraq', 381e5, 762e4), ('Sudan', 367e5, 734e4), ('Canada', 354e5, 708e4), ('Morocco', 337e5, 674e4), ('Afghanistan', 333e5, 666e4), ('Malaysia', 31e6, 62e5), ('Venezuela', 309e5, 618e4), ('Peru', 307e5, 614e4), ('Uzbekistan', 295e5, 59e5), ('Nepal', 29e6, 58e5), ('Saudi Arabia', 282e5, 564e4), ('Yemen', 274e5, 548e4), ('Ghana', 269e5, 538e4), ('Mozambique', 259e5, 518e4), ('North Korea', 251e5, 502e4)]
groupKeys = []
scale = 50
for i, j, k in countries:
  group[i] = [[0, j, 0, k]]
  growth[i] = [0]
  groupKeys.append(i)
group[groupKeys[0]] = [[13, countries[0][1]-13, 0, countries[0][2]]]
growth[groupKeys[0]] = [13]
e = 2.7183
def eDistribute(bottom, top, original):
  if(top == bottom):
    return top
  return top*e**((bottom-original)/(top-bottom))
def grow(sets, keys, past, index, spread=lambda a,b:1, expose=lambda a:1, expand=0.05, cut=5, spreadFactor=0.5, growthFactor=10):
  spreadSum = 0
  for i in keys:
    if(sets[i][index][0]+sets[i][index][1] == 0):
      adjExpand = 0
    else:
      adjExpand = expand * sets[i][index][1]/(sets[i][index][0]+sets[i][index][1])
    if(len(sets[i]) == index+1):
      sets[i].append([sets[i][index][0], sets[i][index][1], sets[i][index][2], sets[i][index][3]])
      past[i].append(0)
    for j in keys:
      if(i != j):
        currentSpread = spread(past[i], past[j])
        spreadSum -= currentSpread
        todaySpread = round(normal(1.5, 1)*eDistribute(0, spreadFactor, currentSpread)*sets[i][index][0]*expand)
        if(todaySpread > 0):
          if(len(sets[j]) == index+1):
            sets[j].append([sets[j][index][0]+todaySpread, sets[j][index][1]-todaySpread, sets[j][index][2], sets[j][index][3]])
            past[j].append(todaySpread)
          else:
            sets[j][index+1][0] += todaySpread
            sets[j][index+1][1] -= todaySpread
            if(sets[j][index+1][1] < 0):
              sets[j][index+1][0] += sets[j][index+1][1]
              sets[j][index+1][1] = 0
            past[j][index+1] += todaySpread
    currentSpread = expose(past[i])
    spreadSum -= currentSpread
    newGrowth = round(normal(1.5, 1)*eDistribute(0, growthFactor, currentSpread)*sets[i][index][0]*adjExpand)
    sets[i][index+1][0] += newGrowth
    sets[i][index+1][1] -= newGrowth
    past[i][index+1] += newGrowth
    if(sets[i][index+1][1] < 0):
      sets[i][index+1][0] += sets[i][index+1][1]
      past[i][index+1] -= sets[i][index+1][1]
      sets[i][index+1][1] = 0
    if(index >= cut):
      if(past[i][index-cut] > sets[i][index+1][0]):
        past[i][index-cut] = sets[i][index+1][0]
      sets[i][index+1][0] -= past[i][index-cut]
      sets[i][index+1][2] += past[i][index-cut]
  for one in sets:
    spreadSum += sets[one][index+1][0]
    if(sets[one][index+1][0] > sets[one][index+1][3]):
      spreadSum += 10*(sets[one][index+1][0]-sets[one][index+1][3])
  return spreadSum

impact = 0
impacts = {}
spreadFactor = 0.5
growthFactor = 1.25
meantime = 11
incperiod = 8
strictness = 100, 70
def adj(maxVal):
  def decorator(f):
    def wrapper(*args, **kwargs):
      raw = f(*args, **kwargs)
      if(raw < maxVal):
        return maxVal - raw
      return 0
    return wrapper
  return decorator
compensate = growthFactor*scale + spreadFactor*scale**2
def testAlgs(ban, close):
  i = 0
  impact = 0
  while i < 10:
    j = 0
    while i < 150:
      impacts[str(i)] = impact
      impact = floor(impact + compensate + grow(group, groupKeys, growth, i, spread=lambda a,b:ban(a, b, i-incperiod), expose = lambda a:close(a, i-incperiod), expand = 0.029, cut=meantime+incperiod, spreadFactor=spreadFactor, growthFactor=growthFactor))
      j += 1
  return impact / 10

Leave a Reply