Initial supply app
This commit is contained in:
commit
7c429fb0eb
3 changed files with 290 additions and 0 deletions
146
supply.py
Normal file
146
supply.py
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import math
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
BACKLOG_COST = 25
|
||||
SUPPLY_COST = 5
|
||||
|
||||
class Base:
|
||||
w1 = 0
|
||||
w2 = 0
|
||||
def request(self):
|
||||
return 0
|
||||
|
||||
def reconcile(self, req, got):
|
||||
a = self.w2
|
||||
self.w2 = self.w1
|
||||
self.w1 = req
|
||||
return a
|
||||
|
||||
def cost(self):
|
||||
return (0, 0)
|
||||
|
||||
class Chain:
|
||||
supply = 0
|
||||
backlog = 0
|
||||
last_req = 0
|
||||
last_got = 0
|
||||
last_ship = 0
|
||||
|
||||
def __init__(self, initial):
|
||||
self.supply = initial
|
||||
|
||||
def reconcile(self, req, got):
|
||||
self.last_req = req
|
||||
self.last_got = got
|
||||
self.supply += got
|
||||
self.backlog += req
|
||||
out = 0
|
||||
if self.backlog > self.supply:
|
||||
out = self.supply
|
||||
self.backlog = self.backlog - self.supply
|
||||
self.supply = 0
|
||||
else:
|
||||
out = self.backlog
|
||||
self.supply = self.supply - self.backlog
|
||||
self.backlog = 0
|
||||
self.last_ship = out
|
||||
return out
|
||||
|
||||
def cost(self):
|
||||
return (self.supply, self.backlog)
|
||||
|
||||
class PlayerChain(Chain):
|
||||
inp = 0
|
||||
name = "Player"
|
||||
def __init__(self, name, initial):
|
||||
self.name = name
|
||||
super().__init__(initial)
|
||||
|
||||
def request(self):
|
||||
print(self.name)
|
||||
print("Supply: ", self.supply, " | Backlog: ", self.backlog)
|
||||
print("Last Requested ", self.last_req, " | Last Recieved: ", self.last_got, " | Last Shipped: ", self.last_ship)
|
||||
x = input("How many to request [%d]? " % self.inp)
|
||||
if x != "":
|
||||
self.inp = int(x)
|
||||
return self.inp
|
||||
|
||||
class StupidAI(Chain):
|
||||
def request(self):
|
||||
if self.supply > 0:
|
||||
# if self.supply < (self.last_req / 2):
|
||||
# return self.last_req
|
||||
return 0
|
||||
if self.backlog > 0:
|
||||
return self.backlog + self.last_req
|
||||
return self.last_req
|
||||
|
||||
class LessStupidAI(Chain):
|
||||
def request(self):
|
||||
return self.last_req
|
||||
|
||||
def variableRequestGenerator(turns, low, high):
|
||||
for i in range(turns):
|
||||
x = random.randint(low, high)
|
||||
yield x
|
||||
|
||||
def simpleRequestGenerator(turns, init=5):
|
||||
x = init
|
||||
for i in range(turns):
|
||||
if i >= x:
|
||||
yield 8
|
||||
else:
|
||||
yield 4
|
||||
|
||||
TURNS = 50
|
||||
|
||||
def main():
|
||||
global TURNS
|
||||
global SUPPLY_COST
|
||||
global BACKLOG_COST
|
||||
g = variableRequestGenerator(TURNS, 8, 14)
|
||||
#chain = [StupidAI(12), PlayerChain("Manufacturer", 12), Base()]
|
||||
chain = [PlayerChain("me", 12), LessStupidAI(12), LessStupidAI(12), LessStupidAI(12), Base()]
|
||||
turn = 0
|
||||
total_sold = 0
|
||||
history = []
|
||||
for topreq in g:
|
||||
turn += 1
|
||||
print("\nTurn ", turn)
|
||||
requests = [x.request() for x in chain]
|
||||
requests = requests[:-1]
|
||||
requests.insert(0, topreq)
|
||||
got = 0
|
||||
for i, c in reversed(list(enumerate(chain))):
|
||||
got = c.reconcile(requests[i], got)
|
||||
print("Sold ", got, " units.")
|
||||
total_sold += got
|
||||
history.append([x.cost() for x in chain])
|
||||
print("**********")
|
||||
print("Sold a total of %d units." % total_sold)
|
||||
print("Front player was short %d turns." % len([1 for x in history if x[0][1] > 0]))
|
||||
plot_cost(history)
|
||||
|
||||
def plot_cost(history):
|
||||
total_cost = [sum([x[0] * SUPPLY_COST + x[1] * BACKLOG_COST for x in y]) for y in history]
|
||||
print("Final cost: %d" % integrate(total_cost)[-1])
|
||||
plt.figure(1)
|
||||
plt.subplot(211)
|
||||
for i in range(len(history[0]) - 1):
|
||||
plt.plot([y[0] - y[1] for y in [x[i] for x in history]])
|
||||
plt.axhline(0, color='black')
|
||||
plt.subplot(212)
|
||||
plt.plot(integrate(total_cost))
|
||||
plt.show()
|
||||
|
||||
def integrate(l):
|
||||
x = 0
|
||||
out = []
|
||||
for i in l:
|
||||
x += i
|
||||
out.append(x)
|
||||
return out
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue