In [ ]:
import json
import numpy as np
import pprint
In [ ]:
# load the data
with open('output2.log') as f:
data = [line[8:-1] for line in f.read().split('\n')]
transport = np.array([[int(elem) for elem in json.loads(row)] for row in data[:3]])
initial_east = np.array([10, 10, 12])
initial_west = np.array([0, 0, 0])
capacity = 5
value = np.array([[5], [4], [6]])
trips = int(data[3])
profit = int(data[4])
transport, trips, profit
In [ ]:
# calculate east and west states over time
east_state = np.zeros((3, trips + 1), dtype=np.int)
west_state = np.zeros((3, trips + 1), dtype=np.int)
east_state[:, 0] = initial_east
def leave_alone(state, t):
# calculate the results of the fighting...
foxes, geese, corn = state[:, t]
if foxes > 0 and geese > 0:
if foxes > geese:
print('Foxes fight over geese! A fox died today...')
foxes -= 1
else:
print('Foxes enjoy eating geese! {} geese are eaten'.format(foxes))
geese -= foxes
# have not needed to check the geese vs corn case yet...
elif foxes > 0 and corn > 0:
print('A fox eats corn, and dies...')
foxes -= 1
corn -= 1
else:
assert (not foxes and not geese) or (not foxes and not corn) or (not geese and not corn)
print('All is calm')
state[:, t+1] = foxes, geese, corn
print('{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn remain'.format(state[:, t+1]))
# no asserts on capacity or supply
for t in range(0, trips):
print('\n\n**** Trip {} ****'.format(t+1))
if t % 2 == 0:
east_state[:, t+1] = east_state[:, t] - transport[:, t]
west_state[:, t+1] = west_state[:, t]
print('{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn alone on the west...'.format(west_state[:, t]))
leave_alone(west_state, t)
west_state[:, t+1] += transport[:, t]
print('\n{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn taken from east to west'.format(transport[:, t]))
else:
east_state[:, t+1] = east_state[:, t]
print('{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn alone on the east...'.format(east_state[:, t]))
leave_alone(east_state, t)
east_state[:, t+1] += transport[:, t]
west_state[:, t+1] = west_state[:, t] - transport[:, t]
print('\n{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn taken from west to east'.format(transport[:, t]))
print('{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn on the east'.format(east_state[:, t+1]))
print('{0[0]} Foxes, {0[1]} Geese, {0[2]} Corn on the west'.format(west_state[:, t+1]))
In [ ]:
pprint.pprint(east_state)
pprint.pprint(west_state)