# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + pycharm={"name": "#%%\n"}
import matplotlib.pyplot as plt
import numpy as np

from myelin.dp import ValueIteration, ValueIterationCallback
from myelin.mdps import GridWorld


class Monitor(ValueIterationCallback):

    @staticmethod
    def display_value_function(vf):
        np.set_printoptions(precision=4, linewidth=200)
        plt.pcolormesh(vf)
        plt.colorbar()
        plt.show()

    def on_learning_begin(self, data):
        print('Value iteration started...')
        Monitor.display_value_function(data['vf'])

    def on_learning_end(self, data):
        print('Value iteration finished!')

    def on_iteration_end(self, data):
        print('Iteration %d ended' % data['n_iter'])
        Monitor.display_value_function(data['vf'])


ROWS, COLS = 20, 20
value_iteration = ValueIteration(
    mdp=GridWorld(ROWS, COLS),
    theta=0.001,
    gamma=0.99,
    vfunction=np.zeros((ROWS, COLS)),
    callbacks=[Monitor()]
)
value_iteration.learn()

StarcoderdataJupyterScriptsDedupFiltered