コード例 #1
0
def test_grid_distance():
    reset_all()
    with open('training/' + os.listdir('training/')[56]) as f:
        raw_task = json.load(f)
    task = tuplefy_task(raw_task)
    input_grid = task['train'][0]['input']
    output_grid = task['train'][0]['output']
    same_shape = ((8, 8, 0, 8, 8, 0, 0), (0, 8, 0, 0, 8, 0, 0),
                  (8, 8, 8, 8, 8, 8, 0), (0, 0, 0, 0, 0, 0, 0))
    different_shape = ((8, 8, 0, 8, 8, 0, 0), (0, 8, 0, 0, 8, 0, 0),
                       (8, 8, 8, 8, 8, 8, 0), (0, 8, 0, 0, 0, 0, 0))
    # print(base_entity_finder.grid_distance(output_grid, same_shape), base_entity_finder.grid_distance(output_grid, different_shape))
    assert base_entity_finder.grid_distance(
        output_grid, same_shape) < base_entity_finder.grid_distance(
            output_grid, different_shape)

    color_0 = Property(lambda x: frozenset({0}),
                       np.log(10) - 1,
                       name=f'color {0}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1)
    select_not_0 = Selector.make_property_selector(take_color, color_0, False)
    # min_x = Property(lambda x: x.entity.min_coord(axis=1), np.log(4),
    #                  name='the largest x coordinate',
    #                  output_types=frozenset({'x_coordinate'}),
    #                  entity_finder=base_entity_finder)
    # max_x = Property(lambda x: x.entity.max_coord(axis=1), np.log(4),
    #                  name='the largest x coordinate',
    #                  output_types=frozenset({'x_coordinate'}),
    #                  entity_finder=base_entity_finder)
    # x_length = Property.create_distance_property(max_x, min_x)
    x_length = Property(
        lambda x: x.entity.max_coord(axis=1) - x.entity.min_coord(axis=1) + 1,
        np.log(2),
        name='the x length',
        output_types=frozenset({'x_length'}),
        entity_finder=base_entity_finder)
    zero = Property(lambda x: 0,
                    1,
                    name='0',
                    output_types=frozenset({'y_length'}),
                    entity_finder=base_entity_finder)
    train_input = task['train'][0]['input']
    train_output = task['train'][0]['output']
    entities = base_entity_finder(train_input)
    assert x_length(entities[1], train_input) == 3
    x_length_vect = Property.xy_length_to_vector(zero, x_length)
    # print(select_not_0.select(entities))
    _, prediction = move(select_not_0.select(entities),
                         train_input,
                         x_length_vect,
                         copy=True)

    assert base_entity_finder.grid_distance(train_output, prediction) < \
           base_entity_finder.grid_distance(train_output, train_input)
コード例 #2
0
def test_case_34():
    with open('training/' + os.listdir('training/')[34]) as f:
        raw_task = json.load(f)
    base_entity_finder = EntityFinder(
        lambda grid: find_components(grid, directions=ALL_DIRECTIONS))
    task = tuplefy_task(raw_task)
    inp = task['train'][0]['input']
    out = task['train'][0]['output']
    entities = base_entity_finder(inp)

    color_8 = Property(lambda x: frozenset({8}),
                       np.log(10) - 1,
                       name=f'color {8}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    color_0 = Property(lambda x: frozenset({0}),
                       np.log(10) - 1,
                       name=f'color {0}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1,
                          requires_entity=True)
    select_8 = Selector.make_property_selector(take_color, color_8, True)
    select_not_8 = Selector.make_property_selector(take_color, color_8, False)
    select_not_0 = Selector.make_property_selector(take_color, color_0, False)
    select_not_0.nll = np.log(2)
    select_not_0_nor_8 = Selector.intersect(select_not_0, select_not_8)

    selected_entities = select_not_0_nor_8.select(entities)

    collision = Relation(
        lambda entity1, entity2: next(
            iter(collision_directions(entity1, entity2, adjustment=1)))
        if len(collision_directions(entity1, entity2)) == 1 else None,
        nll=1 + np.log(2),
        name='the unique collision vector to',
        output_types=frozenset({'vector'}))
    collision_with_8 = Property.from_relation_selector(collision, select_8,
                                                       base_entity_finder)
    move_into_8 = Transformer(
        lambda entities, grid: move(entities,
                                    vector_property=collision_with_8,
                                    copy=True,
                                    extend_grid=False),
        nll=collision_with_8.nll + np.log(2),
        name=f"{'copy' if True else 'move'} them by ({collision_with_8})")
    new_entities, new_grid = move_into_8.transform(selected_entities, inp)
    assert new_grid == out
    my_entity_finder = base_entity_finder.compose(select_not_0_nor_8)
    my_predictor = Predictor(my_entity_finder, move_into_8)
    for case in task['train'] + task['test']:
        assert my_predictor.predict(case['input']) == case['output']

    my_predictor_2 = Predictor(base_entity_finder, move_into_8)
コード例 #3
0
def test_composite_selections():
    with open('training/' + os.listdir('training/')[205]) as f:
        raw_cases = json.load(f)
    cases = tuplefy_task(raw_cases)
    color_0 = Property(lambda x: frozenset({0}),
                       np.log(2),
                       name=f'color {0}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    color_5 = Property(lambda x: frozenset({5}),
                       np.log(10) - 1,
                       name=f'color {5}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1)
    select_not_0 = Selector.make_property_selector(take_color, color_0, False)
    select_not_5 = Selector.make_property_selector(take_color, color_5, False)
    select_not_0_nor_5 = select_not_0.intersect(select_not_5)
    entity_finder = base_entity_finder.compose(select_not_0_nor_5, True)
    select_5 = Selector.make_property_selector(take_color, color_5)
    center_y = Property(lambda x: x.entity.center(axis=0),
                        nll=np.log(2),
                        name='the center y coordinate',
                        output_types=frozenset({'y_coordinate'}),
                        entity_finder=base_entity_finder,
                        requires_entity=True)
    center_x = Property(lambda x: x.entity.center(axis=1),
                        nll=np.log(2),
                        name='the center x coordinate',
                        output_types=frozenset({'x_coordinate'}),
                        entity_finder=base_entity_finder,
                        requires_entity=True)
    center_5y = center_y.add_selector(select_5)
    length_5y = Property.create_distance_property(center_5y, center_y)
    center_5x = center_x.add_selector(select_5)
    length_5x = Property.create_distance_property(center_5x, center_x)
    vect_prop = Property.xy_length_to_vector(length_5y, length_5x)
    move_to_5 = Transformer(
        lambda entities, grid, copy=True: move(
            entities, vector_property=vect_prop, copy=copy, extend_grid=False),
        nll=vect_prop.nll + np.log(2),
        name=f"{'copy' if True else 'move'} them by ({vect_prop})")
    my_predictor = Predictor(entity_finder, move_to_5)

    for case in cases['train']:
        assert my_predictor.predict(case['input']) == case['output']
コード例 #4
0
def test_sequential():
    with open('training/' + os.listdir('training/')[56]) as f:
        raw_task = json.load(f)
    task = tuplefy_task(raw_task)
    input_grid = task['train'][0]['input']
    output_grid = task['train'][0]['output']
    color_0 = Property(lambda x: frozenset({0}),
                       np.log(10) - 1,
                       name=f'color {0}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1)
    select_not_0 = Selector.make_property_selector(take_color, color_0, False)
    x_length = Property(
        lambda x: x.entity.max_coord(axis=1) - x.entity.min_coord(axis=1) + 1,
        np.log(2),
        name='the x length',
        output_types=frozenset({'x_length'}),
        entity_finder=base_entity_finder)
    zero = Property(lambda x: 0,
                    1,
                    name='0',
                    output_types=frozenset({'y_length'}),
                    entity_finder=base_entity_finder)
    x_length_vect = Property.xy_length_to_vector(zero, x_length)
    copy_move_x_length = Transformer(
        lambda entities, grid: move(entities, grid, x_length_vect, copy=True),
        name=f'copy them by ({x_length_vect})')
    my_entity_finder = base_entity_finder.compose(select_not_0)
    cropper = Transformer(crop_entities, nll=np.log(2), name='crop them')
    single_predictor = Predictor(my_entity_finder,
                                 copy_move_x_length,
                                 parallel=False)
    predictor_1 = Predictor(my_entity_finder, copy_move_x_length)
    predictor_2 = Predictor(my_entity_finder, cropper)
    sequential_predictor = Predictor([my_entity_finder, my_entity_finder],
                                     [copy_move_x_length, cropper],
                                     parallel=False)
    composed_predictor = predictor_1.compose(predictor_2, parallel=False)
    train_input = task['train'][0]['input']
    train_output = task['train'][0]['output']
    print(composed_predictor)
    assert sequential_predictor.predict(train_input) == train_output
    assert composed_predictor.predict(train_input) == train_output
コード例 #5
0
def test_transformers_predictors():
    with open('training/' + os.listdir('training/')[7]) as f:
        raw_case7 = json.load(f)
    case7 = tuplefy_task(raw_case7)
    inp = case7['train'][0]['input']
    out = case7['train'][0]['output']
    base_entity_finder = EntityFinder(find_components)
    entities = base_entity_finder(inp)
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1)
    color_2 = Property(lambda x, i=2: frozenset({2}),
                       np.log(10) - 2,
                       name=f'color {2}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    color_8 = Property(lambda x, i=8: frozenset({8}),
                       np.log(10) - 2,
                       name=f'color {8}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    select_8 = Selector.make_property_selector(take_color, color_8)
    select_2 = Selector.make_property_selector(take_color, color_2)
    max_ord = OrdinalProperty(lambda x: nth_ordered(x, 0, use_max=True),
                              nll=0,
                              name=f'take the {1} largest')
    find_collision_vect_to_8 = Property.from_relation_selector(
        collision_relation,
        select_8,
        entity_finder=base_entity_finder,
        ordinal_property=max_ord)
    my_transformer = Transformer(
        lambda entities, grid: move(entities,
                                    vector_property=find_collision_vect_to_8),
        name=f'move them by ({find_collision_vect_to_8})',
        nll=1 + np.log(2))

    assert my_transformer.transform(select_2.select(entities))[1] == out

    select_2_finder = base_entity_finder.compose(select_2)
    my_predictor = Predictor(select_2_finder, my_transformer)
    assert my_predictor.predict(inp) == out
コード例 #6
0
def test_case_29():
    with open('training/' + os.listdir('training/')[29]) as f:
        raw_task = json.load(f)
    base_entity_finder = EntityFinder(
        lambda grid: find_components(grid, directions=ALL_DIRECTIONS))
    trivial_selector = Selector(lambda entity, grid: True, name='')
    task = tuplefy_task(raw_task)
    inp = task['train'][0]['input']
    out = task['train'][0]['output']
    # print(task['train'][0]['input'])
    take_color = Property(lambda x: x.entity.colors(),
                          name='the colors',
                          output_types=frozenset({'color'}),
                          entity_finder=base_entity_finder,
                          nll=1,
                          requires_entity=True)
    # color_2 = Property(lambda x, i=2: frozenset({2}), np.log(10) - 2, name=f'color {2}',
    #                    output_types=frozenset({'color'}))
    color_1 = Property(lambda x, i=2: frozenset({1}),
                       np.log(10) - 1,
                       name=f'color {1}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    color_0 = Property(lambda x, i=2: frozenset({0}),
                       np.log(10) - 1,
                       name=f'color {0}',
                       output_types=frozenset({'color'}),
                       entity_finder=base_entity_finder)
    select_1 = Selector.make_property_selector(take_color, color_1)
    property_0 = Property(lambda x, i=0: i,
                          nll=1,
                          name=f'{0}',
                          output_types=frozenset({
                              'x_coordinate', 'y_coordinate', 'x_length',
                              'y_length', 'quantity'
                          }),
                          entity_finder=base_entity_finder)
    select_not_0 = Selector.make_property_selector(take_color,
                                                   color_0,
                                                   the_same=False)
    smallest_y = Property(lambda x: x.entity.max_coord(axis=0),
                          1 + np.log(4),
                          name='the largest y coordinate',
                          output_types=frozenset({'y_coordinate'}),
                          entity_finder=base_entity_finder,
                          requires_entity=True)
    min_y_of_blue = smallest_y.add_selector(select_1)
    distance_to_min_y_of_blue = Property.create_distance_property(
        min_y_of_blue, smallest_y)
    vector_to_min_y_of_blue = Property.xy_length_to_vector(
        distance_to_min_y_of_blue, property_0)
    move_transform = Transformer(
        lambda entities, grid, vector_prop=vector_to_min_y_of_blue: move(
            entities, vector_property=vector_prop),
        nll=vector_to_min_y_of_blue.nll + np.log(2),
        name=f'move them by ({vector_to_min_y_of_blue})')
    my_predictor = Predictor(base_entity_finder.compose(trivial_selector),
                             move_transform)  # .compose(select_not_0)
    # display_case(my_predictor.predict(inp))
    # display_case(out)
    assert my_predictor.predict(inp) == out

    test_input = task['test'][0]['input']
    test_output = task['test'][0]['output']
    test_entities = base_entity_finder(test_input)
    assert len(test_entities) == 4

    selected_finder = base_entity_finder.compose(select_not_0)
    # selected_finder(test_input)
    assert len(selected_finder(test_input)) == 3

    assert my_predictor.predict(test_input) == test_output
コード例 #7
0
def create_predictor_queue(task,
                           max_nll,
                           base_entity_finder,
                           allow_selector_pairs=False):
    for i, example in enumerate(task['train']):
        if len(base_entity_finder(example['input'])) == 0:
            return []
    start_time = time.perf_counter()
    selector_list = list(
        selector_iterator(task,
                          base_entity_finder,
                          max_nll=max_nll - SELECTOR_MAX_NLL_CORRECTION))
    selector_list.sort()
    print(f"selecting time = {time.perf_counter() - start_time}")

    if MAKE_PROPERTY_LIST:
        Property.property_list.sort()
        print(f"len(Property.property_list) = {len(Property.property_list)}")

    print(f'built selector list (1), length={len(selector_list)}')

    if allow_selector_pairs:
        for selector1, selector2 in itertools.combinations(selector_list, 2):
            if combine_pair_selector_nll(
                    selector1,
                    selector2) < max_nll - SELECTOR_MAX_NLL_CORRECTION:
                new_selector = selector1.intersect(selector2)
                if new_selector.validate_and_register(
                        task, base_entity_finder,
                        max_nll - SELECTOR_MAX_NLL_CORRECTION):
                    selector_list.append(new_selector)
        if time.perf_counter() - start_time > MAX_SMALL_TIME:
            print('Out of time')
            return []

    selector_list.sort()
    print(f'built selector list (2), length={len(selector_list)}')
    # print('Time after selectors created = ', time.perf_counter() - start_time)
    # Create distance properties out of coordinate properties
    Property.of_type['x_coordinate'].sort()
    Property.of_type['y_coordinate'].sort()
    # LENGTH PROPERTIES
    x_length_props = (prop1.create_distance_property(
        prop2, register=False) for prop1, prop2 in combine_sorted_queues((
            Property.of_type['x_coordinate'],
            Property.of_type['x_coordinate']), max_nll - np.log(2))
                      if prop1.count != prop2.count and (
                          not prop1.is_constant or not prop2.is_constant))
    y_length_props = (prop1.create_distance_property(
        prop2, register=False) for prop1, prop2 in combine_sorted_queues((
            Property.of_type['y_coordinate'],
            Property.of_type['y_coordinate']), max_nll - np.log(2))
                      if prop1.count != prop2.count and (
                          not prop1.is_constant or not prop2.is_constant))

    length_props = sorted(list(itertools.chain(x_length_props,
                                               y_length_props)))
    for length_prop in length_props:
        length_prop.validate_and_register(
            task,
            extra_validation=lambda output_signature: all(
                (value.is_integer() for value in output_signature)))

    if time.perf_counter() - start_time > MAX_SMALL_TIME:
        print('Out of time')
        return []

    Property.of_type['x_length'].sort()
    Property.of_type['y_length'].sort()

    # Constructing point properties
    point_props = [
        Property.create_point_property(prop1, prop2, register=False)
        for prop1, prop2 in combine_sorted_queues((
            Property.of_type['y_coordinate'],
            Property.of_type['x_coordinate']), max_nll - 2 - POINT_PROP_COST)
    ]
    for point_prop in point_props:
        point_prop.validate_and_register(task)
    Property.of_type['point'].sort()

    if time.perf_counter() - start_time > MAX_SMALL_TIME:
        print('Out of time')
        return []

    # Constructing vector properties

    # Create vectors from single lengths
    for axis, name in enumerate(['y_length', 'x_length']):
        for length in Property.of_type[name]:
            vect_prop = Property.length_to_vector(length, axis, register=False)
            vect_prop.validate_and_register(task)

    # Create vectors from pairs of points
    for source_pt, target_pt in combine_sorted_queues(
        (Property.of_type['point'], Property.of_type['point']),
            max_nll - np.log(2)):
        vect_prop = Property.points_to_vector(source_pt,
                                              target_pt,
                                              register=False)
        vect_prop.validate_and_register(
            task,
            extra_validation=lambda output_signature: all(
                (value[i].is_integer() for value in output_signature
                 for i in range(2))))
        if time.perf_counter() - start_time > MAX_SMALL_TIME:
            print('Out of time')
            return []

    penalize_dim_change = True if all(
        (len(case['input']) == len(case['output'])
         and len(case['input'][0]) == len(case['output'][0])
         for case in task['train'])) else False

    transformers = (
        # 34
        Transformer(
            lambda entities, grid, vector_prop=vector_prop, copy=copy: move(
                entities,
                vector_property=vector_prop,
                copy=copy,
                extend_grid=not penalize_dim_change),
            nll=vector_prop.nll + np.log(2),
            name=f"{'copy' if copy else 'move'} them by ({vector_prop})")
        for vector_prop in Property.of_type['vector']
        for copy in [True, False] if vector_prop.nll + np.log(2) <= max_nll)
    if time.perf_counter() - start_time > MAX_SMALL_TIME:
        print('Out of time')
        return []
    Property.of_type['color'].sort()
    # 35
    composite_transformers = (Transformer(lambda entities, grid, offsets=offsets:
                                          crop_entities(entities, grid, offsets=offsets),
                                          nll=np.log(2) + sum(
                                              (abs(offset) for offset in offsets)) * np.log(2) + \
                                              penalize_dim_change * DIM_CHANGE_PENALTY,
                                          name=f'crop them with offset {offsets}')
                              for offsets in itertools.product([-1, 0, 1], repeat=4)
                              if np.log(2) + sum((abs(offset) for offset in offsets)) * np.log(2) + \
                              penalize_dim_change * DIM_CHANGE_PENALTY < max_nll)
    if any(({entry
             for row in case['input'] for entry in row
             } == {entry
                   for row in case['output'] for entry in row}
            for case in task['train'])):
        new_colors = False