# Problem 1(e): number of states, iterations and total VI time for each grid size
# goal (3,3), lava (3,2), wall (2,2) and start (1,1) stay at the demo defaults
# total time is measured the same way as in the demo (around run_vi_histories),
# setup time is the part spent building the transition table before the first iteration
import time
from common import make_mdp
from simple_rl.planning.ValueIterationClass import ValueIteration

print('size  states  iterations  backups  total time (s)  setup time (s)')
for n in [5, 10, 15, 20, 25]:
    totals = []
    setups = []
    for run in range(5):
        mdp = make_mdp(width=n, height=n, init_loc=(1, 1), goal_locs=[(3, 3)],
                       lava_locs=[(3, 2)], walls=[(2, 2)])
        vi = ValueIteration(mdp, max_iterations=500, delta=0.0001)
        start = time.time()
        vi._compute_matrix_from_trans_func()
        mid = time.time()
        n_iter = vi.run_vi_histories()[0]
        end = time.time()
        totals.append(end - start)
        setups.append(mid - start)
    print(n, ' ', len(vi.get_states()), ' ', n_iter, ' ', vi.bellman_backups, ' ',
          round(sum(totals) / 5, 2), ' ', round(sum(setups) / 5, 2))
