<jupyter_start><jupyter_text>How do you get LOTlib to start with a hypothesis and expand from there? This is awkward right now -- you have to add the input as a rule...It also doesn't really understand how to compile lists of lists, just numbers.. I think we'd need to change the backend a bit<jupyter_code># intializing basic hypothesis, will edit later
class ARCHypothesis(LOTHypothesis):
    def __init__(self, **kwargs):
        LOTHypothesis.__init__(self, grammar=grammar, display="lambda grid: %s", **kwargs)
        
    def __call__(self, *args):
        try:
            # try to do it from the superclass
            return LOTHypothesis.__call__(self, *args)
        except ZeroDivisionError:
            # and if we get an error, return nan
            return float("nan")

    def compute_single_likelihood(self, datum):
        # for each correct pixel, add log(1), for each incorrect, add log(exp(-b))
        ll = 0
        input_grid = self(*datum.input)
        output_grid = datum.output
        # assuming input and output are the same size for now
        height, width = len(input_grid[:,0]), len(input_grid[0,:])
        for i in range(height):
            for j in range(width):
                if input_grid[i][j] == output_grid[i][j]:
                    ll += log(1)
                elif input_grid[i][j] == 0 and output_grid[i][j] != 0:
                    ll += log(0.1)
                else:
                    ll += log(0.05)
        # print(self.value, ll)
        return ll
        
    def display_hypothesis(self):
        return self.value
inputs = solver.train_inputs
outputs = solver.train_outputs
print([inputs[0]])
data = [FunctionData(input=[inputs[0]],output=outputs[0])]
print(data)
@primitive
def red_squares_(grid):
    grid = copy.deepcopy(grid)
    height, width = len(grid[:,0]), len(grid[0,:])
    lst = []
    for i in range(height):
        for j in range(width):
            if grid[i][j] == 2:
                lst.append((i,j))
    return lst

@primitive
def blue_squares_(grid):
    grid = copy.deepcopy(grid)
    height, width = len(grid[:,0]), len(grid[0,:])
    lst = []
    for i in range(height):
        for j in range(width):
            if grid[i][j] == 1:
                lst.append((i,j))
    return lst

@primitive
def diag_(lst, grid):
    grid = copy.deepcopy(grid)
    height, width = len(grid[:,0]), len(grid[0,:])
    diag_lst = []
    for tup in lst:
        # check whether diagonals exist on board constraints
        N, E, S, W = tup[0]-1, tup[1] + 1, tup[0] + 1, tup[1] -1
        if  N > 0 and S < height-1 and W > 0 and E < width - 1:
            diag_lst.extend([(N,W),(N,E),(S,E),(S,W)])
    return diag_lst

@primitive
def cross_(lst, grid):
    grid = copy.deepcopy(grid)
    height, width = len(grid[:,0]), len(grid[0,:])
    cross_lst = []
    for tup in lst:
        # check whether diagonals exist on board constraints
        N, E, S, W = tup[0]-1, tup[1] + 1, tup[0] + 1, tup[1] -1
        if  N > 0 and S < height-1 and W > 0 and E < width - 1:
            cross_lst.extend([(N,tup[1]),(tup[0],E),(S,tup[1]),(tup[0],W)])
    return cross_lst

@primitive
def color_(lst, clr):
    return {k:clr for k in lst}

@primitive
def paint_(dict_, grid):
    grid = copy.deepcopy(grid)
    height, width = len(grid[:,0]), len(grid[0,:])
    for coord in dict_.keys():
        grid[coord[0]][coord[1]] = dict_[coord]
    return grid
# initializing grammar
grammar = Grammar(start='GRID')

# adding production rules
grammar.add_rule('LIST', 'red_squares_', ['GRID'], 1.0)
grammar.add_rule('LIST', 'blue_squares_', ['GRID'], 1.0)
grammar.add_rule('LIST', 'diag_', ['LIST', 'GRID'], 1.0)
grammar.add_rule('LIST', 'cross_', ['LIST', 'GRID'], 1.0)
grammar.add_rule('DICT', 'color_', ['LIST','COLOR'], 1.0)
grammar.add_rule('GRID', 'paint_', ['DICT', 'GRID'], 1.0)

# adding numbers 0 through 9 as terminals
# for n in range(10):
#     grammar.add_rule('COLOR', str(n), None, 1.0)

# right now just consider 4 and 7 to see if this makes the right solution come up
grammar.add_rule('COLOR', str(4), None, 1.0)
grammar.add_rule('COLOR', str(7), None, 1.0)

grammar.add_rule('GRID', 'grid', None, 3.0)
rs = red_squares_(inputs[0])
diag_(rs, inputs[0])
grammar.display_rules()
h = ARCHypothesis()
print(h)
h(inputs[0])
DisplayGrid(h(inputs[0]))
DisplayGrid(paint_(color_(diag_(red_squares_(inputs[0]),inputs[0]),4),inputs[0]))
# problem: MHS considers the class to have no arguments. Current workaround, globally declare num_vars before running

tn = TopN(N=10) # store the top N

h0 = ARCHypothesis()
print(h0)

for h in MetropolisHastingsSampler(h0, data, steps=1000):
    tn.add(h)

for h in tn.get_all(sorted=True):
    print(h.posterior_score, h.likelihood,  h.prior, h)
print(h)
print(inputs[0])<jupyter_output>[[0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0]
 [0 0 2 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 1 0 0]
 [0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0]]
{'fraction_non_alphanumeric': 0.1501507537688442, 'fraction_numerical': 0.036984924623115575, 'mean_word_length': 2.8844652615144417, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '324037', 'license': 'Apache-2.0', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1786, 'n_words': 552, 'path': 'notebooks/.ipynb_checkpoints/ARC LOTlib-checkpoint.ipynb', 'repo_name': 'aysjajohnson/ARC', 'size': '4975'}