File size: 14,125 Bytes
3bdb76c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
from dreamcoder.program import Primitive, prettyProgram
from dreamcoder.grammar import Grammar
from dreamcoder.type import tlist, tint, arrow, baseType #, t0, t1, t2
#from functools import reduce
#todo
int_to_int = baseType("int_to_int")
int_to_bool = baseType("int_to_bool")
int_to_int_to_int = baseType("int_to_int_to_int")
#deepcoderPrimitives
Null = 300 #or perhaps something else, like "an integer outside the working range"?
def _head(xs): return xs[0] if len(xs)>0 else Null
def _tail(xs): return xs[-1] if len(xs)>0 else Null
def _take(n): return lambda xs: xs[:n]
def _drop(n): return lambda xs: xs[n:]
def _access(n): return lambda xs: xs[n] if n>=0 and len(xs)>n else Null
def _minimum(xs): return min(xs) if len(xs)>0 else Null
def _maximum(xs): return max(xs) if len(xs)>0 else Null
def _reverse(xs): return list(reversed(xs))
def _sort(xs): return sorted(xs)
def _sum(xs): return sum(xs)
#higher order:
def _map(f): return lambda l: list(map(f, l))
def _filter(f): return lambda l: list(filter(f, l))
def _count(f): return lambda l: len([x for x in l if f(x)])
def _zipwith(f): return lambda xs: lambda ys: [f(x)(y) for (x, y) in zip(xs, ys)]
def _scanl1(f):
def _inner(xs):
ys = []
if len(xs) > 0:
ys.append(xs[0])
for i in range(1, len(xs)):
ys.append( f(ys[i-1])(xs[i]))
return ys
return _inner
#int to int:
def _succ(x): return x+1
def _pred(x): return x-1
def _double(x): return x*2
def _half(x): return int(x/2)
def _negate(x): return -x
def _square(x): return x**2
def _triple(x): return x*3
def _third(x): return int(x/3)
def _quad(x): return x*4
def _quarter(x): return int(x/4)
#int to bool:
def _pos(x): return x>0
def _neg(x): return x<0
def _even(x): return x%2==0
def _odd(x): return x%2==1
#int to int to int:
def _add(x): return lambda y: x + y
def _sub(x): return lambda y: x - y
def _mult(x): return lambda y: x * y
def _min(x): return lambda y: _minimum([x,y])
def _max(x): return lambda y: _maximum([x,y])
def deepcoderPrimitives():
return [
Primitive("HEAD", arrow(tlist(tint), tint), _head),
Primitive("LAST", arrow(tlist(tint), tint), _tail),
Primitive("TAKE", arrow(tint, tlist(tint), tlist(tint)), _take),
Primitive("DROP", arrow(tint, tlist(tint), tlist(tint)), _drop),
Primitive("ACCESS", arrow(tint, tlist(tint), tint), _access),
Primitive("MINIMUM", arrow(tlist(tint), tint), _minimum),
Primitive("MAXIMUM", arrow(tlist(tint), tint), _maximum),
Primitive("REVERSE", arrow(tlist(tint), tlist(tint)), _reverse),
Primitive("SORT", arrow(tlist(tint), tlist(tint)), _sort),
Primitive("SUM", arrow(tlist(tint), tint), _sum)
] + [
Primitive("MAP", arrow(int_to_int, tlist(tint), tlist(tint)), _map), #is this okay???
Primitive("FILTER", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), #is this okay???
Primitive("COUNT", arrow(int_to_bool, tlist(tint), tint), _count), #is this okay???
Primitive("ZIPWITH", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), #is this okay???
Primitive("SCANL1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), #is this okay???
] + [
Primitive("INC", int_to_int, _succ),
Primitive("DEC", int_to_int, _pred),
Primitive("SHL", int_to_int, _double),
Primitive("SHR", int_to_int, _half),
Primitive("doNEG", int_to_int, _negate),
Primitive("SQR", int_to_int, _square),
Primitive("MUL3", int_to_int, _triple),
Primitive("DIV3", int_to_int, _third),
Primitive("MUL4", int_to_int, _quad),
Primitive("DIV4", int_to_int, _quarter),
] + [
Primitive("isPOS", int_to_bool, _pos),
Primitive("isNEG", int_to_bool, _neg),
Primitive("isEVEN", int_to_bool, _even),
Primitive("isODD", int_to_bool, _odd),
] + [
Primitive("+", int_to_int_to_int, _add),
Primitive("-", int_to_int_to_int, _sub),
Primitive("*", int_to_int_to_int, _mult),
Primitive("MIN", int_to_int_to_int, _min),
Primitive("MAX", int_to_int_to_int, _max)
]
def OldDeepcoderPrimitives():
return [
Primitive("head", arrow(tlist(tint), tint), _head),
Primitive("tail", arrow(tlist(tint), tint), _tail),
Primitive("take", arrow(tint, tlist(tint), tlist(tint)), _take),
Primitive("drop", arrow(tint, tlist(tint), tlist(tint)), _drop),
Primitive("access", arrow(tint, tlist(tint), tint), _access),
Primitive("minimum", arrow(tlist(tint), tint), _minimum),
Primitive("maximum", arrow(tlist(tint), tint), _maximum),
Primitive("reverse", arrow(tlist(tint), tlist(tint)), _reverse),
Primitive("sort", arrow(tlist(tint), tlist(tint)), _sort),
Primitive("sum", arrow(tlist(tint), tint), _sum)
] + [
Primitive("map", arrow(int_to_int, tlist(tint), tlist(tint)), _map), #is this okay???
Primitive("filter_int", arrow(int_to_bool, tlist(tint), tlist(tint)), _filter), #is this okay???
Primitive("count", arrow(int_to_bool, tlist(tint), tint), _count), #is this okay???
Primitive("zipwith", arrow(int_to_int_to_int, tlist(tint), tlist(tint), tlist(tint)), _zipwith), #is this okay???
Primitive("scanl1", arrow(int_to_int_to_int, tlist(tint), tlist(tint)), _scanl1), #is this okay???
# ] + [
# Primitive("succ", arrow(tint, tint), _succ),
# Primitive("pred", arrow(tint, tint), _pred),
# Primitive("double", arrow(tint, tint), _double),
# Primitive("half", arrow(tint, tint), _half),
# Primitive("neg", arrow(tint, tint), _neg),
# Primitive("square", arrow(tint, tint), _square),
# Primitive("triple", arrow(tint, tint), _triple),
# Primitive("third", arrow(tint, tint), _third),
# Primitive("quad", arrow(tint, tint), _quad),
# Primitive("quarter", arrow(tint, tint), _quarter),
# ] + [
# Primitive("pos", arrow(tint, tbool), _pos),
# Primitive("neg", arrow(tint, tbool), _neg),
# Primitive("even", arrow(tint, tbool), _even),
# Primitive("odd", arrow(tint, tbool), _odd),
# ] + [
# Primitive("add", arrow(tint, tint, tint), _add),
# Primitive("sub", arrow(tint, tint, tint), _sub),
# Primitive("mult", arrow(tint, tint, tint), _mult),
# Primitive("min", arrow(tint, tint, tint), _min),
# Primitive("max", arrow(tint, tint, tint), _max)
] + [
Primitive("succ_fn", int_to_int, _succ),
Primitive("pred_fn", int_to_int, _pred),
Primitive("double_fn", int_to_int, _double),
Primitive("half_fn", int_to_int, _half),
Primitive("negate_fn", int_to_int, _negate),
Primitive("square_fn", int_to_int, _square),
Primitive("triple_fn", int_to_int, _triple),
Primitive("third_fn", int_to_int, _third),
Primitive("quad_fn", int_to_int, _quad),
Primitive("quarter_fn", int_to_int, _quarter),
] + [
Primitive("pos_fn", int_to_bool, _pos),
Primitive("neg_fn", int_to_bool, _neg),
Primitive("even_fn", int_to_bool, _even),
Primitive("odd_fn", int_to_bool, _odd),
] + [
Primitive("add_fn", int_to_int_to_int, _add),
Primitive("sub_fn", int_to_int_to_int, _sub),
Primitive("mult_fn", int_to_int_to_int, _mult),
Primitive("min_fn", int_to_int_to_int, _min),
Primitive("max_fn", int_to_int_to_int, _max)
]
def deepcoderProductions():
return [(0.0, prim) for prim in deepcoderPrimitives()]
def flatten_program(p):
string = p.show(False)
num_inputs = string.count('lambda')
string = string.replace('lambda', '')
string = string.replace('(', '')
string = string.replace(')', '')
#remove '_fn' (optional)
for i in range(num_inputs):
string = string.replace('$' + str(num_inputs-i-1),'input_' + str(i))
string = string.split(' ')
string = list(filter(lambda x: x is not '', string))
return string
if __name__ == "__main__":
#g = Grammar.uniform(deepcoderPrimitives())
g = Grammar.fromProductions(deepcoderProductions(), logVariable=.9)
request = arrow(tlist(tint), tint, tint)
p = g.sample(request)
print("request:", request)
print("program:")
print(prettyProgram(p))
print("flattened_program:")
flat = flatten_program(p)
print(flat)
#robustfill output = names from productions + input_0-2 or 3
# # with open("/home/ellisk/om/ec/experimentOutputs/list_aic=1.0_arity=3_ET=1800_expandFrontier=2.0_it=4_likelihoodModel=all-or-nothing_MF=5_baseline=False_pc=10.0_L=1.0_K=5_rec=False.pickle", "rb") as handle:
# # b = pickle.load(handle).grammars[-1]
# # print b
# p = Program.parse(
# "(lambda (lambda (lambda (if (empty? $0) empty (cons (+ (car $1) (car $0)) ($2 (cdr $1) (cdr $0)))))))")
# t = arrow(tlist(tint), tlist(tint), tlist(tint)) # ,tlist(tbool))
# print(g.logLikelihood(arrow(t, t), p))
# assert False
# print(b.logLikelihood(arrow(t, t), p))
# # p = Program.parse("""(lambda (lambda
# # (unfold 0
# # (lambda (+ (index $0 $2) (index $0 $1)))
# # (lambda (1+ $0))
# # (lambda (eq? $0 (length $1))))))
# # """)
# p = Program.parse("""(lambda (lambda
# (map (lambda (+ (index $0 $2) (index $0 $1))) (range (length $0)) )))""")
# # .replace("unfold", "#(lambda (lambda (lambda (lambda (fix1 $0 (lambda (lambda (#(lambda (lambda (lambda (if $0 empty (cons $1 $2))))) ($1 ($3 $0)) ($4 $0) ($5 $0)))))))))").\
# # replace("length", "#(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ ($1 (cdr $0)) 1))))))").\
# # replace("forloop", "(#(lambda (lambda (lambda (lambda (fix1 $0 (lambda (lambda (#(lambda (lambda (lambda (if $0 empty (cons $1 $2))))) ($1 ($3 $0)) ($4 $0) ($5 $0))))))))) (lambda (#(eq? 0) $0)) $0 (lambda (#(lambda (- $0 1)) $0)))").\
# # replace("inc","#(lambda (+ $0 1))").\
# # replace("drop","#(lambda (lambda (fix2 $0 $1 (lambda (lambda (lambda (if
# # (#(eq? 0) $1) $0 (cdr ($2 (- $1 1) $0)))))))))"))
# print(p)
# print(g.logLikelihood(t, p))
# assert False
# print("??")
# p = Program.parse(
# "#(lambda (#(lambda (lambda (lambda (fix1 $0 (lambda (lambda (if (empty? $0) $3 ($4 (car $0) ($1 (cdr $0)))))))))) (lambda $1) 1))")
# for j in range(10):
# l = list(range(j))
# print(l, p.evaluate([])(lambda x: x * 2)(l))
# print()
# print()
# print("multiply")
# p = Program.parse(
# "(lambda (lambda (lambda (if (eq? $0 0) 0 (+ $1 ($2 $1 (- $0 1)))))))")
# print(g.logLikelihood(arrow(arrow(tint, tint, tint), tint, tint, tint), p))
# print()
# print("take until 0")
# p = Program.parse("(lambda (lambda (if (eq? $1 0) empty (cons $1 $0))))")
# print(g.logLikelihood(arrow(tint, tlist(tint), tlist(tint)), p))
# print()
# print("countdown primitive")
# p = Program.parse(
# "(lambda (lambda (if (eq? $0 0) empty (cons (+ $0 1) ($1 (- $0 1))))))")
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tint, tlist(tint)), arrow(
# tint, tlist(tint))), p))
# print(_fix(9)(p.evaluate([])))
# print("countdown w/ better primitives")
# p = Program.parse(
# "(lambda (lambda (if (eq0 $0) empty (cons (+1 $0) ($1 (-1 $0))))))")
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tint, tlist(tint)), arrow(
# tint, tlist(tint))), p))
# print()
# print("prepend zeros")
# p = Program.parse(
# "(lambda (lambda (lambda (if (eq? $1 0) $0 (cons 0 ($2 (- $1 1) $0))))))")
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tint,
# tlist(tint),
# tlist(tint)),
# tint,
# tlist(tint),
# tlist(tint)),
# p))
# print()
# assert False
# p = Program.parse(
# "(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ 1 ($1 (cdr $0))))))))")
# print(p.evaluate([])(list(range(17))))
# print(g.logLikelihood(arrow(tlist(tbool), tint), p))
# p = Program.parse(
# "(lambda (lambda (if (empty? $0) 0 (+ 1 ($1 (cdr $0))))))")
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tlist(tbool), tint), arrow(
# tlist(tbool), tint)), p))
# p = Program.parse(
# "(lambda (fix1 $0 (lambda (lambda (if (empty? $0) 0 (+ (car $0) ($1 (cdr $0))))))))")
# print(p.evaluate([])(list(range(4))))
# print(g.logLikelihood(arrow(tlist(tint), tint), p))
# p = Program.parse(
# "(lambda (lambda (if (empty? $0) 0 (+ (car $0) ($1 (cdr $0))))))")
# print(p)
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tlist(tint),
# tint),
# tlist(tint),
# tint),
# p))
# print("take")
# p = Program.parse(
# "(lambda (lambda (lambda (if (eq? $1 0) empty (cons (car $0) ($2 (- $1 1) (cdr $0)))))))")
# print(p)
# print(
# g.logLikelihood(
# arrow(
# arrow(
# tint,
# tlist(tint),
# tlist(tint)),
# tint,
# tlist(tint),
# tlist(tint)),
# p))
# assert False
# print(p.evaluate([])(list(range(4))))
# print(g.logLikelihood(arrow(tlist(tint), tlist(tint)), p))
# p = Program.parse(
# """(lambda (fix (lambda (lambda (match $0 0 (lambda (lambda (+ $1 ($3 $0))))))) $0))""")
# print(p.evaluate([])(list(range(4))))
# print(g.logLikelihood(arrow(tlist(tint), tint), p))
|