Skip to main content

A package to learn specification and synthesize strategy from demonstrations

Project description

[WIP] specless (SPECification LEarning and Strategy Synthesis)

Documentation Status Test Coverage PyPI Latest Release PyPI Downloads License

Installation

  • from PyPI
pip install specless
  • from source
pip install git@github.com:watakandai/specless.git
  • or clone and install.
git clone https://github.com/watakandai/specless.git
cd specless
pip install .

Quickstart

You can use the specless package in two ways: as a library, and as a CLI tool.

To infer a specification from demonstrations,

Parse a demonstration file:

import specless as sl  # or load from specless.inference import TPOInference
import pandas as pd

# Manually prepare a list of demonstrations
demonstrations = [
    ["e1", "e2", "e3", "e4", "e5"],             # trace 1
    ["e1", "e4", "e2", "e3", "e5"],             # trace 2
    ["e1", "e2", "e4", "e3", "e5"],             # trace 3
]
dataset = sl.ArrayDataset(demnstrations, columns=["symbol"])
# or load from a file
csv_filename = "./examples/readme/example.csv"
dataset = sl.BaseDataset(pd.read_csv(csv_filename))

# Run the inference
inference = sl.TPOInferenceAlgorithm()
specification = inference.infer(dataset)            # returns a Specification

# Note: Not yet supported
print(specification)                                # prints the specification
sl.save_graph(specification, filenpath='spec')       # exports the specification to a file
sl.draw_graph(specification, png_filepath='spec')    # drawws the specification to a file

Demonstrations can be obtained by simulating runs in an environment.

The environment is based on the OpenAI Gym library (or more specifically, PettingZoo)

import gymnasium as gym
import gym_minigrid # To load minigrid envs
from specless.gym.utils import collect_demonstration

#
env = gym.make("MiniGrid-Empty-5x5-v0")
# Collect Demonstrations
demonstrations = [collect_demonstration(env) for i in range(10)]
# Convert them to a Dataset Class
demonstrations = sl.ArrayDataset(demonstrations, columns=["s1", "s2", ...]) # state labels
  • Once the specification is obtained, synthesize a strategy:
import gymnasium as gym
import gym_minigrid # To load minigrid envs (e.g., MiniGrid-Empty-5x5-v0)
import specless as sl
# or from specless.specparser import LTLfParser
# or from specless.synthesis import TSPSynthesis

env = gym.make("MiniGrid-Empty-5x5-v0")
# TODO: LTLf must be installed. Need a little bit of work.
specparser = sl.LTLfParser(engine='ltlf2dfa')             # Choose an engine
specification = specparser.parse("G(a -> X b)")            # Translate a LTLf formula to specification class
synthesizer = sl.TSPSynthesisAlgorithm()                  # Set parameters at initialization
strategy = synthesizer.synthesize(specification, env)      # Run the synthesis Algorithm

print(strategy)
sl.save_graph(strategy, path='./strategy')

You can use the strategy in an env like

state, info = env.reset()
terminated, truncated = False, False
while not (terminated or truncated):
    action = strategy.action(state) # Stategies make a decision given an observed state

    (next_state,
     reward,
     terminated,
     truncated,
     info) = env.step(action)       # PlanStrategy class is ******a** feedforward strategy.
                                    # It precomputs a plan at each **step** and does not
                                    # depend on the observed state.
    state = next_state
env.close()

[Not yet Supported] As a CLI Interface

With the click package, we exposed some functions as a command line tool.

demo2spec -f <path/to/file>
synthesize -d <path/to/demo> OR -s <LTLf formula> AND -e <Gym env> AND -p <path/to/param>

Development

If you want to contribute, set up your development environment as follows:

  • Install Poetry
  • Clone the repository: git clone https://github.com/watakandai/specless.git && cd specless
  • Install the dependencies: poetry shell && poetry install

Tests

To run tests: tox

To run only the code tests: tox

Docs

Locally, run make html inside the docs directory.

Once you are ready, make a pull request and the documentations are built automatically with GitHub Actions. See .github/generate-documentation.yml.

License

Apache 2.0 License

Copyright 2023- KandaiWatanabe

WIP

TODO:

  1. Create a wrapper (LabelMinigridWrapper) that labels an observed state. It must:
    1. accept any labeling function given by the user
    2. skip/ignore some states that are unnecessary (e.g., empty labels). This can be specified by the user by providing a list of labels that must be ignored or by providing a function that checks for unnecessary labels.
  2. Update the MiniGridTransitionSystemWrapper with LabelMiniGridWrapper so that the step function is updated to return observations with the augmented information (e.g. labels).
  3. Update the TransitionSystemBuilder with the new MiniGridTransitionSystemWrapper so that the transition system only includes desired labels!!!
  4. Collect a demonstration (trace) from the TransitionSystem
  5. Collect a timed trace from the TransitionSystem
  6. Update

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

specless-0.0.2.tar.gz (98.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

specless-0.0.2-py3-none-any.whl (119.8 kB view details)

Uploaded Python 3

File details

Details for the file specless-0.0.2.tar.gz.

File metadata

  • Download URL: specless-0.0.2.tar.gz
  • Upload date:
  • Size: 98.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for specless-0.0.2.tar.gz
Algorithm Hash digest
SHA256 818f8e220c5b7d2195329de023fbdd95835e574473acf72d857eeba60b9b8a55
MD5 45d66db064cd946daa67bd5a51d15694
BLAKE2b-256 42cb9a6e70061ef405551def08cbf6c711e784527c1ecccc7bea463e763220ad

See more details on using hashes here.

File details

Details for the file specless-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: specless-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 119.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for specless-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 262ad85c4a1cd1ebc105898da934a4b85a580add03c564392a0780fd10c61cb0
MD5 a28f86d37fb9ac0b1e1e27ca852a5a4e
BLAKE2b-256 ca9027c582105a779e88d3097089592f91f047e3111345fbe959b53f4f287a4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page