# shared code for Assignment 5 (Problem 1, value iteration)
# make_mdp uses the same settings as generate_MDP in
# simple_rl/examples/value_iteration_update_viz_example.py (step cost 0.1, lava cost 1.0)
from simple_rl.tasks import GridWorldMDP
from simple_rl.planning.ValueIterationClass import ValueIteration


def make_mdp(width=4, height=4, init_loc=(1, 1), goal_locs=[(4, 4)],
             lava_locs=[(4, 3)], walls=[(1, 2)], gamma=0.95, slip=0.05):
    return GridWorldMDP(width=width, height=height, init_loc=init_loc,
                        goal_locs=goal_locs, lava_locs=lava_locs,
                        gamma=gamma, walls=walls, slip_prob=slip,
                        lava_cost=1.0, step_cost=0.1)


def run_vi(mdp, delta=0.0001, max_iter=500):
    # returns the number of iterations, the value of the start state and
    # the list of value functions (one per iteration)
    vi = ValueIteration(mdp, max_iterations=max_iter, delta=delta)
    n_iter, v_start, q_hist, v_hist = vi.run_vi_histories()
    return n_iter, v_start, v_hist
