161 lines
4.7 KiB
Python
161 lines
4.7 KiB
Python
import math
|
|
import random
|
|
import matplotlib.pyplot as plt
|
|
import matrixbot
|
|
|
|
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 = [PlayerChain("Manufacturer", 12), Base()]
|
|
#chain = [PlayerChain("me", 12), LessStupidAI(12), LessStupidAI(12), LessStupidAI(12), Base()]
|
|
mb1 = matrixbot.MatrixBot([ 0.29963248, 0.08142691, -0.86641011, 0.23675042, 0.7086182 ,
|
|
-0.06441882, 0.03062067, -1.18299915, 0.68115478, 0.16068485,
|
|
-1.7178326 , -0.29003603, -0.83202861, 1.73222139, -1.98878473,
|
|
-0.96036021, 0.65793096, 1.50552035, -1.18544224, 0.03207729,
|
|
0.78074126, -0.91446605, 2.92469872, 0.48680479, -0.34067442],
|
|
[-0.07150818, -0.10019835, -0.02610447, -0.02049193, 0.42415006], 12)
|
|
mb2 = matrixbot.MatrixBot([ 0.29963248, 0.08142691, -0.86641011, 0.23675042, 0.7086182 ,
|
|
-0.06441882, 0.03062067, -1.18299915, 0.68115478, 0.16068485,
|
|
-1.7178326 , -0.29003603, -0.83202861, 1.73222139, -1.98878473,
|
|
-0.96036021, 0.65793096, 1.50552035, -1.18544224, 0.03207729,
|
|
0.78074126, -0.91446605, 2.92469872, 0.48680479, -0.34067442],
|
|
[-0.07150818, -0.10019835, -0.02610447, -0.02049193, 0.42415006], 12)
|
|
chain = [mb1, PlayerChain("me", 12), mb2, Base()]
|
|
import pdb; pdb.set_trace()
|
|
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()
|