Beispiel #1
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)
Beispiel #2
0
def test_replace_color():
    with open('training/' + os.listdir('training/')[9]) as f:
        raw_task = json.load(f)
    task = tuplefy_task(raw_task)
    input_grid = task['train'][0]['input']
    output_grid = task['train'][0]['output']
    entities = base_entity_finder(input_grid)
    # for selected_entity in entities:
    #     selected_entity.display()
    size_prop = Property(lambda x: x.entity.num_points(),
                         nll=np.log(2),
                         name='the number of points',
                         output_types=frozenset({'quantity'}),
                         entity_finder=base_entity_finder,
                         requires_entity=True)
    minimum = OrdinalProperty(lambda x, n=0: nth_ordered(x, 0, use_max=False),
                              nll=0,
                              name=f'take the {ordinal(0 + 1)} largest',
                              input_types=frozenset({
                                  'x_length', 'y_length', 'x_coordinate',
                                  'y_coordinate', 'quantity'
                              }))
    smallest_size = Property.from_entity_prop_and_ordinal(size_prop, minimum)
    color_4 = Property(lambda x: frozenset({4}),
                       np.log(10) - 2,
                       name=f'color {4}',
                       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_smallest = Selector.make_property_selector(size_prop,
                                                      smallest_size,
                                                      the_same=True)
    selected_entities = select_smallest.select(entities)
    assert len(selected_entities) == 1

    assert selected_entities[0].positions == {(6, 7): 5, (7, 7): 5, (8, 7): 5}

    # smallest_color = take_color.add_selector(select_smallest, ORDINAL_PROPERTIES[0])

    recolor_yellow = Transformer(
        lambda entities, grid, source_color_prop=take_color, target_color_prop=
        color_4: replace_color(entities,
                               source_color_prop=source_color_prop,
                               target_color_prop=target_color_prop),
        nll=take_color.nll + color_4.nll + np.log(2),
        name=f'recolor ({take_color}) with ({color_4})')
    _, prediction_grid = recolor_yellow.transform(selected_entities)

    assert base_entity_finder.grid_distance(
        prediction_grid, output_grid) < base_entity_finder.grid_distance(
            input_grid, output_grid)
Beispiel #3
0
def test_case_30():
    with open('training/' + os.listdir('training/')[30]) 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']
    output = task['train'][0]['output']
    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,
                          requires_entity=True)
    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_not_0 = Selector.make_property_selector(take_color,
                                                   color_0,
                                                   the_same=False)
    crop_transform = Transformer(crop_entities,
                                 nll=np.log(2),
                                 name='crop them')
    _, trivial_transformed_grid = crop_transform.transform(entities)
    assert trivial_transformed_grid == inp

    selected_entities = select_not_0.select(entities)
    _, transformed_grid = crop_transform.transform(selected_entities)
    assert transformed_grid == ((0, 2, 2, 2), (0, 0, 2, 0), (2, 2, 2, 0),
                                (2, 0, 2, 0))

    my_predictor = Predictor(base_entity_finder.compose(select_not_0),
                             crop_transform)

    for case in task['train']:
        assert my_predictor.predict(case['input']) == case['output']

    test_case = task['test'][0]
    print(my_predictor)
    assert my_predictor.predict(test_case['input']) == test_case['output']
Beispiel #4
0
def test_is_rectangle():
    with open('training/' + os.listdir('training/')[28]) as f:
        raw_case = json.load(f)
    case = tuplefy_task(raw_case)
    rectangle = Property(lambda x: x.entity.is_a_rectangle(),
                         0,
                         name='is a rectangle',
                         output_types=frozenset({'bool'}),
                         entity_finder=base_entity_finder)
    color_2 = Property(lambda x: frozenset({2}),
                       np.log(10) - 1,
                       name=f'color {2}',
                       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)
    is_true = Property(lambda x: True,
                       0,
                       name='True',
                       output_types=frozenset({'bool'}),
                       entity_finder=base_entity_finder)
    select_2 = Selector.make_property_selector(rectangle, color_2, True)
    first_case = case['train'][0]['input']
    entities = select_2.select(base_entity_finder(first_case))
    for entity in entities:
        assert rectangle(entity, first_case)

    is_true = Property(lambda x: True,
                       0,
                       name='True',
                       output_types=frozenset({'bool'}),
                       entity_finder=base_entity_finder)
    select_rectangle = Selector.make_property_selector(rectangle, is_true,
                                                       True)
    rect_entities = select_rectangle.select(base_entity_finder(first_case))
    assert len(rect_entities) == 1
    # for entity in rect_entities:
    #     entity.display()
    crop = Transformer(lambda entities, grid, offsets=(1, -1, 1, -1):
                       crop_entities(entities, grid, offsets=offsets),
                       nll=np.log(2) + sum(
                           (abs(offset)
                            for offset in (1, -1, 1, -1))) * np.log(2),
                       name='crop them')
    _, output = crop.transform(rect_entities)
    assert output == case['train'][0]['output']
Beispiel #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