def load_model(actions, input_dir, circuit, experiment, number):

    path = os.path.join(input_dir, circuit, experiment, number)
    q_table_path_file = os.path.join(path, sorted(os.listdir(path))[0])

    qlearn_file = open(os.path.join(q_table_path_file))
    model = pickle.load(qlearn_file)

    qlearn = QLearn(actions=actions, alpha=0.2, gamma=0.9, epsilon=0.05)
    qlearn.q = model

    print(
        "\n\n---------------- MODEL LOADED ----------------\n {}\n-----------------------\n\n"
        .format(qlearn_file))

    return qlearn
Beispiel #2
0
###########################################

f = open("resources/world.txt", 'r')
lines = f.readlines()
f.close()

height = len(lines)
width = max([len(x) for x in lines])

ai = QLearn(actions=range(cfg.directions),
            alpha=cfg.alpha,
            gamma=cfg.gamma,
            epsilon=cfg.epsilon)
if (os.path.isfile('mouse.pickle')):
    with open('mouse.pickle', 'rb') as p:
        ai.q = pickle.load(p)

pprint(ai.q)
print('Items: ' + str(len(ai.q)))
#exit()

dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
actions = range(cfg.directions)
i = 0
j = 0
for line in lines:
    print("\n", end='')
    line = line.rstrip('\n')
    for i in range(len(line)):
        if (line[i] == 'X'):
            print(line[i], end='')
import unittest
from qlearn import QLearn
from action_state import ActionState
import numpy as np


class QlearnTest(unittest.TestCase):
    def testStateEquality(self):
        ai = QLearn([-1, 0, 1])
        a1 = ActionState(1.0, 1.0, {'vol60': 1})
        a2 = ActionState(1.0, 1.0, {'vol60': 1})
        ai.learn(a1, 1, 1.0, a2)
        self.assertEqual(ai.getQAction(a2), 1)

    #def testQTableLookup(self):
actions = [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -7, -10, -15, -20]
ai = QLearn(actions)
ai.q = np.load('test_q.npy').item()
ai.q
state = ActionState(30, 0.9, {})
ai.q.get((state, -10))
print(ai.getQAction(state))