Parsing, executing, and calculating expected information gain for program-form questions.
Project description
Question Programs & Expected Information Gain
This is a package for parsing/executing questions and calculating Expected Information Gain (EIG) for question programs defined on the Battleship Dataset in the paper "Question Asking as Program Generation".
This package provide a Pure python version (slow) and a Python/C++ hybrid version (fast). Both versions have the same API but different implementations.
Installation
This package can be installed using pip
pip install expected-information-gain
Usage
The following example shows how to execute a program on a given board
# define a board using BattleshipHypothesis
from eig.battleship import Ship, BattleshipHypothesis, Parser, Executor
ships = [Ship(ship_label=1, topleft=(0, 0), size=2, orientation='V'),
Ship(ship_label=2, topleft=(1, 2), size=2, orientation='V')
hypothesis = BattleshipHypothesis(grid_size=3, ships=ships)
# the board looks like this
# B W W
# B W R
# W W R
# parse and execute the program
question = Parser.parse("(bottomright (coloredTiles Red))")
executor = Executor(question)
executor.execute(hypothesis) # (2, 2)
# we can also evaluate general arithmic and logical expressions, with whatever hypothesis provided
question2 = Parser.parse("(and (not (< 4 9)) (== (+ 1 3) 4))")
executor2 = Executor(question)
executor.execute(hypothesis) # False
The next example shows how to calculate Expected Information Gain on a partly revealed board
# first we need to construct a hypothesis space
# We suggest to do this as an initilization step, and use this instance every time
# Because this step is time consuming, and may take several seconds to finish.
from eig.battleship import BattleshipHypothesisSpace
hypotheses = BattleshipHypothesisSpace(grid_size=6, ship_labels=[1, 2, 3],
ship_sizes=[2, 3, 4], orientations=['V', 'H'])
# suppose we have a program and a partly revealed board
import numpy as np
program = "..."
board = np.array([...])
# next we can calculate EIG as follows
from eig import compute_eig, Bayes, Context
from eig.battleship import Parser, Executor
from eig.battleship.program import ProgramSyntaxError
try:
ast = Parser.parse(program) # parse the program into abstract syntax tree
executor = Executor(ast) # obtain an executor to execute the program
belief = eig.Bayes(hypotheses) # a prior belief given the hypothesis space
context = eig.Context(hypotheses, belief) # context stores the posterior belief
context.observe(board) # update posterior belief given the board
score = eig.compute_eig(executor, context) # compute EIG given program and posterior belief
except ProgramSyntaxError: # if the program is invalid, a ProgramSyntaxError will be raised
# do something
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for expected-information-gain-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13db70aa3141419386a380d070d8bd76e4f44d42a81a3efa68a9d0952902ba9c |
|
MD5 | 151151a7ac77f7196278f0c057dfb308 |
|
BLAKE2b-256 | 833b970a6f2b2185b310794de4db2d7b41dfd732802d2771a1b00e0f7f01586b |
Hashes for expected_information_gain-1.0.2-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ff17322ee2ddca27cfea06c73b9e223528850c48f10c14124e18e5cd54e1d61 |
|
MD5 | d38b115f2c1dc9c512ae5aa9fb3f296c |
|
BLAKE2b-256 | 92e5604ffa7c8292dab9e3459074fb3ba89bc1e0cf09e9d2dbe82fa1bb241fd9 |