Skip to content

Instantly share code, notes, and snippets.

@duartenina
Created January 17, 2016 20:23
Show Gist options
  • Save duartenina/75a51eb60b32ebe362b8 to your computer and use it in GitHub Desktop.
Save duartenina/75a51eb60b32ebe362b8 to your computer and use it in GitHub Desktop.
ChimeClicker
# coding: utf-8
# In[107]:
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
# In[108]:
n_max = 10**1
cost_0 = 100
# In[109]:
def cost_from_level(level, cost_0, previous = None):
if level == 0:
return cost_0
if previous:
return previous + 0.1 * cost_0 * level
return cost_from_level(level-1, cost_0) + 0.1 * cost_0 * level
def cost_stable(level, cost_0):
return cost_0 * (1 + 0.05 * level*(level-1))
# In[110]:
levels = np.linspace(1, n_max, n_max, dtype = 'i')
c1 = [cost_from_level(0, cost_0, 0)]
for i in range(1, n_max):
c1.append(cost_from_level(i, cost_0, c1[-1]))
c2 = [cost_stable(i, cost_0) for i in levels]
diff = [c2[i-1] - c1[i-1] for i in levels]
plt.figure(figsize=(16,6))
plt.plot(levels, c1, "b-")
plt.plot(levels, c2, "r*")
plt.plot(levels, diff, "r-")
plt.show()
print(max(diff))
# In[111]:
def cum_cost_stable(level_to, level_from, cost_0):
n = level_from
m = level_to
return cost_0 * (m - n + 1 + 1/60 * (m*(m+1)*(m+2) - n*n*n + n))
# In[112]:
levels = np.linspace(1, n_max, n_max, dtype = 'i')
cc1 = [cost_0]
for i in range(1, n_max):
cc1.append(cc1[-1] + c1[i])
cc2 = [cum_cost_stable(i, 0, cost_0) for i in range(n_max)]
diff = [i - j for (i,j) in zip(cc1,cc2)]
plt.figure(figsize=(16,6))
plt.plot(levels, cc1, "b-")
plt.plot(levels, cc2, "r*")
plt.plot(levels, diff, "r-")
plt.show()
print(max(diff))
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment