Advertisement
logicmoo

Untitled

Jan 26th, 2022
2,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 12.00 KB | None | 0 0
  1.  
  2. :- use_module(library(logicmoo_common)).
  3. /*
  4. JSON Conversion
  5. :- use_module(library(http/json_convert)).
  6.  
  7. test_pairs(Name,Type,In,Out):-
  8.   kaggle_arc_eval(Name,Stuff), once(atom_json_term(Stuff,json(L),[])),
  9.   json_pairs(L,Type,In,Out).
  10.  
  11. json_pairs([],_,_,_):- !, fail.
  12. json_pairs(json(T),Type,In,Out):-!,json_pairs(T,Type,In,Out).
  13. json_pairs([input=In,output=Out],_Type,In,Out):-!.
  14. json_pairs(Type=List,Type,In,Out):-!,member(L,List),
  15.    json_pairs(L,Type,In,Out).
  16. json_pairs([H|T],Type,In,Out):-!,
  17.   (json_pairs(H,Type,In,Out);json_pairs(T,Type,In,Out)).
  18.  
  19. %print_trainer:- kaggle_arc_train(Name,Stuff), atom_json_term(Stuff,JSON,[]),print_arc(Name,JSON).
  20. %print_evaler:- kaggle_arc_eval(Name,Stuff), atom_json_term(Stuff,JSON,[]),print_arc(Name,JSON).
  21.  
  22. */
  23.  
  24. :- [kaggle_arc_train].
  25. :- [kaggle_arc_eval].
  26.  
  27. print_trainer:- kaggle_arc_train(Name,Type,In,Out),print_arc(Name=in(Type),In),print_arc(Name=out(Type),Out).
  28.  
  29. % Type is test or train
  30. kaggle_arc(Name,Type,In,Out):- kaggle_arc_train(Name,Type,In,Out).
  31. kaggle_arc(Name,Type,In,Out):- kaggle_arc_eval(Name,Type,In,Out).
  32.  
  33. /*
  34. % data looks like
  35.  
  36. kaggle_arc_train('007bbfb7',train,[[0,7,7],[7,7,7],[0,7,7]],[[0,0,0,0,7,7,0,7,7],[0,0,0,7,7,7,7,7,7],[0,0,0,0,7,7,0,7,7],[0,7,7,0,7,7,0,7,7],[7,7,7,7,7,7,7,7,7],[0,7,7,0,7,7,0,7,7],[0,0,0,0,7,7,0,7,7],[0,0,0,7,7,7,7,7,7],[0,0,0,0,7,7,0,7,7]]).
  37. kaggle_arc_train('007bbfb7',train,[[4,0,4],[0,0,0],[0,4,0]],[[4,0,4,0,0,0,4,0,4],[0,0,0,0,0,0,0,0,0],[0,4,0,0,0,0,0,4,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,0,4,0,4,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,4,0,0,0,0]]).
  38. kaggle_arc_train('007bbfb7',train,[[0,0,0],[0,0,2],[2,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,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,2],[0,0,0,0,0,0,2,0,2],[0,0,0,0,0,0,0,0,0],[0,0,2,0,0,0,0,0,2],[2,0,2,0,0,0,2,0,2]]).
  39. kaggle_arc_train('007bbfb7',train,[[6,6,0],[6,0,0],[0,6,6]],[[6,6,0,6,6,0,0,0,0],[6,0,0,6,0,0,0,0,0],[0,6,6,0,6,6,0,0,0],[6,6,0,0,0,0,0,0,0],[6,0,0,0,0,0,0,0,0],[0,6,6,0,0,0,0,0,0],[0,0,0,6,6,0,6,6,0],[0,0,0,6,0,0,6,0,0],[0,0,0,0,6,6,0,6,6]]).
  40. kaggle_arc_train('007bbfb7',train,[[2,2,2],[0,0,0],[0,2,2]],[[2,2,2,2,2,2,2,2,2],[0,0,0,0,0,0,0,0,0],[0,2,2,0,2,2,0,2,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,2,2,2,2,2,2],[0,0,0,0,0,0,0,0,0],[0,0,0,0,2,2,0,2,2]]).
  41. kaggle_arc_train('007bbfb7',test,[[7,0,7],[7,0,7],[7,7,0]],[[7,0,7,0,0,0,7,0,7],[7,0,7,0,0,0,7,0,7],[7,7,0,0,0,0,7,7,0],[7,0,7,0,0,0,7,0,7],[7,0,7,0,0,0,7,0,7],[7,7,0,0,0,0,7,7,0],[7,0,7,7,0,7,0,0,0],[7,0,7,7,0,7,0,0,0],[7,7,0,7,7,0,0,0,0]]).
  42.  
  43. kaggle_arc_train('00d62c1b',train,[[0,0,0,0,0,0],[0,0,3,0,0,0],[0,3,0,3,0,0],[0,0,3,0,3,0],[0,0,0,3,0,0],[0,0,0,0,0,0]],[[0,0,0,0,0,0],[0,0,3,0,0,0],[0,3,4,3,0,0],[0,0,3,4,3,0],[0,0,0,3,0,0],[0,0,0,0,0,0]]).
  44. kaggle_arc_train('00d62c1b',train,[[0,0,0,0,0,0,0,0,0,0],[0,0,3,0,3,0,0,0,0,0],[0,0,0,3,0,3,0,0,0,0],[0,0,3,0,0,0,3,0,0,0],[0,0,0,0,0,3,0,3,0,0],[0,0,0,3,0,3,3,0,0,0],[0,0,3,3,3,0,0,0,0,0],[0,0,0,3,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,0,0,0,0,0,0],[0,0,3,0,3,0,0,0,0,0],[0,0,0,3,0,3,0,0,0,0],[0,0,3,0,0,0,3,0,0,0],[0,0,0,0,0,3,4,3,0,0],[0,0,0,3,0,3,3,0,0,0],[0,0,3,3,3,0,0,0,0,0],[0,0,0,3,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]]).
  45. kaggle_arc_train('00d62c1b',train,[[0,0,0,0,0,3,0,0,0,0],[0,0,0,0,3,0,0,0,0,0],[0,3,3,0,3,3,0,3,0,0],[3,0,0,3,0,0,3,0,3,0],[0,0,0,3,0,0,3,3,0,0],[0,0,0,3,0,0,3,0,0,0],[0,0,0,3,0,0,3,0,0,0],[0,0,0,0,3,3,0,3,0,0],[0,0,0,0,0,0,0,0,3,0],[0,0,0,0,0,0,0,0,0,0]],[[0,0,0,0,0,3,0,0,0,0],[0,0,0,0,3,0,0,0,0,0],[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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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,3,4,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,3,0,3,3,3,3,3,0,3,3,0,0,0,0,0,0,0,0],[0,0,0,0,3,4,4,4,4,3,4,4,3,0,0,0,0,0,0,0],[0,0,0,0,3,3,3,3,3,0,3,3,3,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,3,4,4,4,3,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,3,4,4,4,3,0,0],[0,0,0,0,0,0,0,0,0,3,3,3,3,3,4,4,4,3,0,0],[0,0,0,0,0,0,0,0,0,3,4,4,4,3,4,4,4,3,0,0],[0,0,0,0,0,0,0,0,3,3,3,3,3,3,4,4,4,3,0,0],[0,0,0,0,0,0,3,3,4,3,0,0,0,3,3,3,3,3,0,0],[0,0,3,0,0,0,0,0,3,3,0,0,0,0,0,0,0,0,0,0],[0,3,4,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,3,0,3,0,3,3,3,3,3,3,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,3,4,4,4,3,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,3,4,4,4,3,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,3,3,3,3,3,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]]).
  49. */
  50. %tell(s),ignore((nl,nl,test_pairs(Name,Type,In,Out),format('~N~q.~n',[test_pairs_cache(Name,Type,In,Out)]),fail)),told.
  51.  
  52. is_grid([H|L]):- nonvar(L), is_list(H),member(E,H),!,\+ is_list(E).
  53.  
  54. print_arc(Name,json(JSON)):-!, print_arc(Name,JSON).
  55. print_arc(Name,train=Y):- !, print_arc(Name,Y).
  56. print_arc(Name,X->Y):- !, print_arc(in(Name),X), print_arc(out(Name),Y).
  57. print_arc(Name,X=Y):- !, print_arc(Name=X,Y).
  58. print_arc(Name,[H|L]):- is_grid([H|L]),!,print_grid(Name,[H|L]).
  59. print_arc(Name,[H|L]):- !, maplist(print_arc(Name),[H|L]).
  60. print_arc(Name,Val):- print_tree_nl(Name=Val).
  61.  
  62. % Grid pretty printing
  63. print_grid(Name,Grid):- wdmsg(Name),print_grid(Grid).
  64. print_grid(Grid):- maplist(print_rows,Grid),nl.
  65. print_rows(List):- maplist(write_c,List),nl.
  66. block_colors([(black),(blue),(red),(green),(yellow),'#c0c0c0',(magenta),'#ff8c00',(cyan),'#8b4513']).
  67. write_c(V):-var(V),!,write(' ?').
  68. write_c(0):-!,write(' .').
  69. write_c(N):-write(' '),write_c1(N).
  70. write_c1(C):- integer(C),block_colors(L),nth0(C,L,Color),ansi_format(fg(Color),'~w',[C]),!.
  71. write_c1(C):-write(' '),write(C).
  72.  
  73. % Random Non Blk Eles
  74. first_color(Grid1,C1):- sub_term(C1,Grid1),C1 \= 0,integer(C1).
  75.  
  76. % Grid size/resize
  77. make_lengths(N,L):- length(L,N).
  78. grid_size(Grid,Size):- grid_size_nd(Grid,Size),!.
  79. grid_size_nd([C,R|Rows],size(H,V)):-
  80.    (var(Rows)->between(2,30,V);!),
  81.    length([C,R|Rows],V),
  82.    (var(R)->between(1,30,H);true),
  83.    length(R,H),
  84.    (is_list(C)->true;(length(C,H),maplist(make_lengths(H),Rows))).
  85. grid_size_nd([L],size(H,1)):- (var(L)->between(1,30,H);true), length(L,H).
  86.  
  87. % make or do plan
  88. do_change(Change,Grid1,Grid2):- \+ is_list(Change),!,one_change(Change,Grid1,Grid2).
  89. do_change(Change,Grid1,Grid2):- do_change_nd(Change,Grid1,Grid2).
  90.  
  91. do_change_nd([],Grid1,Grid1).
  92. do_change_nd([H|T],Grid1,Grid2):- one_change(H,Grid1,GridM),do_change_nd(T,GridM,Grid2).
  93.  
  94. one_change(same,Grid1,Grid2):- is_grid(Grid2),Grid1=Grid2,!.
  95. one_change(colorChange(C1,C2),Grid1,Grid2):-
  96.   first_color(Grid1,C1),ignore((is_grid(Grid2),first_color(Grid2,C2))),
  97.   subst(Grid1,C1,C2,Grid2).
  98. one_change(blank1Color(C1),Grid1,Grid2):-
  99.   first_color(Grid1,C1),copy_cells(==(C1),free_cell,Grid1,Grid2).
  100. one_change(same_size,Grid1,Grid2):- var(Grid2),grid_size(Grid1,C1),grid_size(Grid2,C1),!.
  101. one_change(resize(C1,C2),Grid1,Grid2):- var(Grid2),grid_size(Grid1,C1),grid_size(Grid2,C2).
  102.  
  103. individual(Grid,Indv):- hv_value(Grid,C,H,V),individual_from(Grid,C,H,V,Indv).
  104.  
  105.  
  106. individual_from(Grid,C,H,V,[How|Indv]):- nav_to(Grid,C,H,V,How,C,H2,V2),individual_from(Grid,C,H2,V2,Indv).
  107. individual_from(Grid,C,H,V,[]):- !.
  108.  
  109.  
  110. hv_value(Grid,C,H,V):- nth1(V,Grid,Row),nth1(H,Row,C).
  111. /*b_set_hv_value(Grid,C,H,V):- nth1(V,Grid,Row),set_nth1(H,Row,C).
  112. nb_set_hv_value(Grid,C,H,V):- nth1(V,Grid,Row),nb_set_nth1(H,Row,C).
  113. b_rplc_hv_value(Grid,OldC,NewC,H,V):- nth1(V,Grid,Row),rplc_nth1(H,Row,OldC,NewC).
  114. nb_rplc_hv_value(Grid,OldC,NewC,H,V):- nth1(V,Grid,Row),nb_rplc_nth1(H,Row,OldC,NewC).
  115.  
  116. */
  117. % turtle(H,V,Dir,N,H2,V2):-
  118. prim_ops([
  119. call_object_grid_size(obj),
  120. trim_grid_to_size(point,size),
  121. fill_from_point(point,color),
  122. create_a_ray(point,dir,len),
  123. object_as_own_grid(obj,gridOps),
  124. copy_one_object(obj,point),
  125. rotate_one_object(obj,nsew),
  126. flatten_one_object(obj),
  127. sort_by_gravity(nsew),
  128. flip_grid(hOrv),
  129. rotate_grid(nsew)]).
  130.  
  131. create_movements:-
  132.  forall( between(1,30,H),
  133.   forall(between(1,30,V),
  134.   calc_movement(H,V))).
  135.  
  136. calc_movement(H,V):- forall(nav(Dir,HO,VO), save_calc_movement(H,V,Dir,HO,VO)).
  137.  
  138. save_calc_movement(H,V,Dir,HO,VO):- H2 is HO+H, V2 is VO+V,
  139.   ignore((between(1,30,H2), between(1,30,V2),
  140.     format(atom(HV),'point_~`0t~d~2+_~`0t~d~2+',  [H,V]),
  141.     format(atom(HV2),'point_~`0t~d~2+_~`0t~d~2+', [H2,V2]),
  142.     assert_if_new(save1_calc_movement(HV,Dir,HV2)),
  143.     assert_if_new(save1_calc_point(H,V,HV)),
  144.     assert_if_new(save1_calc_movement(H,V,Dir,H2,V2)))).
  145.  
  146.  
  147.  
  148.  
  149. % eventually use 2nd arg as a hueristic
  150. nav_to(Grid,_OldC,H,V,How,C,H2,V2):- save1_calc_movement(H,V,How,H2,V2), hv_value(Grid,C,H2,V2).
  151.  
  152. nav(s,0,1). nav(e, 1,0). nav(w,-1,0). nav(n,0,-1).
  153. nav(se, 1,1). nav(sw,-1,1). nav(nw,-1,-1). nav(ne, 1,-1).
  154.  
  155. rot45(s,sw). rot45(sw,w). rot45(w,nw). rot45(nw,n). rot45(n,ne). rot45(ne,e). rot45(e,se). rot45(se,s).
  156.  
  157.  
  158. free_cell(Var):- var(Var),!.
  159. free_cell(0).
  160.  
  161. copy_cells(B,A,H,HH):- call(B,H),!,call(A,HH).
  162. copy_cells(_,_,H,H):- \+ is_list(H),!.
  163. copy_cells(_,_,[],[]):-!.
  164. copy_cells(B,A,[H|T],[HH|TT]):-!, copy_cells(B,A,H,HH), copy_cells(B,A,T,TT).
  165.  
  166.  
  167. same_grid(Grid1,Grid1).
  168.  
  169.  
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement