include("AI_unravels_the_chess.h"). ?- G_Models := 16, % from 1 to 16 G_Sel := 0, G_Pause := 0, G_Freeze := 0, G_with_Randomness := 0, G_Observed := 0, G_ObserveNow := 0, G_One_Step := 0, G_FastSpeed := 0, G_Last_Speed := 0, G_Results := 0, G_Max1 := min_integer, G_Max2 := min_integer, array(active, 16, 0), array(circuits, 16, 0), array(time_last, 16, 0), array(used_states, 16, 0), array(all_arrows, 16, 0), array(all_traces, 16, 0), array(active_previous, 16, 0), array(active_last, 16*2, 0), array(events_last, 16*2, 0), array(links, 16*8*8, 0), array(states, 16*8*4, 0), array(events, 16*8*10, max_integer), array(results, 16*8*4, 0), % G_K =< 4 array(results0, 8*4, 0), % G_K =< 4 array(buf_views, 50, 0), array(buf_incorrect, 50, 0), array(buf_actions, 50, 0), array(buf_arrows, 16*50, 0), array(buf_the_trace, 16*50, 0), array(board, 66, 0), init_all, size(screen, _, H), G_Zoom_Font := (H >= 1080 -> 1 else 0.8), font(G_Zoom_Font*10, G_Zoom_Font*25, "Courier"), (G_Zoom_Font=:=1 -> X := 1270, Y := 1100 else X := 1020, Y := 760), window(title("AI Unravels the Chess"), size(X, Y), pos(100, 10), paint_indirectly,double_clicks). init_all:- start_random_from(3), % make the random deterministic G_K := 3, % possible values in input/output, from 2 to 4 G_Time := 0, G_Incorrect := 0, clean_buffers, init_board, update_memory, board(65):= 7, fixed_models, for( Model, 6, 15), init_model(Model), fail. init_all. init_model(Model):- all_arrows(Model) := 1, all_traces(Model) := 3+random(2), % for trace, from 1 to 4 circuits(Model) := 1, % increase for more complicated models init_events(Model, 0, 4, all_traces(Model)), init_Links(Model). time_func(end) :- check_pause, update_memory, G_Time := G_Time + 1, set_window_text, % observation given by the world clean_results, G_Max1 := min_integer, G_Max2 := min_integer, calc_observation(0), Min1 := min_integer, create_probability(Min1, _, _, _, _, Value, calc), buf_views(G_Time mod 50):= Value, % first transition transition(1), % action done by the agent G_Incorrect := 0, calc_observation(1), create_incorrect, (G_Incorrect =:= 2**G_K - 1 -> find_possible), buf_incorrect(G_Time mod 50):= G_Incorrect, (G_Freeze =:= 0 -> find_correct(Action) else Action := 0 ), buf_actions(G_Time mod 50):= Action, % second transition transition(-1), update_window(_). transition(Views):- for( Model, 0, G_Models-1), transition2(Model, Views), fail. transition(Views). transition2(Model, Views):- events_are_true(Model, Events, Views), choose_new_state(Model, New, Events), (New =\= active(Model) -> (Views =:= 1-> active_last(2*Model+0) := active(Model), active_last(2*Model+1) := New, events_last(2*Model+0) := links(64*Model+8*active(Model)+New)/\Events, events_last(2*Model+1) := 0 else (G_Time =\= time_last(Model) -> active_last(2*Model+0) := active(Model), active_last(2*Model+1) := active(Model), events_last(2*Model+0) := 0 ), events_last(2*Model+1) := links(64*Model+8*active(Model)+New)/\Events ), buf_arrows(50*Model + G_Time mod 50):= events_last(2*Model+0) \/ events_last(2*Model+1), time_last(Model) := G_Time, active(Model) := New else (Views =:= 1-> buf_arrows(50*Model + G_Time mod 50):= 0 )). choose_new_state(Model, New, Events):- New := active(Model), for(I, 0, 7), links(64*Model+8*active(Model)+I)/\Events =\= 0, (New =:= active(Model); random(100) < 33 -> New := I), fail. choose_new_state(Model, New, Events). set_window_text :- set_text("AI Unravels the Chess, step="+G_Time+(G_Freeze =:= 1 -> " (Agent is Freezed)" else "")+ (G_Pause =:= 1 -> (G_One_Step > 1 -> " (One Transition)" else " (Pause)") else ""), _). check_pause:- (G_Pause =:= 1 -> (G_One_Step =:= 1 -> G_One_Step := 0 else G_One_Step =:= 2, (G_Time =:= time_last(G_Sel) -> G_One_Step := 0) ) ). menu_agent(init) :- menu(normal, action(menu_freeze), text("&Freeze\tZ") ). menu_world(init) :- menu(normal, action(menu_with_randomness), text("&With randomness") ). menu_time(init) :- menu(normal, action(menu_pause), text("&Pause\tP") ), menu(separator), menu(normal, action(menu_one_step), text("&One Step\tO") ), menu(separator), menu(normal, action(menu_one_transition), text("&One Transition\tI") ), menu(separator), menu(normal, action(menu_slow_motion), text("&Slow Motion\tS") ), menu(checked, action(menu_normal_speed), text("&Normal Speed") ), menu(normal, action(menu_fast_speed), text("&Fast Speed\tF") ). menu_show(init) :- menu(normal, action(menu_results), text("Results &List\tL") ). menu_help(init) :- menu(normal, action(menu_description), text("&Description") ), menu(normal, action(menu_notetions), text("&Notetions") ). menu_shift(init) :- menu(normal, action(sift_left), text("&Left\t<-") ), menu(normal, action(sift_right), text("&Right\t->") ). menu_restart(press) :- init_all, set_window_text, update_window(_). menu_fast_speed(press) :- G_FastSpeed:=(G_FastSpeed=:=1 -> 0 else 1), change_speed. menu_normal_speed(press) :- (G_FastSpeed=\=0-> G_FastSpeed:=0, change_speed ). menu_slow_motion(press) :- G_FastSpeed:=(G_FastSpeed=:= -1 -> 0 else -1), change_speed. change_speed:- modify_menu(G_Menu_Time, 8+G_Last_Speed, normal, _ ), modify_menu(G_Menu_Time, 8+G_FastSpeed, checked, _ ), G_Last_Speed := G_FastSpeed, Interval := (G_FastSpeed =:= 1 -> 0.01 else (G_FastSpeed =:= 0 -> 0.5 else 2)), kill_timer(_, G_Timer), G_Timer := set_timer(_, Interval, time_func). menu_results(press) :- G_Results := 1 - G_Results, modify_menu(G_Menu_Show, 1, (G_Results =:= 1 -> checked else normal), _ ), update_window(_). menu_freeze(press) :- G_Freeze := 1 - G_Freeze, modify_menu(G_Menu_Agent, 1, (G_Freeze =:= 1 -> checked else normal), _ ), set_window_text. menu_with_randomness(press) :- G_with_Randomness := 1 - G_with_Randomness, modify_menu(G_Menu_World, 1, (G_with_Randomness =:= 1 -> checked else normal), _ ). menu_pause(press) :- G_Pause := 1 - G_Pause, modify_menu(G_Menu_Time, 1, (G_Pause =:= 1 -> checked else normal), _ ), G_One_Step := 0, set_window_text. menu_one_step(press) :- (G_Pause =:= 1 -> G_One_Step := 1 ), time_func(end). menu_one_transition(press) :- G_Pause := 1, modify_menu(G_Menu_Time, 1, checked, _ ), G_One_Step := 2, time_func(end). menu_description(press):- message("AI Unravels the Chess", "What is this program? 1. This is not a program which can play chess. 2. This is not Artificial Intelligence that can learn the rules of any game. 3. This is a program containing the rules of the chess. In order to understand the rules of the chess, AI has to find them automatically. To find something, you need to know what you are looking for.",i). menu_notetions(press):- message("AI Unravels the Chess - Notetions", "a0=0 means that the action at the current step is zero. v0=0 means that the observation at the current step is zero. a1 means the action at the previous step (the current minus one). a0=\\=0 means that the action at the current step is not zero. a0*0 means that the action \"zero\" at the current step is correct.",i). sift_left(press):- move_model(-1). sift_right(press):- move_model(1). move_model(Direction):- G_Sel := G_Sel + Direction, (G_Sel < 0 -> G_Sel := 0), (G_Sel > G_Models-1 -> G_Sel := G_Models-1), update_window(_). win_func(init) :- menu(normal, action(menu_restart), text("&Restart") ), G_Menu_Agent := menu( pop_up, action(menu_agent), text("&Agent") ), G_Menu_World := menu( pop_up, action(menu_world), text("&World") ), G_Menu_Time := menu( pop_up, action(menu_time), text("&Time") ), G_Menu_Show := menu( pop_up, action(menu_show), text("&Show") ), menu( pop_up, action(menu_shift), text("&Shift") ), menu( right, pop_up, action(menu_help), text("&Help") ), G_Timer := set_timer(_, 0.5, time_func). win_func(size(X,Y)):- G_Zoom := Y/967, update_window(_). win_func(key_down(37, Rep)):- sift_left(press). win_func(key_down(39, Rep)):- sift_right(press). win_func(key_down(82, Rep)):- % R menu_restart(press). win_func(key_down(90, Rep)):- % Z menu_freeze(press). win_func(key_down(80, Rep)):- % P menu_pause(press). win_func(key_down(79, Rep)):- % O menu_one_step(press). win_func(key_down(73, Rep)):- % I menu_one_transition(press). win_func(key_down(83, Rep)):- % S menu_slow_motion(press). win_func(key_down(70, Rep)):- % F menu_fast_speed(press). win_func(key_down(76, Rep)):- % L menu_results(press). win_func(double_click(X, Y)):- (G_Zoom_Font=:=1 -> N := 30, X2 := X else N := 24, X2 := X + 36), (Y<66, Y>8, abs((X2+N//4) mod N - N//2) =< N//4 -> Observed := X2 // N - 2, (Observed > 5, Observed < 16, G_Observed =\= Observed -> G_Freeze := 1, modify_menu(G_Menu_Agent, 1, checked, _ ), G_Observed := Observed else G_Observed := 0 ) ), update_window(_). win_func(mouse_click(X, Y)):- (G_Zoom_Font=:=1 -> N := 30, X2 := X else N := 24, X2 := X + 36), (Y<66, Y>8, abs((X2+N//4) mod N - N//2) =< N//4 -> G_Sel := X2 // N - 2 else (YG_Zoom*750 -> I := X//N - 33, (I >= 0, I < 8 -> J := 7 - (Y - G_Zoom*750)//(G_Zoom*25), active(1) := I, active_previous(1) := I, active(2) := J, active_previous(2) := J ) )), update_window(_). win_func(paint):- color_text(_, rgb(0,0,0)), text_out(" ", pos(0,10)), (G_Zoom_Font=:=1 -> text_out(" ")), for(I, 0, G_Models-1), (make_colors(I) -> true), names(I, Name, _, _, _), text_out(Name+" "), fail. win_func(paint):- color_text(_, rgb(0,0,255)), color_text_back(_, system_color(window)), text_out(" ", pos(0,40)), (G_Zoom_Font=:=1 -> text_out(" ")), for(I, 0, G_Models-1), (I > 5-> text_out((observable(I)-> (G_Observed > 0 -> "x " else "* ") else ". ")) else (I =:= 4-> color_text(_, rgb(255,0,0)), text_out(("2 ")) else (I =:= 5-> text_out(("1 ")), color_text(_, rgb(0,0,255)) else text_out(active_previous(I)+" ") ))), fail. win_func(paint):- show_line(0, "views ", 0, buf_views). win_func(paint):- show_line(0, "incorrect", 40, buf_incorrect). win_func(paint):- show_line(0, "actions ", 80, buf_actions). win_func(paint):- show_line(G_Sel, "arrows ", 120, buf_arrows). win_func(paint):- show_line(G_Sel, "the trace", 160, buf_the_trace). win_func(paint):- X := 990, text_out(" ", pos(G_Zoom_Font*X, G_Zoom*700)), make_chess_squre(X, -8, 9), for(J, 0, 7), for(I, 0, 7), make_chess_squre(X, I, J), fail. win_func(paint):- color_text_back(_, system_color(window)), G_Results =:= 0, (G_Sel =:= 4 -> show_grid(8, 8) else (G_Sel =:= 5 -> show_grid(2, 1) )), fail. win_func(paint):- (G_Zoom_Font=:=1 -> N := 4+2*G_Sel, S := 15 else N := 1+2*G_Sel, S := 12 ), I := 2*active_previous(1), J := active_previous(2), X := G_Zoom_Font*990, Y := G_Zoom*(925-25*J), pen(2, rgb(0, 0, 255)), line(X+I*S-2, Y, X+(I+2)*S+1, Y, X+(I+2)*S+1, Y+G_Zoom_Font*26, X+I*S-2, Y+G_Zoom_Font*26, X+I*S-2, Y), pen(2, rgb(255, 0, 0)), line(N*S-2, 39, (N+1)*S+1, 39, (N+1)*S+1, 65, N*S-2, 65, N*S-2, 39), (G_Sel > 3, G_Sel < 6, G_Results =:= 0 -> pen(8, rgb(255, 0, 0)), (G_Sel =:= 4 -> X := G_Zoom*(20 + 35*I), Y := G_Zoom*(610 - 70*J) else X := G_Zoom*(300-70 + 70*active_previous(3)), Y := G_Zoom*(400-35) ), S := 70*G_Zoom, line(X, Y, X+S, Y, X+S, Y+S, X, Y+S, X, Y) ), fail. win_func(paint):- G_Results =:= 0, (G_Sel > 3, G_Sel < 6 -> show_memory_rules ), fail. win_func(paint):- G_Results =:= 0, for(Event, 0, all_arrows(G_Sel)-1), makeColor(Event, _, Color), (2**Event/\buf_arrows(50*G_Sel + G_Time mod 50) =\= 0 -> pen(10, Color) else pen(3, Color) ), line(G_Zoom*600, G_Zoom*(50+Event*70), G_Zoom*1000, G_Zoom*(50+Event*70)), line(G_Zoom*970, G_Zoom*(60+Event*70), G_Zoom*1000, G_Zoom*(50+Event*70), G_Zoom*970, G_Zoom*(40+Event*70)), pen(3, Color), color_text(_, rgb(255,0,0)), Star := (event_is_true(G_Sel, Event, 0, 0)-> "*" else " "), text_out(Star+2**Event+". ", pos(G_Zoom*600, G_Zoom*(70+Event*70))), color_text(_, rgb(0,0,0)), print_event(Event, 0), for(I, 0, 7), for(J, 0, 7), links(64*G_Sel+8*I+J)/\2**Event =\= 0, R := (active_previous(G_Sel)=:=J -> 40 else 25), arrow(I, J, R, Event), fail. win_func(paint):- G_Results =:= 0, (observable(G_Sel) -> color_text(_, rgb(0,0,0)) else color_text(_, rgb(255,0,0)) ), names(G_Sel, _, Name, _, _), text_out(Name, pos(G_Zoom*70, G_Zoom*110)), G_Sel =\= 4, G_Sel =\= 5, color_text(_, rgb(255,0,0)), text_out("The trace", pos(G_Zoom*600, G_Zoom*390)), for(Event, 0, all_traces(G_Sel)-1), Cell := events(80*G_Sel+10*(Event+4)), Delay := true_value((Cell//(2*G_K)) mod 6 =:= 2), (event_is_true(G_Sel, Event+4, 1, Delay)-> State := (Delay =:= 1; G_Time =\= time_last(G_Sel) -> active_previous(G_Sel) else active_last(2*G_Sel+1)), Trace := states(32*G_Sel+4*State+Event), Star := "*" else Trace := 5, Star := " " ), makeColor(_, Trace, Color), pen(10, Color), line(G_Zoom*600, G_Zoom*(430+Event*70), G_Zoom*1000, G_Zoom*(430+Event*70)), color_text(_, rgb(255,0,0)), text_out(Star+2**Event+". ", pos(G_Zoom*600,G_Zoom*(450+Event*70))), color_text(_, rgb(0,0,0)), print_event(Event+4, 0), fail. win_func(paint):- G_Results =:= 1, color_text(_, rgb(0,0,0)), text_out("everytime", pos(G_Zoom*80,G_Zoom*90)), text_out(" never", pos(G_Zoom*250,G_Zoom*90)), text_out(" higher", pos(G_Zoom*450,G_Zoom*90)), text_out(" lower", pos(G_Zoom*600,G_Zoom*90)), color_text(_, rgb(255,0,0)), text_out("collisions", pos(G_Zoom*750,G_Zoom*90)), color_text(_, rgb(0,0,255)), text_out("possibility", pos(G_Zoom*950,G_Zoom*90)), make_line(1, 100), make_line(2, 250), make_line(3, 450), make_line(4, 600), make_table(160, 0), make_table(190, 4), fail. win_func(paint):- pen(1, rgb(0, 255 ,0)), line(G_Zoom*10, G_Zoom*865, G_Zoom_Font*970, G_Zoom*865), (G_Results =:= 1 -> line(G_Zoom*110, G_Zoom*160, G_Zoom*110, G_Zoom*740), line(G_Zoom*460, G_Zoom*160, G_Zoom*460, G_Zoom*740), line(G_Zoom*800, G_Zoom*160, G_Zoom*800, G_Zoom*740), line(G_Zoom*950, G_Zoom*160, G_Zoom*950, G_Zoom*740) ), G_Results =:= 0, pen(3, rgb(0,0,0)), makeColor(_, 5, Color), brush(Color), for(I, 0, 7), states(32*G_Sel+4*I)>0, calcCoords(X, Y, I), (active_previous(G_Sel)=:=I -> R := 40, (G_Time mod 2 =:= 1 -> pen(6, rgb(0,0,0))) else R := 25 ), ellipse(X-R, Y-R, X+R, Y+R), draw_trace(I, X, Y, R, 0, 15, -15), draw_trace(I, X, Y, R, 1, 10, -5), draw_trace(I, X, Y, R, 2, 10, 5), draw_trace(I, X, Y, R, 3, 15, 15), (active_previous(G_Sel)=:=I -> text_out(I, pos(X-6, Y-10)) ), pen(3, rgb(0, 0, 0)), fail. make_table(Y, Row1):- for(Row, Row1, Row1+G_K-1), color_text(_, rgb(255,0,0)), (Row1 =:= 0 -> Star := (Row =:= buf_views(G_Time mod 50) -> "*" else " "), Text := (G_K > 2, Row>0 -> string_from_ASCII(Row-1+first_ASCII("x")) else Row) else Star := (Row-4 =:= buf_actions(G_Time mod 50) -> "*" else " "), Text := (G_K > 2, Row-4>0 -> string_from_ASCII(Row-5+first_ASCII("a")) else Row-4) ), text_out(Star+Text+". ", pos(G_Zoom*50, G_Zoom*(Y+Row*70))), color_text(_, rgb(0,0,0)), X := 120, I := 0, for(Col, 0, 7), (Col mod 3 =:= 2 -> color_text(_, rgb(0,0,255)), (Row1 =:= 0, results0(4*Row) =:= G_Max1 -> color_text_back(_, rgb(255,255,0)), text_out(" "+results0(4*Row+2*I)+" ", pos(G_Zoom*X, G_Zoom*(Y+Row*70))), color_text_back(_, rgb(255,255,255)) else (Row1 =:= 4, results0(4*Row) =< G_Max2 -> (results0(4*Row) =:= G_Max2 -> color_text_back(_, rgb(255,255,0)) else color_text_back(_, rgb(255,128,128)) ), text_out(" "+results0(4*Row+2*I)+" ", pos(G_Zoom*X, G_Zoom*(Y+Row*70))), color_text_back(_, rgb(255,255,255)) else text_out(" "+results0(4*Row+2*I)+" ", pos(G_Zoom*X, G_Zoom*(Y+Row*70))) )), color_text(_, rgb(0,0,0)), I := 1 else (Col =:= 6 -> Collision1 := min(results0(4*Row + 0)+results0(4*Row + 1), results0(4*Row + 1)), Collision2 := min(results0(4*Row + 2)+results0(4*Row + 3), results0(4*Row + 3)), (Collision1+Collision2 > 0 -> Text := Collision1+"+"+Collision2+"="+(Collision1+Collision2) else Text := "." ), color_text(_, rgb(255,0,0)), text_out(Text, pos(G_Zoom*X, G_Zoom*(Y+Row*70))) else (Col =:= 7 -> (Row1 =:= 0, results0(4*Row) =:= G_Max1 -> color_text(_, rgb(0,0,255)), Min1 := min_integer, Text := "", create_probability(Min1, _, _, _, Row, Text, paint), text_out(Text, pos(G_Zoom*X, G_Zoom*(Y+Row*70))) else (Row1 =:= 4 -> color_text(_, rgb(255,0,0)), text_out(2**(Row-4)+". ", pos(G_Zoom*X, G_Zoom*(Y+Row*70))), color_text(_, rgb(0,0,255)), (results0(4*Row) >= G_Max2 -> (results0(4*Row) > G_Max2 -> Text := "one" else (results0(4*Row+2) =:= 0 -> Text := "one" else (results0(4*Row+2) < 0 -> Text := "0.25" else Text := "one" ))) else Text := " 0 " ), (G_Incorrect /\ 2**(Row-4) =\= 0 -> color_text_back(_, rgb(255,128,128)), text_out(Text), color_text_back(_, rgb(255,255,255)) else text_out(Text) ) )) else ((Col - I) mod 2 =:= 0 -> Result0 := results0(4*Row + Col - I) + results0(4*Row + Col - I + 1) else Result0 := results0(4*Row + Col - I) ), Text := (Result0 =\= 0 -> Result0 else "."), Result := results(32*G_Sel+4*Row + Col - I), (Result =\= 0 -> Text := Text+" ("+Result+")"), text_out(Text, pos(G_Zoom*X, G_Zoom*(Y+Row*70))) ))), X := X + (Col mod 3 > 0 -> 100 else 150), fail. make_table(Y, Row1). create_incorrect:- for( Action, 0, G_K-1), (results0(4*(Action+4)) < G_Max2 -> G_Incorrect := G_Incorrect \/ 2**Action else (results0(4*(Action+4)) =:= G_Max2, results0(4*(Action+4) + 2) < 0 -> (random(20) < 5 -> G_Incorrect := G_Incorrect \/ 2**Action) )), fail. create_incorrect. find_possible:- for(Action, 0, G_K - 1), results0(4*(Action+4)) =:= G_Max2, G_Incorrect := 2**G_K - 1 - 2**Action. find_correct(Action):- Action := random(G_K), (G_Incorrect /\ 2**Action =\= 0 -> find_correct(Action) ). create_probability(Min1, Min2, Number, NumberMin2, Row, Text, _):- Number := 0, Min2 := max_integer, for(I, 0, G_K-1), results0(4*I) =:= G_Max1, results0(4*I+2) > Min1, Number := Number + 1, (Min2 > results0(4*I + 2) -> NumberMin2 := 1, Min2 := results0(4*I + 2) else (Min2 =:= results0(4*I + 2) -> NumberMin2 := NumberMin2 + 1 )), fail. create_probability(_, Min2, Number, NumberMin2, Row, Text, paint):- (Min2 < results0(4*Row + 2) -> Text := Text + "("+(2*Number-NumberMin2)+"/"+2*Number+")*", create_probability(Min2, _, _, _, Row, Text, paint) else (Number =\= NumberMin2 -> Text := Text + "(1/"+2*Number+")" else (Number =\= 1 -> Text := Text + "(1/"+Number+")" else Text := Text + "one" ))). create_probability(_, Min2, Number, NumberMin2, _, Value, calc):- (Number =:= NumberMin2 -> ItsNumber := (G_with_Randomness =:= 0 -> 0 else random(Number)), find_its_number(Min2, ItsNumber, Value) else ItsNumber := (G_with_Randomness =:= 0 -> 2*Number-1 else random(2*Number)), (ItsNumber < NumberMin2 -> find_its_number(Min2, ItsNumber, Value) else create_probability(Min2, _, _, _, _, Value, calc) ) ). find_its_number(Min1, ItsNumber, Value):- Number := 0, for(I, 0, G_K-1), results0(4*I) =:= G_Max1, results0(4*I+2) =:= Min1, Number := Number + 1, ItsNumber < Number, Value := I. make_line(Col, X):- makeColor(_, Col, Color), pen(10, Color), line(G_Zoom*X, G_Zoom*130, G_Zoom*(X+130), G_Zoom*130). draw_trace(I, X, Y, R, Num, X1, Y1):- State:=states(32*G_Sel+4*I+Num), makeColor(_, State, Color), pen(10, Color), line(X-R+X1,Y+Y1,X+R-X1,Y+Y1). print_event(Event, Count):- Cell := events(80*G_Sel+10*Event+Count), (Cell =\= max_integer -> print_event(Event, Count+1), PosCell := Cell//2, Atom := PosCell//G_K//6, Type := (PosCell//G_K) mod 6, (Count=:=0 -> (Event>3 -> text_out(" => ")), color_text(_, rgb(0,0,255)) ), Value := PosCell mod G_K, (G_K > 2, Value > 0, Type =\= 3 -> Value := string_from_ASCII(Value-1+first_ASCII((Type =:= 2 -> "x" else "a"))) ), (Type =:= 1 -> text_out("a" + Atom + (Cell mod 2 =:= 0 -> "*" else "#") + Value) else (Type =:= 3 -> text_out("m" + Atom + (Cell mod 2 =:= 0 -> "=" else "=\\=") + Value) else (Type =:= 4 -> names(Atom, _, _, Name, _), text_out((Cell mod 2 =:= 0 -> "" else "!") + "ob(" + Name + ")") else text_out((Type =:= 2 -> "v" else "a") + Atom + (Cell mod 2 =:= 0 -> "=" else "=\\=") + Value) ))), (Count>0, not(Count =:= 1 , Event>3) -> text_out(" & ") ) ). event_is_true(Model, Event, Count, Delay):- Cell := events(80*Model+10*Event+Count), (Cell =\= max_integer -> PosCell := Cell//2, Atom := PosCell//G_K//6, Type := (PosCell//G_K) mod 6, Value := PosCell mod G_K, (Type =:= 1 -> Res := true_value(buf_incorrect((G_Time+Delay-Atom) mod 50) /\ 2**(PosCell mod G_K) =:= 0) else (Type =:= 3 -> Res := true_value(active_previous(Atom)=:=Value) else (Type =:= 4 -> Res := true_value(observable(Atom)) else Buffer := (Type =:= 0 -> buf_actions else buf_views), Res := true_value(Buffer((G_Time+Delay-Atom) mod 50) =:= PosCell mod G_K) ))), (Res + Cell) mod 2 =:= 1, event_is_true(Model, Event, Count+1, Delay) ). events_are_true(Model, Events, Views):- Events := 0, for(Event, 0, all_arrows(Model)-1), Cell := events(80*Model+10*Event), (Cell =:= max_integer -> (Views =:= -1 -> Events := Events \/ 2**Event) else (Views*((Cell//G_K//2) mod 6 - 1.5) > 0 , event_is_true(Model, Event, 0, 0) -> Events := Events \/ 2**Event) ), fail. events_are_true(Model, Events, Views). show_line(Model, Title, Y, Buffer):- color_text(_, rgb(0,0,0)), text_out(" "+Title+" -> ", pos(0,G_Zoom*(750+Y))), for(N, 1, 50), ((N+G_Time) mod 3 =:=2 -> color_text(_, rgb(255,0,0))), ((N+G_Time) mod 3 =\=2 -> color_text(_, rgb(0,0,255))), Value := Buffer(50*Model + (N+G_Time) mod 50), (Value=:=0 -> text_out(".") else (Y=\=0, Y=\=80 -> text_out(hex_string(Value)) else (G_K =:= 2 -> text_out("*") else (Y=:=0 -> text_out(string_from_ASCII(Value-1+first_ASCII("x"))) else text_out(string_from_ASCII(Value-1+first_ASCII("a"))) )))), fail. op( 700, xfx, ':=='). arrow(I, J, R, Event):- calcCoords(X1,Y1,I), calcCoords(X2,Y2,J), D:= sqrt((X1-X2)**2+(Y1-Y2)**2), [A,B]:==[X1-X2,Y1-Y2]/D, Sign := (X1>X2; X1=X2, Y11 else -1), [X1,Y1]:==[X1,Y1]+(Sign*10*(Event-1.5))*[B,-1*A], [X2,Y2]:==[X2,Y2]+(Sign*10*(Event-1.5))*[B,-1*A], [X3,Y3]:==[X2,Y2]+R*[A,B], [X4,Y4]:==[X2,Y2]+(R+30*G_Zoom)*[A,B], [X4a,Y4a]:==[X4,Y4]+10*G_Zoom*[-1*B,A], [X4b,Y4b]:==[X4,Y4]+10*G_Zoom*[B,-1*A], makeColor(Event, _, Color), (I=:=active_last(2*G_Sel+0), J=:=active_last(2*G_Sel+1), 2**Event/\events_last(2*G_Sel+0) =\= 0 ; I=:=active_last(2*G_Sel+1), J=:=active(G_Sel), 2**Event/\events_last(2*G_Sel+1) =\= 0 -> pen(10, Color) else pen(3, Color) ), line(X4a,Y4a,X3,Y3,X4b,Y4b), line(X1,Y1,X3,Y3). calcCoords(X,Y,I):- X := G_Zoom*(300 - 200*cos(I*pi/4)), Y := G_Zoom*(400 - 200*sin(I*pi/4)). makeColor(0, 0, rgb(0,0,0)). % arrow, trace makeColor(1, 1, Color):- makeColor2(Color, 255,0,0). makeColor(2, 2, Color):- makeColor2(Color, 0,0,255). makeColor(3, 4, Color):- makeColor2(Color, 0,128,0). makeColor(4, 3, Color):- makeColor2(Color, 255,255,0). makeColor(5, 5, Color):- makeColor2(Color, 0,255,255). makeColor2(Color, R, G, B):- (observable(G_Sel) -> Color := rgb(R, G, B) else Color := rgb(R//2, G//2, B//2) ). calc_observation(Action):- for( Model, 0, G_Models-1), observable(Model), calc_observation2(Model, Action). calc_observation(Action):- for(Value, 4*Action, 4*Action+G_K-1), for(I, 0, 1), New := 0, New1 := 0, add_results(New, New1, Value, I), results0(4*Value+2*I) := New, results0(4*Value+2*I+1) := New1, (Action =:= 0 -> (G_Max1 < New, I =:= 0 -> G_Max1 := New) else (G_Max2 < New, I =:= 0 -> G_Max2 := (New < 0 -> New else 0) ) ), fail. calc_observation(Action). calc_observation2(Model, Action):- for(Event, 0, all_traces(Model)-1), State := states(32*Model+4*active(Model) + Event) - 1, State < 4, PosCell := events(80*Model+10*(Event+4))//2, ((PosCell//G_K) mod 6) - 1 =\= Action, Value := 4*Action + PosCell mod G_K, (event_is_true(Model, Event+4, 1, 0)-> results(32*Model+4*Value+State) := results(32*Model+4*Value+State) + 1, buf_the_trace(50*Model + G_Time mod 50) := buf_the_trace(50*Model + G_Time mod 50) \/ 2**Event ), fail. add_results(New, New1, Value, I):- for( Model, 0, G_Models-1), New := New + results(32*Model+4*Value+2*I) - results(32*Model+4*Value+2*I+1), New1 := New1 + results(32*Model+4*Value+2*I+1), fail. add_results(New, New1, Value, I). clean_buffers:- for(I, 0, 16-1), active(I):=0, active_previous(I):=0, time_last(I):=0, used_states(I):=1, fail. clean_buffers:- for(I, 0, 16*2-1), active_last(I):=0, events_last(I):=0, fail. clean_buffers:- for(I, 0, 16*8*8-1), links(I):=0, fail. clean_buffers:- for(I, 0, 16*8*4-1), states(I):=0, fail. clean_buffers:- for(I, 0, 50-1), buf_views(I):=0, buf_incorrect(I):=0, buf_actions(I):=0, fail. clean_buffers:- for(I, 0, 16*50-1), buf_arrows(I):=0, buf_the_trace(I):=0, fail. clean_buffers:- for(I, 0, 16*8*10-1), events(I):=max_integer, fail. clean_buffers:- for(I, 0, 66-1), board(I):=0, fail. clean_buffers. clean_results:- for(I, 0, 16*8*4-1), results(I):=0, fail. clean_results:- for(Model, 0, 16-1), active_previous(Model) := active(Model), buf_the_trace(50*Model + G_Time mod 50) := 0, fail. clean_results. init_events(Model, Event, Start, AllEvents):- (Event < AllEvents -> init_one_event(Model, Event, Start), (not(exist_the_same(Model, Event, Start))-> Event := Event + 1 ), init_events(Model, Event, Start, AllEvents) ). init_one_event(Model, Event, Start):- (Start =:= 0 -> random(4)=\=0, % make the event "True" First := random(3), FirstVal := (G_K =:= 2, First =\= 1 -> 2*random(2) else random(2*G_K)) else First := 2, FirstVal := (G_K =:= 2, First =\= 1 -> 2 else 2*random(G_K)) ), event_tail(Model, Event + Start, First, FirstVal, 0). init_one_event(Model, Event, Start). event_tail(Model, Event, Last, LastVal, Count):- events(80*Model+10*Event+Count) := 2*G_K*Last + LastVal, Count < 2, random(2)=:=0, Next := Last + 6, % increase 3 to look further in the past NextVal := (G_K =:= 2, Next mod 3 =\= 1 -> 2*random(2) else random(2*G_K)), event_tail(Model, Event, Next, NextVal, Count+1). exist_the_same(Model, This, Start):- for(Event, 0, This - 1), the_same_event(Model, This + Start, Event + Start, 0). the_same_event(Model, E1, E2, Count):- events(80*Model+10*E1+Count) =:= events(80*Model+10*E2+Count), (events(80*Model+10*E1+Count) =:= max_integer; the_same_event(Model, E1, E2, Count + 1)). init_Links(Model):- G_Used_Arrows := 0, G_Used_Traces := 1, states(32*Model+0) := random(4)+1, % for more trace states(32*Model+1) := 5, states(32*Model+2) := 5, states(32*Model+3) := 5, for(I, 1, circuits(Model)), circuit(Model, 0). init_Links(Model):- all_arrows(Model) := G_Used_Arrows, all_traces(Model) := G_Used_Traces. circuit(Model, State):- State2 := random(7), (State=:=State2; State2 >= used_states(Model) -> (used_states(Model) < 8 -> State2 := used_states(Model), used_states(Model) := used_states(Model) + 1 else State2 := 7 ) ), Event := random(min(G_Used_Arrows+1, all_arrows(Model))), (Event =:= G_Used_Arrows -> G_Used_Arrows := G_Used_Arrows+1 ), links(64*Model+8*State+State2) := links(64*Model+8*State+State2)\/2**Event, (states(32*Model+4*State)=:=0 -> make_trace(Model, State, 0), make_trace(Model, State, 1), make_trace(Model, State, 2), make_trace(Model, State, 3) ), State2 =\= 0, circuit(Model, State2). make_trace(Model, State, Event):- (Event > G_Used_Traces; Event >= all_traces(Model) -> Trace := 5 else Trace := min(random(7), 4)+1, (Trace<5 -> /**/ Q := events(80*Model+10*(Event+4)) mod (G_K*2), (Trace mod 2 =:= 0 -> (Q =\= 0 -> Trace := Trace - 1) else (Q =:= 0 -> Trace := Trace + 1 )), /**/ (Event =:= G_Used_Traces -> G_Used_Traces := G_Used_Traces+1 ) ) ), states(32*Model+4*State+Event):= Trace. [] :== []/S. [H|T] :== [H1|T1]/S :- H:= H1/S, T:== T1/S. [] :== []+S*[]. [H|T] :== [H1|T1]+S*[H2|T2] :- H:=H1+S*H2, T:==T1+S*T2.