carpedm20/karel

Karel dataset for program synthesis and program induction

Python

79

3 commits

updated Dec 24, 2017

See the code

README

Karel Dataset

This is the code used to generate the Karel dataset as described in following papers:

karel

Introduction

Karel is an educational programming languag. The Domain-specific language for Karel programs is:

karel

but I added {, } and removed : to explicitly represent the scope without tabspace.

The state of the grid world are represented as a H x W x 16 tensor. Each cell of grid is the 16-dimensional vector indicated as:

IndexDescription
0Hero facing North
1Hero facing South
2Hero facing West
3Hero facing East
4Wall or not
50 marker
61 marker
72 marker
83 marker
94 marker
105 marker
116 marker
127 marker
138 marker
149 marker
1510 marker

Usage

0. Installation

To install karel package:

$ pip install karel

1. Data generation

There are two types of parser which can be defined with --parser_type:

  1. curly: Karel + curly braces ({, })
  2. synthesis: Karel for program synthesis (examples can be found here)

To generate programs and its input/output examples:

$ python generate.py --data_dir=data --max_depth=5 --parser_type=synthesis

which will generate data/train.npz, data/test.npz and data/val.npz and you can use these data with:

data = np.load(npz_path)
for input, output, code in zip(data['inputs'], data[outputs'], data['codes']):
    train(input, output, code)

To generate programs only as text:

$ python generate.py --mode=text --beautify=True --parser_type=curly

and it will generate data/train.txt, data/test.txt and data/val.txt that contain codes like:

def run() {
  while(right_is_clear()) {
    repeat(7) {
      put_marker()
    }
  }
}
def run() {
  while(left_is_clear()) {
    repeat(13) {
      pick_marker()
    }
  }

  while(no_markers_present()) {
    ifelse(right_is_clear()) {
      put_marker()
    }
    else {
      pick_marker()
    }
  }
}

2. Interpreter

To run Karel interpreter (with random grid world):

$ python -m karel.parser_with_curly
In [1]: def run() {
   ...:   repeat(7) {
   ...:     ifelse(front_is_clear()) {
   ...:       move()
   ...:     }
   ...:     else {
   ...:       turn_right()
   ...:     }
   ...:   }
   ...: }
Input:  ########
        #.1.1#.#
        ##..1..#
        #.11.1##
        #1>1..1#
        ##....1#
        #.#....#
        ########
Output: ########
        #.1.1#.#
        ##..1..#
        #.11.1##
        #1.1..1#
        ##....1#
        #.#...v#
        ########
In [2]:

or,

$ python -m karel.parser_for_synthesis
In [1]: DEF run m( IF c( frontIsClear c) i( turnRight move i) m)
Input:  ########
        #....#.#
        #......#
        #.oo..##
        #o>....#
        #.....o#
        #......#
        ########
Output: ########
        #....#.#
        #......#
        #.oo..##
        #o.....#
        #.v...o#
        #......#
        ########
In [2]:

To run Karel interpreter with a world file (ex. assets/simple.world):

$ python -m karel.parser_with_curly --world=assets/simple.world
In [1]: def run() {
   ...:   repeat(7) {
   ...:     ifelse(front_is_clear()) {
   ...:       put_marker();
   ...:       move()
   ...:     }
   ...:     else {
   ...:       turn_right()
   ...:     }
   ...:   }
   ...: }
Input:  ########
        #....#.#
        ##..o..#
        #.....##
        #.>....#
        ##.....#
        #.#....#
        ########
Output: ########
        #....#.#
        ##..o..#
        #.....##
        #.ooooo#
        ##....o#
        #.#...v#
        ########
In [2]:

Author

Taehoon Kim / @carpedm20

Contributors

carpedm20

3 commits

carpedm20/karel

Karel dataset for program synthesis and program induction

Python

79

3 commits

updated Dec 24, 2017

See the code

README

Karel Dataset

This is the code used to generate the Karel dataset as described in following papers:

karel

Introduction

Karel is an educational programming languag. The Domain-specific language for Karel programs is:

karel

but I added {, } and removed : to explicitly represent the scope without tabspace.

The state of the grid world are represented as a H x W x 16 tensor. Each cell of grid is the 16-dimensional vector indicated as:

IndexDescription
0Hero facing North
1Hero facing South
2Hero facing West
3Hero facing East
4Wall or not
50 marker
61 marker
72 marker
83 marker
94 marker
105 marker
116 marker
127 marker
138 marker
149 marker
1510 marker

Usage

0. Installation

To install karel package:

$ pip install karel

1. Data generation

There are two types of parser which can be defined with --parser_type:

  1. curly: Karel + curly braces ({, })
  2. synthesis: Karel for program synthesis (examples can be found here)

To generate programs and its input/output examples:

$ python generate.py --data_dir=data --max_depth=5 --parser_type=synthesis

which will generate data/train.npz, data/test.npz and data/val.npz and you can use these data with:

data = np.load(npz_path)
for input, output, code in zip(data['inputs'], data[outputs'], data['codes']):
    train(input, output, code)

To generate programs only as text:

$ python generate.py --mode=text --beautify=True --parser_type=curly

and it will generate data/train.txt, data/test.txt and data/val.txt that contain codes like:

def run() {
  while(right_is_clear()) {
    repeat(7) {
      put_marker()
    }
  }
}
def run() {
  while(left_is_clear()) {
    repeat(13) {
      pick_marker()
    }
  }

  while(no_markers_present()) {
    ifelse(right_is_clear()) {
      put_marker()
    }
    else {
      pick_marker()
    }
  }
}

2. Interpreter

To run Karel interpreter (with random grid world):

$ python -m karel.parser_with_curly
In [1]: def run() {
   ...:   repeat(7) {
   ...:     ifelse(front_is_clear()) {
   ...:       move()
   ...:     }
   ...:     else {
   ...:       turn_right()
   ...:     }
   ...:   }
   ...: }
Input:  ########
        #.1.1#.#
        ##..1..#
        #.11.1##
        #1>1..1#
        ##....1#
        #.#....#
        ########
Output: ########
        #.1.1#.#
        ##..1..#
        #.11.1##
        #1.1..1#
        ##....1#
        #.#...v#
        ########
In [2]:

or,

$ python -m karel.parser_for_synthesis
In [1]: DEF run m( IF c( frontIsClear c) i( turnRight move i) m)
Input:  ########
        #....#.#
        #......#
        #.oo..##
        #o>....#
        #.....o#
        #......#
        ########
Output: ########
        #....#.#
        #......#
        #.oo..##
        #o.....#
        #.v...o#
        #......#
        ########
In [2]:

To run Karel interpreter with a world file (ex. assets/simple.world):

$ python -m karel.parser_with_curly --world=assets/simple.world
In [1]: def run() {
   ...:   repeat(7) {
   ...:     ifelse(front_is_clear()) {
   ...:       put_marker();
   ...:       move()
   ...:     }
   ...:     else {
   ...:       turn_right()
   ...:     }
   ...:   }
   ...: }
Input:  ########
        #....#.#
        ##..o..#
        #.....##
        #.>....#
        ##.....#
        #.#....#
        ########
Output: ########
        #....#.#
        ##..o..#
        #.....##
        #.ooooo#
        ##....o#
        #.#...v#
        ########
In [2]:

Author

Taehoon Kim / @carpedm20

Contributors

carpedm20

3 commits

Languages

Python

100.0%