/adeptly

Python 3.6+ library for universal adaptive intelligent agents based on Deep-Q Neural Networks

Primary LanguagePythonMIT LicenseMIT

Build Status Azure DevOps tests (branch) Azure DevOps coverage (branch) Code style: black PyPI

Adeptly

Python 3.6+ library for adaptive intelligent agents in real-time environments (e.g. games) based on a combination of rule-based policies and Deep Q Neural Networks.

Current State

Right now this is little more than a basic DQN implementation. The vision is for this to become a library dedicated to reinforcement learning agents that can learn as the act in an environment (which is my definition of adaptive here). In the next step this will then be integrated with decision and behaviour trees as well as finite state machines in way that q-learning agents become nodes in a behaviour or decision tree.

Usage

from adeptly import AdeptlyEngine, DQNAgent

actions = ['Foo', 'Bar']

bob = DQNAgent(1, 2, [0.5, 0.5])

with AdeptlyEngine():
    observation = None
    action_index = None
    reward = None
    for i in range(1000):
        next_observation = i % 10
        if i > 0:
            bob.remember(observation, action_index, reward, next_observation, True if i == 999 else False)
        observation = next_observation
        action_index = bob.predict_best_action(observation)
        # Train Bob to always Foo, expect if 9 or 10 is observed, then Bar is better.
        reward = 0 if actions[index] == 'Foo' else 1
        if observation > 8 and actions[index] == 'Foo':
            reward = -1
        # Let Bob learn from the past at every 100 steps.
        if i % 100 == 0 and i != 0:
            bob.replay(100)

The above example is trivial, of course, as the environment an the agent are actually decoupled, but it suffices to illustrate the basic usage of the DQN interface.