Package: expected-information-gain
Question Programs & Expected Information GainThis 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.InstallationThis package can be installed using pippip install expected-information-gainBasic UsageThe following example shows how to execute a program on a given board# define a board using BattleshipHypothesisfromeig.battleshipimportShip,BattleshipHypothesis,Parser,Executorships=[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 programquestion=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 providedquestion2=Parser.parse("(and (not (< 4 9)) (== (+ 1 3) 4))")executor2=Executor(question)executor.execute(hypothesis)# FalseThe 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 initialization step, and use this instance every time# Because this step is time consuming, and may take several seconds to finish.fromeig.battleshipimportBattleshipHypothesisSpacehypotheses=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 boardimportnumpyasnpprogram="..."board=np.array([...])# next we can calculate EIG as followsfromeigimportcompute_eig_basicfromeig.battleship.programimportProgramSyntaxErrortry:score=compute_eig_basic(hypotheses,program,board)exceptProgramSyntaxError:# if the program is invalid, a ProgramSyntaxError will be raised# do somethingexceptRuntimeError:# if error happens during execution, a RuntimeError will be raised# do somethingAdvanced UsageWe also provide some advanced interfaces, which are more efficient when the users need to calculate EIG for many
programs on one given board, and they also allows users to incorporate more complicated distributions.# construct the hypothesis spacefromeig.battleshipimportBattleshipHypothesisSpacehypotheses=BattleshipHypothesisSpace(grid_size=6,ship_labels=[1,2,3],ship_sizes=[2,3,4],orientations=['V','H'])# calculate EIG as followsfromeigimportcompute_eig,Bayes,Contextfromeig.battleshipimportParser,Executorfromeig.battleship.programimportProgramSyntaxErrortry:ast=Parser.parse(program)# parse the program into abstract syntax treeexecutor=Executor(ast)# obtain an executor to execute the programprior=EqualSizesDistribution(ship_labels=[1,2,3])# a more cognitive inspired prior distributionbelief=eig.Bayes(hypotheses,prior)# a prior belief given the hypothesis spacecontext=eig.Context(hypotheses,belief)# context stores the posterior beliefcontext.observe(board)# update posterior belief given the boardscore=eig.compute_eig(executor,context)# compute EIG given program and posterior beliefexceptProgramSyntaxError:# if the program is invalid, a ProgramSyntaxError will be raised# do somethingexceptRuntimeError:# if error happens during execution, a RuntimeError will be raised# do something