Skip to content

Instantly share code, notes, and snippets.

@jkilpatr
Last active November 12, 2017 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkilpatr/ec142833a0c2f09de01b37a83a5f78d2 to your computer and use it in GitHub Desktop.
Save jkilpatr/ec142833a0c2f09de01b37a83a5f78d2 to your computer and use it in GitHub Desktop.
# A earning simulator for pay per forward mesh
# This is essentially a scratchpad for trying out ideas, so it's hardly polished.
import numpy
from scipy.optimize import minimize
from scipy.optimize import brute
from scipy.optimize import basinhopping
from scipy.optimize import differential_evolution
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
from Tkinter import *
# probs are taken as normal distributions
uptake_prob = .20 # percent chance someone with the opportunity
# to adopt the system and incentive to do so (cheaper than cutoff) will
cutoff_price = -60 # bucks per month
peers_prob = 3
hop_cost_prob = .02 # cents per gig
base_cost = .08 # cents per gig, wisp cut
scale = 1
usage_prob = 300 # gigs
def compute_earnings(
uptake_prob,
cutoff_price,
hop_cost_prob,
base_cost,
no_hops):
if numpy.random.uniform() >= uptake_prob and no_hops > 1:
return 0
elif no_hops > 20: #even running cat6 20 hops deep is too many
return 0
peers = numpy.random.normal(loc=peers_prob, scale=scale)
earnings = 0
my_price = abs(numpy.random.normal(loc=hop_cost_prob, scale=.01))
usage = 0
for n in range(int(peers)):
peer_usage = compute_earnings(
uptake_prob,
cutoff_price,
hop_cost_prob,
base_cost + my_price,
no_hops + 1)
usage = usage + peer_usage
my_usage = abs(numpy.random.normal(loc=usage_prob, scale=200))
my_costs = (my_usage * base_cost)
earnings = (usage * my_price)
if earnings - my_costs < cutoff_price:
#print(
# "{} hops deep base price {} causes price exit".format(
# no_hops, base_cost))
return 0
else:
#print(
# "{} hops deep with {} neighs I charge {} and pay {} and used {} and passed {} and I earned {}".format(
# no_hops,
# int(peers),
# my_price,
# base_cost,
# my_usage,
# usage,
# earnings -
# my_costs))
return my_usage + usage
def compute_earnings_opt_wrapper(x):
hop_cost_prob = x[0]
base_cost = x[1]
usage_prob = 190
uptake_prob = .1
cutoff_price = -60 # bucks per month
peers_prob = 3
avg = 0
samples = 100
for n in range(samples):
avg = avg + compute_earnings(
uptake_prob,
cutoff_price,
hop_cost_prob,
base_cost,
1) * -1 * base_cost
return (avg/samples)
@numpy.vectorize
def compute_earnings_vec_wrapper(uptake_prob, peers_probability):
hop_cost_prob = .06
base_cost = .06
usage_prob = 190
#uptake_prob = .2
cutoff_price = -160 # bucks per month
#peers_prob = 5
avg = 0
samples = 100
for n in range(samples):
avg = avg + compute_earnings(
uptake_prob,
cutoff_price,
hop_cost_prob,
base_cost,
1) * base_cost
return (avg/samples)
#print brute(compute_earnings_opt_wrapper, ((0,1),(0,1),(0,10000)))
#total_usage = compute_earnings(
# uptake_prob,
# cutoff_price,
# hop_cost_prob,
# base_cost,
# 1)
#print("Infra operator earns {} and passes {}gb".format(
# (base_cost * total_usage), total_usage))
x = arange(0,.7,0.1)
y = arange(0.0,10,1)
X, Y = meshgrid(x,y)
Z = compute_earnings_vec_wrapper(X, Y)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap=cm.RdBu,linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.set_zlabel("Uplink profit")
ax.set_xlabel("Uptake rate")
ax.set_ylabel("Peers")
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment