def test_place_shape(): with open('training/' + os.listdir('training/')[94]) 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) appearing_shapes = Counter() for grid in task['train']: output_entities = base_entity_finder(grid['output']) appearing_shapes += Entity.shapes(output_entities) desired_shape = frozenset({((0.0, 1.0), 1), ((1.0, 0.0), 1), ((-1.0, 1.0), 1), ((1.0, 1.0), 1), ((1.0, -1.0), 1), ((0.0, -1.0), 1), ((-1.0, -1.0), 1), ((-1.0, 0.0), 1)}) assert desired_shape in appearing_shapes 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) center_0 = Property(lambda x: x.entity.center(axis=0), nll=1 + np.log(2), name='the center y coordinate', output_types=frozenset({'y_coordinate'}), entity_finder=base_entity_finder) center_1 = Property(lambda x: x.entity.center(axis=1), nll=1 + np.log(2), name='the center x coordinate', output_types=frozenset({'x_coordinate'}), entity_finder=base_entity_finder) center = Property.create_point_property(center_0, center_1) desired_shape_prop = Property(lambda x: desired_shape, np.log(10) - 1, name=f'shape {desired_shape}', output_types=frozenset({'shape'}), is_constant=True, entity_finder=base_entity_finder) # shape_entity_prop = Property(lambda x: x.entity.shape(), 1, name=f'the shape', # output_types=frozenset({'shape'}), # entity_finder=base_entity_finder) place_desired_shape = Transformer( lambda entities, grid: place_shape( entities, point_prop=center, shape_prop=desired_shape_prop), nll=center.nll + desired_shape_prop.nll + np.log(2), name=f'place ({desired_shape_prop}) at position ({center}))') select_5 = Selector.make_property_selector(take_color, color_5) find_entities_5 = base_entity_finder.compose(select_5) my_predictor = Predictor(find_entities_5, place_desired_shape) assert my_predictor.predict(input_grid) == output_grid with open('training/' + os.listdir('training/')[14]) as f: raw_task14 = json.load(f) task14 = tuplefy_task(raw_task14) input_grid14 = task14['train'][0]['input'] output_grid14 = task14['train'][0]['output'] color_1 = Property(lambda x: frozenset({1}), np.log(10) - 1, name=f'color {1}', output_types=frozenset({'color'}), entity_finder=base_entity_finder) select_1 = Selector.make_property_selector(take_color, color_1) # print(input_grid14) diamond = frozenset({((1.0, 0.0), 7), ((-1.0, 0.0), 7), ((0.0, 1.0), 7), ((0.0, -1.0), 7)}) diamond_prop = Property(lambda x: diamond, np.log(10) - 1, name=f'shape {diamond}', output_types=frozenset({'shape'}), is_constant=True, entity_finder=base_entity_finder) place_diamond = Transformer( lambda entities, grid: place_shape(entities, grid, center, diamond_prop ), name=f'place ({diamond_prop}) at position ({center})') diamond_predictor = Predictor(base_entity_finder.compose(select_1), place_diamond) print(diamond_predictor) for case in task14['train']: # print(case['input']) output_grid = diamond_predictor.predict(case['input']) assert (base_entity_finder.grid_distance( case['output'], diamond_predictor.predict(case['input'])) + base_entity_finder.grid_distance( diamond_predictor.predict(case['input']), case['input']) <= base_entity_finder.grid_distance(case['output'], case['input']))
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