1:-module(logicmoo_hyhtn_code,[]).
14:-multifile(user:push_env_ctx/0). 15:-dynamic(user:push_env_ctx/0). 16
21:- style_check(-singleton). 22:- use_module(library(prolog_pack)). 23:- if( \+ prolog_pack:current_pack(logicmoo_planners)). 24:- dynamic user:file_search_path/2. 25:- multifile user:file_search_path/2. 26:- prolog_load_context(directory,Dir),
27 DirFor = planner,
28 (( \+ user:file_search_path(DirFor,Dir)) ->asserta(user:file_search_path(DirFor,Dir));true),
29 absolute_file_name('../../../../',Y,[relative_to(Dir),file_type(directory)]),
30 (( \+ user:file_search_path(pack,Y)) ->asserta(user:file_search_path(pack,Y));true). 31:- attach_packs. 32:- initialization(attach_packs). 33:- endif. 34
36:- ensure_loaded(library(logicmoo_utils)). 37
38
39:- ensure_loaded(library(logicmoo_util_structs)). 40:- ensure_loaded(library(logicmoo_util_bb_env)). 41:- prolog_load_context(file,File),ain(user:env_source_file(File)). 42
43:-op(500,fx,env_call). 44
67 72:-dynamic my_stats/1.
73
74
75:-multifile(on_call_decl_hyhtn/0). 76:-export(on_call_decl_hyhtn/0). 78on_call_decl_hyhtn :- decl_env_mpred_dom([kb(dom,tasks),stubType(dyn)], ( htn_task/3, planner_task/3, planner_task_slow/3 )).
79
80on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(temp_assertIndivConds/1)). 81on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(is_of_primitive_sort/2, is_of_sort/2)).
82on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(methodC/7, opParent/6,operatorC/5,gOperator/3)).
83on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(objectsC/2,objectsD/2,atomic_invariantsC/1)). 84on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache)],(objectsOfSort/2)). 85on_call_decl_hyhtn :- decl_env_mpred_dom([stubType(dyn),kb(dom,cache) ,(related_op/2, gsubstate_classes/3, gsstates/3)).
86
87on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(initial_state/1)).
88on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache) ,(op_score/2)).
89on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache),(node/5,final_node/1)).
90on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tp_goal/3,closed_node/6,solved_node/2, goal_related_search/1)).
92on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(goal_related/3)).
93on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(current_num/2)).
94on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tn/6)). 95on_call_decl_hyhtn :- decl_env_mpred_task([stubType(dyn),kb(node,cache)],(tp_node/6)).
98
100on_call_decl_hyhtn :- decl_env_mpred_dom([kb(dom,tasks),stubType(dyn)], ( htn_task/3, planner_task/3, planner_task_slow/3 )).
101
103on_call_decl_hyhtn :-
104 decl_env_mpred_dom([kb(dom,file),stubType(dyn)],[domain_name/1,sorts/2,substate_classes/3,objects/2,predicates/1,inconsistent_constraint/1,atomic_invariants/1,
105 implied_invariant/2,operator/4,
106 107 method/6]).
108
109:-export(call_decl_hyhtn/0). 110
112
113call_decl_hyhtn:-must(doall(on_call_decl_hyhtn)).
114
115:- 116 call_decl_hyhtn. 117
118
119
123
124
125 :- rtrace. 126tryff(Call):- predicate_property(Call,_),!,once(tryf((Call,assert(passed_test_try(Call))))),fail.
127tryf(Call):- predicate_property(Call,_),!,catch(Call,E,dmsg(E = Call)).
128trye(Call):- catch(Call,E,((dmsg(error(E , Call)),throw(trace),Call))).
129
130:-dynamic(passed_test_try/1). 131:-dynamic(testing_already/0). 132
133check_passed_any:-not(not(passed_test_try(_))),nl,listing(passed_test_try/1).
134
135ttm:-retractall(passed_test_try(_)),fail.
136ttm:-testing_already,!.
137ttm:-asserta(testing_already), make, retractall(testing_already),fail.
138
139
140:-export(banner_party/2). 141banner_party(E,BANNER):-
142 ansicall(yellow,(
143 format("% xxxxxxxxxxxxxxx ~w xxxxxxxxxxxxxxxxxxx~n",[E]),
144 forall(t_l:doing(X),dmsg(E,doing(X))),
145 dmsg5(E,BANNER),
146 format("% xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx~n",[]))).
147
148
149subst_eq_h(A,B,C,D):- nd_subst(A,B,C,D),!.
150subst_eq_h(A,B,C,D):- throw(trace), nd_subst(A,B,C,D),!.
151
152
153:-export(term_expansion_alias/2). 154term_expansion_alias(In,Out):-term_expansion_alias([],In,Out).
155term_expansion_alias(Not,In,Out):-term_alias(I,O),not(member(I,Not)),subst_eq_h(In,I,O,M), In\=@=M,!, term_expansion_alias([I|Not],M,Out).
156term_expansion_alias(_Not,InOut,InOut).
157term_alias(cond,se).
158term_alias(se,se).
159term_alias(state,ss).
160term_alias(trans,sc).
161term_alias(ne,dif).
162term_alias(neq,dif).
164term_alias(startOcl,start).
165term_alias(startOCL,start).
166
167
170
171
172tasks:-
173 Call = get_tasks(N,Goal,State),
174 setof(Call,Call,List),list_to_set(List,Set),!,
175 env_info(kb(dom,file)),!,
176 once((ignore(forall(member(Call,Set),
177 w_tl(t_l:doing(tasks(N,Goal)),
178 ((
179 must(nonvar(Goal)),must(nonvar(State)),
180 ignore(N=Goal),
181 must((term_expansion_alias((Goal:-State),(GGoal:-GState)))),
182 must(nonvar(GGoal)),must(nonvar(GState)),
183 banner_party(informational,('GOAL'(N):-GGoal)),
184 must(((once( once(startOCL(GGoal,GState))*->banner_party(informational,'SUCCESSS');banner_party(error,'FAILUR!!!!!!!!!!!!!!!!!!!!!!!!!!!E')))))))))))).
185
186t:- once(run_header_tests).
187
188tt:-catch(ttm,E,dmsg(E)),!.
189
190tt:-tryff((tasks)).
191tt:-tryff((task1)).
192tt:-tryff((task2)).
193tt:-tryff((task3)).
194tt:-tryff((task4)).
195tt:-tryff((task5)).
196tt:-tryff((task6)).
197tt:-tryff((task7)).
198tt:-tryff((task8)).
199tt:-tryff((task9)).
200tt:-tryff((task10)).
201tt:-tryff((task11)).
202tt:-tryff((task22)).
203tt:-tryff((task33)).
204tt:-tryff((task44)).
205tt:-check_passed_any,!.
206
207
208tdom:-tdom([htn1,ty,ocl1,r3]).
209tdom2:-tdom([ocl2,htn2,htn3,htn4]).
210
211tdomfile(F):-tdomcall(load_data_file(F)).
212tdomcall(Call):- trye(once((env_clear_doms_and_tasks,trye(Call),tt))).
213
214tdom([]):-!.
215tdom([H|T]):- !, tdom(H),tdom(T).
216tdom(H):- predicate_property(H,_),!, throw(trace),call(H).
217tdom(F):- tdom1(F),!.
218tdom(F):- expand_file_name(F,List),List\=[],!,tdom1(List).
219tdom(F):- throw(tdom(F)).
220
221tdom1([]):-!.
222tdom1([H|T]):- !, tdom(H),tdom(T).
223tdom1(F):- tdom2(F),!.
224tdom1(FIn):- atom(FIn),tdom2(FIn).
225
226tdom2(FIn):- tdom3(FIn),!.
227tdom2(FIn):- atom_concat('domains_ocl/',FIn,F), tdom3(F),!.
228
229tdom3(FIn):- tdom4(FIn).
230tdom3(FIn):- atom_concat(FIn,'htn',F),tdom4(F).
231tdom3(FIn):- atom_concat(FIn,'ocl',F),tdom4(F).
232
233tdom4(F):- exists_file(F),!,tdomfile(F).
234
235
236:- discontiguous(post_header_hook/0).:-retractall(canDoTermExp).
240post_header_hook:-asserta(canDoTermExp).
241
242:-export(env_clear_doms_and_tasks/0). 243env_clear_doms_and_tasks:- env_clear(kb(dom,file)),env_clear(kb(dom,tasks)),env_clear(kb(dom,cache)),!.
244
245:- op(100,xfy,( cl:('=>'))). 246
248post_header_hook:-set_prolog_flag(verbose_load,full).
249post_header_hook:-use_module(library(lists)).
250
252:- style_check(-singleton). 253:- style_check(+discontiguous). 254
255
257:-if(exists_source(library(gui_tracer))). 258post_header_hook:- user:use_module(library(gui_tracer)),catch(guitracer,_,true).
259:-endif.:- !.
261post_header:- dmsg(post_header),fail, forall(clause(post_header_hook,G),G).
262
263
264:- discontiguous(header_tests/0). 265
266run_tests(Call) :-
267 statistics_runtime(InTime),
268 w_tl(doing(run_tests(Call)),
269 call_cleanup(Call,
270 ((
271 statistics_runtime(OutTime),
272 Time is OutTime - InTime,
273 banner_party(informational,runtests(Call) = time(Time)))))).
274
:- run_tests(forall(clause(header_tests,G),run_tests(G))).
276
277
278:-export(test_ocl/1). 279test_ocl(File):- forall(filematch(File,FM),test_ocl0(FM)).
280test_ocl0(File):- time(w_tl(t_l:doing(test_ocl(File)),
281 once((env_clear_doms_and_tasks,clean_problem,l_file(File),tasks)))).
282
:-test_ocl('domains_ocl/*.ocl').
284
285
286:- style_check(-singleton). 287:- style_check(-discontiguous). 289
291
292
301
302planner_failure(Why,Info):-dmsg(error,Why-Info),banner_party(error,'FAILURE_PLANNER'),print_message(error,'FAILURE_PLANNER'(Why,Info)),!. 303
304:-thread_local t_l:doing/1. 305
306statistics_runtime(CP):-statistics(runtime,[_,CP0]), (CP0==0 -> CP= 0.0000000000001 ; CP is (CP0/1000)) . 307statistics_walltime(CP):-statistics(walltime,[_,CP0]), (CP0==0 -> CP= 0.0000000000001 ; CP is (CP0/1000)) . 308
309
310
322startOCL(Goal,Init):-
323 clean_problem,
324 dmsg('OCL-PLANNER-TASK'(Goal)),
325 must(planner_interface(Goal,Init,Sol,_,TNLst)),
326 show_result_and_clean(F,Id,Sol,TNLst).
327
328
329
331:-dynamic kill_file/1,solution_file/1. 334:- style_check(-singleton). 338:- op(100,xfy,'=>'). 351
352incr_op_num:- flag(op_num,X,X+1).
353set_op_num(X):-flag(op_num,_,X).
354current_op_num(X):- flag(op_num,X,X).
355
356
357get_tasks(N,Goal,State):- ocl:htn_task(N,Goal,State).
358get_tasks(N,Goal,State):- env_call planner_task(N,Goal,State).
360
361:- set_op_num(0). 362:-asserta(my_stats(0)). 363
364l_file(File):- \+ exists_file(File),!,forall(filematch(File,FM),l_file(FM)).
365l_file(F):- env_consult(ocl:F).
366l_file(F):-
367 if_defined(cl:force_reload_mpred_file(F),
368 if_defined(cl:with_mpred_consult(cl:consult(F)),cl:consult(F))).
369
370solve_file(F):-with_filematch(l_file(wfm(F))), doall(solve(_)).
371solve:- solve_file(test_hyhtn).
372solve(Id) :-
373 no_repeats(get_tasks(Id,Goal,Init)),
374 planner_interface(Goal,Init,Sol,_,TNLst),
375 show_result_and_clean(user,Id,Sol,TNLst).
376
377show_result_and_clean(F,Id,Sol,TNLst):-
378 must(solution_file(F)),
379 tell(F),
380 format('~NSOLUTION for TASK ~w~n',[Id]),
381 display_sol(Sol),
382 write_out_test_data('.preClean',Id),
383 write('END FILE'),nl,nl,
384 nop((reverse(TNLst,TNForward),
385 display_details(TNForward),
386 write('END PLANNER RESULT'))),
387 told,
388 clean_problem.
389
390write_out_test_data(_MoreID,_Id):-!.
391write_out_test_data(MoreID,Id):-
392 must((
393
394 (var(Id)->statistics(walltime,[Id,_]);true),
395 (push_env_ctx-> A= pushed_ ; A=nonpushed_ ),
396 atom_concat(A,Id,TN),atom_concat(TN,MoreID,TTN),
397 lws(TTN))),!.
398
399display_sol([]).
400display_sol([H|T]) :-
401 write(H),
402 nl,
403 display_sol(T).
404
405display_details([]):-!.
406display_details([H|T]):-!,display_details(H),display_details(T),!.
407display_details(tn(TN,Name,Pre,Post,Temp,Dec)):-
409 nl,write('BEGIN METHOD'),nl,write(TN),write(';'),
410 nl,write('Name:'),write(Name),write(';'),
411 nl,write('Pre-condition:'),write(Pre),write(';'),
413 nl,write('Index Transitions:'),write('=>'),write(Post),write(';'),
415 nl,write('Temporal Constraints:'),write(Temp),write(';'),
416 nl,write('Decomposition:'),write(Dec),write(';'),
417 nl.
418
419display_details(H):-dmsg((display_details:-H)).
420
421clean_problem:-
422 env_retractall(temp_assertIndivConds(_)),
423 env_retractall(opParent(_,_,_,_,_,_)),
424 env_retractall(operatorC(_,_,_,_,_)),
425 env_retractall(initial_state(_)),
426 env_retractall(objectsOfSort(_,_)),
427 env_retractall(objectsD(_,_)),
428 env_retractall(objectsC(_,_)),
429 env_retractall(methodC(_,_,_,_,_,_,_)),
430 env_retractall(is_of_sort(_,_)),
431 env_retractall(is_of_primitive_sort(_,_)),
432 env_retractall(gsubstate_classes(_,_,_)),
433 env_retractall(gOperator(_,_,_)),
434 env_retractall(current_num(_,_)),
435 env_retractall(atomic_invariantsC(_)),
436
437 438 env_retractall(final_node(_)),
439 440 nb_setval(tnodeSORTED,[]),
441 env_retractall(tp_node(_,_,_,_,_,_)),
442 env_retractall(current_num(tp,_)),
443 env_retractall(node(_,_,_,_,_)),
444 env_retractall(solved_node(_,_)),
445 env_retractall(tn(_,_,_,_,_,_)),
446 env_retractall(closed_node(_,_,_,_,_,_)),
447 448 449 env_retractall(op_score(_,_)),
450 env_retractall(goal_related(_,_,_)),
451 env_retractall(goal_related_search(_)),
452
453
454 455 set_op_num(0).
456
457
458
459get_instance(A1,Obj1):- \+compound(A1),!,Obj1=A1.
460get_instance(A1,Obj1):-functor(A1,_,N),N<3,!,arg(1,A1,Obj1).
461get_instance(A1,Obj1):-arg(2,A1,Obj1).
462
463compare_on_instance(Comp,A1,A2):- get_instance(A1,Obj1),get_instance(A2,Obj2),compare(Comp1,Obj1,Obj2),
464 (Comp1== (=) -> compare(Comp,A1,A2) ; Comp=Comp1).
465
466to_ssify(_,_,goal(S,X,L),goal(S,X,L)):-!.
467to_ssify(Hints,SS,G,GGG):-must_det_l((is_list(G),must(to_ssify0(Hints,SS,G,GG)),predsort(compare_on_instance,GG,GGS),must(merge_ss2s(GGS,GGG)))).
468
469to_ssify0(Hints,SS,G,GG):-must(to_ssify1(Hints,SS,G,GG)).
470to_ssify1(Hints,SS,[],[]):- !.
471to_ssify1(Hints,SS,G,[GG]):- atom(G),!,G1=..[G,self],GG=..[SS,top,self,[G1]].
472to_ssify1(Hints,SS,G,[GG]):- ( \+ compound(G)),!,GG=..[SS,top,self,[beliefsTrue(self,G)]].
473to_ssify1(Hints,_,goal(S,X,L),[goal(S,X,L)]):-!.
474to_ssify1(Hints,SS,[GH|GT],GG):- !,to_ssify0(Hints,SS,GH,L1),to_ssify0(Hints,SS,GT,L2),append(L1,L2,GG).
475to_ssify1(Hints,SS,ss(S,X,L),GG):- !, (length(L,1)->GG=[ss(S,X,L)];(append(Hints,L,NewHints),to_ssify0(NewHints,SS,L,GG))).
476to_ssify1(Hints,SS,se(S,X,L),GG):- !, (length(L,1)->GG=[se(S,X,L)];(append(Hints,L,NewHints),to_ssify0(NewHints,SS,L,GG))).
477to_ssify1(Hints,SS,scL(S,X,L),GG):- !, append(Hints,[is_of_sort(X,S)|L],NewHints),to_ssify0(NewHints,scL,L,GG).
478to_ssify1(Hints,SS,scR(S,X,L),GG):- !, append(Hints,[is_of_sort(X,S)|L],NewHints),to_ssify0(NewHints,scR,L,GG).
479to_ssify1(Hints,SS,sc(S,X,Neg=>Pos),GG):-!,
480 must_det_l(( to_ssify0(Hints,SS,scL(S,X,Neg),L1),to_ssify0(Hints,SS,scR(S,X,Pos),L2),
481 append(L1,L2,GG))).
482to_ssify1(Hints,SS,G,[GG]):-get_instance(G,X),must_det_l((get_one_isa(S,X,Hints),GG=..[SS,S,X,[G]])).
483
484merge_ss2s([],[]).
485merge_ss2s([G1,G2|GG],GGG):- must(\+ is_list(G1)),
486 once(must_det_l((G1=..[SS1,S1,Obj1,L1],G2=..[SS2,S2,Obj2,L2]))),Obj1==Obj2,
487 merge_ss(SS1,S1,Obj1,L1,SS2,S2,Obj2,L2,G),merge_ss2s([G|GG],GGG),!.
488merge_ss2s([G|GG],[G|GGG]):-
489 merge_ss2s(GG,GGG).
490
491
492need_ss_swap(scR,scL).
493need_ss_swap(scR,sc).
494need_ss_swap(scL,sc).
495merge_ss(SS1,S1,Obj1,L1,SS2,S2,Obj2,L2,G):- need_ss_swap(SS1,SS2),!,merge_ss(SS2,S2,Obj2,L2,SS1,S1,Obj1,L1,G).
496
497merge_ss(scL,S1,Obj1,L1,scR,S2,_Obj2,L2,sc(S1,Obj1,L1=>L2)):-!.
498merge_ss(sc,S1,Obj1,[L=>R],scL,S2,_Obj2,[L2],sc(S1,Obj1,LL=>R)):-append(L,L2,LL).
499merge_ss(sc,S1,Obj1,[L=>R],scR,S2,_Obj2,[L2],sc(S1,Obj1,L=>RR)):-append(R,L2,RR).
500merge_ss(sc,S1,Obj1,[L1=>R1],sc,S2,_Obj2,[L2=>R2],sc(S1,Obj1,LL=>RR)):-append(L1,L2,LL),append(R1,R2,RR).
502merge_ss(SS,S1,Obj1,L1,SS,S1,Obj2,L2,G):-append(L1,L2,LL),G=..[SS,S1,Obj1,LL].
503
504
505init_locol_planner_interface(G,I,Node):-
506 w_tl(t_l:db_spy,
507 init_locol_planner_interface0(G,I,Node)).
508
509assert_itital_state(I0):- is_list(I0),!, must_maplist(assert_itital_state,I0),!.
510assert_itital_state(sc(S,X,List)):- !,must(assert_itital_state(List)),must(assert_itital_state(is_of_sort(X,S))),!.
511assert_itital_state(ss(S,X,List)):- !,must(assert_itital_state(List)),must(assert_itital_state(is_of_sort(X,S))),!.
512assert_itital_state(is_of_sort(X,S)):- ground(X:S),env_assert(is_of_sort(X,S)).
513assert_itital_state(I0):- env_assert(initial_state(I0)).
514
515get_inital_state_preds0(G,[]):- (\+compound(G)),!.
516get_inital_state_preds0([],[]):-!.
517get_inital_state_preds0([G1|G2],L):- !,get_inital_state_preds0(G1,L1),get_inital_state_preds0(G2,L2),append(L1,L2,L).
518get_inital_state_preds0(ss(_,_,G),L):- !,get_inital_state_preds0(G,L).
519get_inital_state_preds0(sc(_,_,G),L):- !,get_inital_state_preds0(G,L).
520get_inital_state_preds0(se(_,_,G),L):- !,get_inital_state_preds0(G,L).
521get_inital_state_preds0(G,L):- G=..[F,_,_|GL], arg(_,v(ss,sc,se),F),!,get_inital_state_preds0(GL,L).
522get_inital_state_preds0(G,L):- G=..[F,_|GL], arg(_,v(operator,method,substate_classes,se),F),!,get_inital_state_preds0(GL,L).
523get_inital_state_preds0(G,L):- G=..[F|GL], arg(_,v(goal,initial_state,implied_invariant,inconsistent_constraint,(=>),achieve,predicates),F),!,get_inital_state_preds0(GL,L).
524get_inital_state_preds0(G,[F/A]):-functor(G,F,A).
525
526get_inital_state_preds(Hints,L):-
527 findall(G1,
528 (member(F/A,[initial_state/1,operator/4,method/6,substate_classes/3,
529 predicates/1,implied_invariant/2,inconsistent_constraint/1]),
530 functor(G1,F,A),
531 env_call(G1)),LS),
532 get_inital_state_preds0([Hints|LS],UL),list_to_set(UL,L).
533
534
535env_call_if_defined(P):-catch((env_call P),_,fail).
536
537get_inital_state(G,I0):-
538 get_inital_state_preds(G,SP),
539 findall(S,(member(F/A,SP),functor(S,F,A),(if_defined(S,if_defined(ocl:S));env_call(initial_state(S)))),EL),
540 must(to_ssify(EL,ss,EL,I0)).
541
542
543:-export(init_locol_planner_interface0/3). 544
546init_locol_planner_interface0(G0,I0,Node):-
547 must_maplist(assert_itital_state,I0),
548 must(init_locol_planner_interface1(G0,Node)).
549
550init_locol_planner_interface1(G0,Node):-
551 must_det_l((get_inital_state(G0,I0),init_locol_planner_interface2(G0,I0,Node))).
552
553init_locol_planner_interface2(G0,I0,Node):-
554 must_det_l((
555 flatten([G0,I0],Hints),
556 to_ssify(Hints,se,G0,G),
557 to_ssify(Hints,ss,I0,I),
558 wdmsg((goals:-G)),
559 wdmsg((input:-I)),
560 change_obj_list(I),
561 ground_op,
562 assert_is_of_sort,
563 change_op_representation,
564 prim_substate_class,
565 set_op_num(0),
566 statistics_runtime(Time),
567 statistics_walltime(_),
568 write_out_test_data('.setup',Id),
569 ignore(retract(my_stats(_))),
570 assert(my_stats(Time)),
571 make_problem_into_node(I, G, Node))),!.
572
573
574show_time(must(Call)):-!, functor(Call,F,A), time(must(Call)),format('~N% ~q~n',[previous_statistics_for(F/A)]).
575show_time(Call):- functor(Call,F,A), time(Call),format('~N% ~q~n',[previous_statistics_for(F/A)]).
576
577planner_interface0(G,I, SOLN,OPNUM,TNList):-
578 gripe_time(2.0,show_time(must(init_locol_planner_interface(G,I,Node)))),
579 env_assert(Node),!,
580 dmsg(solving_for(G)),!,
581 start_solve(SOLN,OPNUM,TNList).
582
583
584planner_interface(G,I, SOLN,OPNUM,TNList):-
585 (planner_interface0(G,I, SOLN,OPNUM,TNList)
586 *-> true ;
587 (tell(user),nl,write('failure in initial node'),
588 planner_failure('failure in initial node',planner_interface(G,I, SOLN,NODES,TNList)),!)).
589
593
594
595getN_name(node(Name, _, _, _,_), Name).
596getN_pre(node(_,Pre, _, _, _), Pre).
597getN_decomp(node(_, _, Decomp,_,_), Decomp).
598getH_temp(node(_, _, _,Temps, _), Temps).
599getN_statics(node(_,_,_,_,Statics), Statics).
600
602start_solve(SOLN,OPNUM,_):-
603 kill_file(Kill),
604 exists_file(Kill),
606 !,fail.
607
608start_solve(Sol,OPNUM,TNList):-
609 env_retract(final_node(Node)),
610 env_retractall(current_num(_,_)),
611 getN_statics(Node,Statics),
612 statics_consist(Statics),
613 extract_solution(Node,Sol,SIZE,TNList),
614 statistics_runtime(CP),
615 statistics_walltime(TIM),
616 tell(user),
617 flag(op_num,OPNUM,0),
618 nl, nl,
619 write('CPU Time = '),write(CP),nl,
620 write('WALL Time = '),write(TIM),
621 write(' SECONDS'),nl,
622 write('Solution SIZE = '),write(SIZE),nl,
623 write('Operator Used = '),write(OPNUM),nl,
624 write('***************************************'),
625 assert(time_taken(CP)),
626 assert(soln_size(SIZE)),
627 env_retractall(tn(_,_,_,_,_,_)),
628 !.
629start_solve(Sol,OPNUM,TNList):-start_solve0(Sol,OPNUM,TNList)*->true;start_solve1(Sol,OPNUM,TNList).
630
631start_solve0(Sol,OPNUM,TNList):-
632 select_node(Node),
634 635 process_node(Node),
636 start_solve(Sol,OPNUM,TNList).
637
638start_solve1(Sol,OPNUM,TNList):- solve_failed(Sol,OPNUM,TNList).
639
640solve_failed(Sol,OPNUM,TNList):-
641 tell(user), write('+++ task FAILED +++'),
642 planner_failure('+++ task FAILED +++',start_solve(Sol,OPNUM,TNList)),
643 644 clean_problem.
645
646
647
649
651process_node(Node) :-
652 getN_name(Node, Name),
653 getN_pre(Node, Pre),
654 getN_decomp(Node, Dec),
655 getH_temp(Node, Temps),
656 getN_statics(Node, Statics),
657 expand_decomp(Dec,Pre,Post,Temps,Temp1,Statics,Statics1,Dec1),
658 statics_consist(Statics),
659 assert_node(Name,Pre,Dec1,Temp1,Statics1).
660
661assert_node(Name,Pre,Decomp,Temp,Statics):-
662 all_HP_expanded(Decomp),
663 env_assert(final_node(node(Name,Pre,Decomp,Temp,Statics))),!.
664assert_node(Name,Pre,Dec,Temp,Statics):-
665 gensym_special(root,SYM),
666 env_assert(node(SYM,Pre,Dec,Temp,Statics)),!.
667
668all_HP_expanded([]):-!.
669all_HP_expanded([step(HPid,Name,_,_,exp(TN))|THPS]):-
670 all_HP_expanded(THPS),!.
671
677expand_decomp([],Post,Post,Temp,Temp,Statics,Statics,[]):-!.
678
680expand_decomp([step(HPid,Name,Pre0,Post0,exp(TN))|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(TN))|Decomp1]):-
681 state_achieved(Pre0,Pre),
682 state_change(Pre,Pre0,Post0,State),
683 statics_consist(Statics),
684 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics,Statics1,Decomp1),!.
685
687expand_decomp([step(HPid,ACH,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
688 ACH=..[achieve|_],
689 statics_consist(Statics),
690 expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,
691 Temp,Temp1,Statics,Statics1,Decomp1),!.
692
694expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(Name))|Decomp1]):-
695 apply_op(Name,HPid,Name,Pre,undefd,State,Statics,Statics2),
696 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
697
699apply_op(Name,HPid,Name,Pre,Post,State,Statics,Statics1):-
700 env_call operatorC(Name,Pre0,Post0,Cond,Statics0),
701 statics_append(Statics0,Statics,Statics2),
702 state_achieved(Pre,Pre0,Statics2),
703 state_change(Pre,Pre0,Post0,State2),
704 cond_state_change(State2,Cond,State),
705 all_achieved(Post,Statics2,State),
706 remove_unneed(Statics2,[],Statics1),
707 statics_consist_instance(Statics1),
708 statics_consist_instance(Statics0),
709 incr_op_num,!.
710
713expand_decomp([step(HPid,Name,undefd,undefd,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,Name,Pre,State,exp(TN))|Decomp1]):-
714 apply_method(TN,HPid,Name,Pre,undefd,State,Statics,Statics2),
715 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
716
718apply_method(TN,HPid,Name,Pre,Post,State,Statics,Statics1):-
719 env_call methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
720 statics_append(Statics0,Statics,Statics2),
721 all_achieved(Pre0,Statics2,Pre),
722 remove_unneed(Statics2,[],Statics21),
723 make_dec1(HPid,Pre,ACH0,Statics21,Temp0,Dec0,Temp2,Dec2),
724 expand_decomp(Dec2,Pre,State,Temp2,Temp1,Statics21,Statics1,Dec1),
725 all_achieved(Post,Statics1,State),
726 incr_op_num,
727 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
728
730expand_decomp([step(HP,N,Pre0,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
731 get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
732 HP,Temp,Temp2,Decomp,Decomp2),
733 expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HP,N,Pre0,Post0,unexp)|Decomp2],
734 Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
735
738
740get_another_step(A,Pre,Statics,HP,Temp,Temp1,[],Dec2):-fail.
741get_another_step(step(HP2,Name2,Pre2,Post2,Exp),Pre,Statics,HP,Temp,[before(HP2,HP)|Temp],Dec,Dec2):-
742 member(step(HP2,Name2,Pre2,Post2,Exp),Dec),
743 not(necessarily_before(HP,HP2, Temp)),
744 state_achieved(Pre2,Pre,Statics),
745 list_take(Dec,[step(HP2,Name2,Pre2,Post2,Exp)],Dec2).
746
750expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,Decomp1):-
751 state_achieved(Post0,Pre),
754 remove_temp(Temp,HPid,Temp,Temp2),
755 expand_decomp(Decomp,Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1),!.
756
758expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
759 expand_ach_goal(HPid,TN,ACH,Pre,Post0,State,Statics,Statics2),
760 expand_decomp(Decomp,State,Post,Temp,Temp1,Statics2,Statics1,Decomp1),!.
761
763expand_decomp_ach([step(HPid,ACH,Pre,Post0,unexp)|Decomp],Pre,Post,Temp,Temp1,Statics,Statics1,[step(HPid,ACH,Pre,Post0,exp(TN))|Decomp1]):-
764 get_another_step(step(HP2,N2,Pre2,Post2,Exp),Pre,Statics,
765 HP,Temp,Temp2,Decomp,Decomp2),
766 expand_decomp([step(HP2,N2,Pre2,Post2,Exp),step(HPid,ACH,Pre,Post0,unexp)|Decomp2], Pre,Post,Temp2,Temp1,Statics,Statics1,Decomp1).
767
770
773expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
774 direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1),!.
775
779expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
780 make_tpnodes(Pre,Post,Statics),
782 fwsearch(TN,State),
785 clean_temp_nodes.
786
789
792direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics):-
793 apply_tn(TN,HPid,ACH,Pre,Post,State,Statics,Statics).
795direct_expand_ach_goal(HPid,OP,ACH,Pre,Post,State,Statics,Statics1):-
796 dir_apply_op(OP,HPid,ACH,Pre,Post,State,Statics,Statics1).
799direct_expand_ach_goal(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
800 dir_apply_method(TN,HPid,ACH,Pre,Post,State,Statics,Statics1),!.
802
804apply_tn(Tn0,HPid,ACH,Pre,Post,State,Statics,Statics):-
805 env_call(tn(Tn0,Name,Pre0,Post0,Temp0,Decomp0)),
806 state_achieved(Pre0,Pre),
807 state_change(Pre,Pre0,Post0,State),
808 all_achieved(Post,Statics,State),
810 incr_op_num,!.
812
814dir_apply_op(Name,HPid,ACH,Pre,Goal,State,Statics,Statics1):-
816 make_se_primitive(Goal,Post),
817 env_call operatorC(Name,Pre0,Post0,Cond,Statics0),
818 statics_append(Statics0,Statics,Statics2),
819 state_related(Post0,Cond,Post),
821 state_change(Pre,Pre0,Post0,State2),
822 cond_state_change(State2,Cond,State),
823 all_achieved(Post,Statics2,State),
824 remove_unneed(Statics2,[],Statics1),
825 statics_consist(Statics1),
828 829 830 incr_op_num,!.
831
833dir_apply_method(TN,HPid,ACH,Pre,Goal,State,Statics,Statics1):-
835 make_se_primitive(Goal,Post),
836 env_call methodC(Name,Pre0,Post0,Statics0,Temp0,achieve(ACH0),Dec0),
837 statics_append(Statics0,Statics,Statics2),
838 state_related(Post0,Post),
840 state_change(Pre,Pre0,Post0,State2),
842 may_achieved(Post,Statics2,State2),
844 make_dec1(HPid,Pre,ACH0,Statics2,Temp0,Dec0,Temp2,Dec2),
845 expand_decomp(Dec2,Pre,State,Temp2,Temp1,Statics2,Statics1,Dec1),
846 all_achieved(Post,Statics1,State),
849 incr_op_num,
850 make_tn(TN,Name,Pre,State,Temp1,Dec1),!.
851
853make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
854 var(HPid),
855 gensym_special(hp,HPid),
856 make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1),!.
857make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,Temp1,Dec1):-
858 all_achieved(ACH,Statics,Pre),
859 make_dec01(HPid,1,Dec,Dec1),
860 change_temp(HPid,Temp,[],Temp1),!.
861make_dec1(HPid,Pre,ACH,Statics,Temp,Dec,[before(STID0,STID1)|Temp1],[step(STID0,achieve(ACH),Pre,ACH,unexp)|Dec1]):-
862 gensym_num(HPid,0,STID0),
863 gensym_num(HPid,1,STID1),
864 make_dec01(HPid,1,Dec,Dec1),
865 change_temp(HPid,Temp,[],Temp1),!.
866
867make_dec01(HPid,_,[],[]):-!.
868make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
869 env_call operatorC(HDec,_,_,_,_),
870 gensym_num(HPid,Num,STID),
871 Num1 is Num + 1,
872 make_dec01(HPid,Num1,TDec,TDec0).
873make_dec01(HPid,Num,[HDec|TDec],[step(STID,HDec,undefd,undefd,unexp)|TDec0]):-
874 env_call methodC(HDec,_,_,_,_,_,_),
875 gensym_num(HPid,Num,STID),
876 Num1 is Num + 1,
877 make_dec01(HPid,Num1,TDec,TDec0).
878
879change_temp(HPid,[],Temp2,Temp2):-!.
880change_temp(HPid,[before(N1,N2)|Temp],Temp2,[before(ST1,ST2)|Temp0]):-
881 gensym_num(HPid,N1,ST1),
882 gensym_num(HPid,N2,ST2),
883 change_temp(HPid,Temp,Temp2,Temp0),!.
886
889make_tpnodes(Pre,Post, Statics):-
890 flag(opCounter,Num,Num),
891 Num>=1000,
892 env_retractall(tp_goal(_,_,_)),
893 env_retractall(related_op(_,_)),
894 env_assert(tp_goal(Pre,Post,Statics)),
895 env_assert(tp_node(init,Pre,Statics,from(init),0,[])),!.
896
897make_tpnodes(Pre,Post, Statics):-
898 env_retractall(tp_goal(_,_,_)),
899 env_retractall(related_op(_,_)),
900 env_assert(tp_goal(Pre,Post,Statics)),
901 assert_goal_related_init(Pre,Post,Statics),
902 env_assert(op_score(goal,0)),
903 find_all_related_goals(Pre,Statics,1,N),
905 env_assert(tp_node(init,Pre,Statics,from(init),0,[])),!.
906
908
909
910
912
913fwsearch(TN,State):- fwsearch0(fwsearch,1000, TN,State).
915
916
917fwsearch0(Caller, D, TN,State):-fwsearch0_0(Caller, D, TN,State).
918
919fwsearch0_0(_,_, TN,State):-
920 env_retract(solved_node(_,step(HP,Name,Pre,State,exp(TN)))).
921fwsearch0_0(_Caller, D, TN,State):-
922 var(TN),var(State),!,
923 D > 4, D2 is D-4,
924 fwsearch1(var_fwsearch0, D2, TN,State).
925fwsearch0_0(_Caller, D, TN,State):-
926 D > 0, D2 is D-1,
927 fwsearch1(fwsearch0, D2, TN,State).
928
929fwsearch1(_Caller, D, TN,State):-
930 select_tnode(tp_node(TP,Pre,Statics,from(PR),Score,Steps)),
931 env_assert(closed_node(TP,Pre,Statics,from(PR),Score,Steps)),
932 expand_node(TP,OP,Statics,Statics1,Pre,Post,from(PR),Steps,Steps1),
933 assert_tnode(TP,OP,PR,Score1,Post,Statics1,Steps1),
934 env_call solved_node(_,_), 935 fwsearch0(fwsearch1, D, TN,State).
936fwsearch1(_Caller, D ,TN,State):-
937 env_call(tp_node(TP,Pre,Statics,from(PR),Score,Steps)),
938 fwsearch0(tp_node, D ,TN,State).
939
940
941
942clean_temp_nodes_was_wrong:-
943 944 env_retractall(related_op(_,_)),
945 946 env_retractall(tp_goal(_,_,_)),!.
947
948clean_temp_nodes:-
949 clean_temp_nodes_was_wrong,
950 env_retractall(goal_related(_,_,_)),
951 env_retractall(goal_related_search(_)),
952 env_retractall(op_score(_,_)),
953 env_retractall(solved_node(_,_)),
954 env_retractall(current_num(tp,_)),
955 env_retractall(tp_node(_,_,_,_,_,_)),
956
957
958 env_retractall(closed_node(_,_,_,_,_,_)),!.
959
962expand_node(TP,done,Statics,Statics,Pre,Pre,from(PR),List,List):-
963 env_call tp_goal(_,Goal,_),
964 state_achieved(Goal,Pre,Statics),!.
965expand_node(TP,TN,Statics,Statics1,Pre,State,from(PR),List,List1):-
966 expand_node1(TN,Statics,Statics1,Pre,State,from(PR),List,List1).
967
969expand_node1(TN,Statics,Statics1,Pre,State,from(PR),List,List1):-
970 env_call tp_goal(_,Goal,_),
971 direct_expand(HP,TN,achieve(Goal),Pre,Goal,State,Statics,Statics1),
973 append_dcut(List,[step(HP,achieve(Goal),Pre,State,exp(TN))],List1),!.
977direct_expand(HPid,TN,ACH,Pre,Post,State,Statics,Statics1):-
978 dir_apply_method(TN,HPid,ACH,Pre,Post,State,Statics,Statics1),!.
979
981expand_node1(ID,Statics,Statics,Pre,State,from(PR),List,List1):-
982 find_related_op(Pre,[],OPls),
983 member(ID,OPls),
984 env_call gOperator(ID,_,OP),
985 apply_ground_op(OP,Pre,State,List,List1).
986expand_node1(OP,Statics,Statics1,Pre,State,from(PR),List,List1):-
987 not(env_call goal_related(_,_,_)),
988 env_call operatorC(OP,Pre0,Post0,Cond,ST),
989 apply_unground_op(OP,Pre0,Post0,Cond,ST,Statics,Statics1,Pre,State,List,List1).
990
991apply_ground_op(operator(OP,Prev,Nec,Cond),Pre,State,List,List1):-
992 state_achieved(Prev,Pre),
993 nec_state_change(Pre,Nec,State2),
994 cond_state_change(State2,Cond,State),
995 gensym_special(hp,HP),
996 append_dcut(List,[step(HP,OP,Pre,State,exp(OP))],List1),
997 incr_op_num,!.
998
999apply_unground_op(OP,Pre0,Post0,Cond,ST,Statics,Statics1,Pre,State,List,List1):-
1000 statics_append(ST,Statics,Statics2),
1001 state_achieved(Pre0,Pre,Statics2),
1002 state_change(Pre,Pre0,Post0,State2),
1003 cond_state_change(State2,Cond,State),
1004 statics_consist_instance(ST),
1005 remove_unneed(Statics2,[],Statics1),
1006 gensym_special(hp,HP),
1007 append_dcut(List,[step(HP,OP,Pre,State,exp(OP))],List1),
1008 incr_op_num.
1009
1010find_related_op([],Ops1,Ops):-
1011 remove_dup(Ops1,[],Ops),!.
1012find_related_op([Head|Pre],List,Ops):-
1013 setof(OPls,Head^Level^env_call(goal_related(Head,OPls,Level)),OPs0),
1014 flatten(OPs0,[],OPs1),
1015 append_dcut(List,OPs1,List1),
1016 find_related_op(Pre,List1,Ops),!.
1017find_related_op([Head|Pre],List,Ops):-
1018 find_related_op(Pre,List,Ops),!.
1019
1020
1021
1050
1051select_tnode(tp_node(TPid,Pre,Statics,Parents,Score,Dec)) :-
1052 lowest_tnode_FORCED,
1053 nb_getval(tnodeLOWEST,tp_node(TPid,Pre,Statics,Parents,Score,Dec)),
1054 env_retract(tp_node(TPid,Pre,Statics,Parents,Score,Dec)),!,
1055 nb_delete(tnodeLOWEST).
1057
1058random_tnode_FREE :- nb_current(tnodeLOWEST,_)->true;random_tnode_FORCED.
1059
1060random_tnode_FORCED:-
1061 env_call(tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1062 nb_setval(tnodeLOWEST,tp_node(HPid,Pre,Statics,Parents,Score,Dec)),!.
1063
1064lowest_tnode_FORCED:-
1065 random_tnode_FORCED,
1066 ignore(((
1067 nb_getval(tnodeLOWEST,tp_node(_,_,_,_,SScore,_)),
1068 env_call(tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1069 Score < SScore,
1070 nb_setval(tnodeLOWEST,tp_node(HPid,Pre,Statics,Parents,Score,Dec)),
1071 fail))).
1072
1073
1076assert_tnode(TP,OP,PR,Score,Post,Statics,[]):-!.
1077assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1078 env_call tp_goal(Pre,Goal,Statics1),
1079 state_achieved(Goal,Post,Statics),
1080 combine_exp_steps(Post,Steps,OneStep),
1081 env_assert(solved_node(Statics,OneStep)),!.
1082assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1083 existing_node(Post,Statics),!.
1084assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1085 get_score(PR,Post,Steps,Score),
1088 gensym_special(tp,TP1),
1091 env_assert(tp_node(TP1,Post,Statics,from(TP),Score,Steps)),!.
1092
1093
1094
1095assert_tnode(TP,OP,PR,Score,Post,Statics,Steps):-
1096 get_score(PR,Post,Steps,Score),
1099 gensym_special(tp,TP1),
1102 TNODEFULL = tp_node(TP1,Post,Statics,from(TP),Score,Steps),
1103 ((nb_current(tnodeLOWEST,tp_node(_,_,_,_,SScore,_)), Score < SScore)
1104 ->
1105 true;
1106 (nb_setval(tnodeLOWEST,TNODEFULL),
1107 env_shadow(asserta,TNODEFULL))),!,
1108 assert_tnode_into_sorted_list(Score,Post,TNODEFULL).
1109
1110assert_tnode_into_sorted_list(Score,Post,TNODEFULL):-
1111 (nb_getval(tnodeSORTED,NODES) ->
1112 (NODES = [] ->
1113 nb_setval(tnodeSORTED,[TNODEFULL]);
1114 ((
1115 [FrontNode|RightNodes] = NODES,
1116 must(FrontNode = tp_node(_,FPost,_,_,FScore,_)),
1117 (Post == FPost -> true ;
1118 ((FScore > Score)
1119 -> nb_setval(tnodeSORTED,[TNODEFULL|NODES]) ;
1120 insert_to_list(Score,Post,TNODEFULL,RightNodes,NODES))))));
1121
1122 nb_setval(tnodeSORTED,[TNODEFULL])).
1123
1124
1125insert_to_list(_,_,TNODEFULL,[],NODES):-nb_setarg(2,NODES,[TNODEFULL]),!.
1126insert_to_list(Score,Post,TNODEFULL,AllNodes,NODES):-
1127 AllNodes = [FrontNode|RightNodes],
1128 FrontNode = tp_node(_,FPost,_,_,FScore,_),!,
1129 (Post = FPost -> true ;
1130 ((FScore > Score) -> ( env_asserta(TNODEFULL),nb_setarg(2,NODES,[TNODEFULL|AllNodes])) ;
1131 (insert_to_list(Score,Post,TNODEFULL,RightNodes,AllNodes)))).
1132insert_to_list(Score,Post,TNODEFULL,AllNodes,NODES):-
1133 AllNodes = [_|RightNodes],
1134 insert_to_list(Score,Post,TNODEFULL,RightNodes,AllNodes).
1135
1136
1137combine_exp_steps(Post,Steps,step(HP,achieve(Goal),Pre,Post,exp(TN))):-
1138 env_call tp_goal(Pre,Goal,Statics),
1139 get_action_list(Steps,[],ACTls),
1140 make_temp(ACTls,[],Temp),
1141 gensym_special(hp,HP),
1142 make_tn(TN,achieve(Goal),Pre,Post,Temp,Steps),!.
1143
1145get_action_list([],ACTls,ACTls):-!.
1146get_action_list([step(HP,_,_,_,_)|Steps],List,ACTls):-
1147 append_dcut(List,[HP],List1),
1148 get_action_list(Steps,List1,ACTls),!.
1149
1150make_temp([HP|[]],Temp,Temp):-!.
1151make_temp([HP1|[HP2|Rest]],List,Temp):-
1152 append_dcut(List,[before(HP1,HP2)],List1),
1153 make_temp([HP2|Rest],List,Temp),!.
1154
1155existing_node(Post,_Statics):-
1156 env_call(tp_node(_,Post,_,_,_,_)).
1157existing_node(Post,_Statics):-
1158 env_call closed_node(_,Post,_,_,_,_).
1160
1161assert_goal_related_init(Pre,[],Statics):-!.
1164
1165
1166assert_goal_related_init(Pre,[se(Sort,Obj,SE)|Post],Statics):-
1167 ground(Obj),
1168 env_call is_of_primitive_sort(Obj,SortP),
1169 env_assert(goal_related(se(SortP,Obj,SE),[],0)),
1170 assert_goal_related_init(Pre,Post,Statics),!.
1171assert_goal_related_init(Pre,[se(Sort,Obj,SE)|Post],Statics):-
1172 assert_related_goals_varible(Sort,Obj,SE,goal,0),
1173 assert_goal_related_init(Pre,Post,Statics),!.
1174
1177find_all_related_goals(Pre,Statics,N,N):-
1178 get_all_state(States),
1179 all_found(States,Pre,Statics),
1180 env_assert(goal_related_search(succ)),
1181 find_all_related_goals_final(Statics,N),!.
1182find_all_related_goals(Pre,Statics,I,N):-
1183 I1 is I-1,
1184 env_call goal_related(_,_,I1),
1185 find_related_goal(Statics,I1,I),
1186 I2 is I+1,
1187 Key is random(10),
1188 find_all_related_goals(Pre,Statics,I2,N),!.
1189find_all_related_goals(Pre,Statics,N,N):-
1190 not(env_call goal_related(_,_,N)),
1191 env_assert(goal_related_search(fail)),
1192 write('related goal search failed'),
1193 env_retractall(goal_related(_,_,_)),!.
1194 1195 1196
1197
1200find_all_related_goals_final(Statics,N):-
1201 N1 is N-1,
1202 env_call goal_related(Pre,_,N1),
1203 find_related_goal(Statics,N1,N),!.
1204find_all_related_goals_final(Statics,N):-!.
1205
1207get_all_state(States):-
1208 setof(Goal, Statics^Level^OP^(env_call goal_related(Goal,OP,Level)),States11),
1209 put_one_obj_together(States11,[],States),!.
1210
1211put_one_obj_together([],States,States):-!.
1212put_one_obj_together([se(Sort,Obj,ST)|States1],List,States):-
1213 put_one_obj_together1(se(Sort,Obj,ST),List,List1),
1214 put_one_obj_together(States1,List1,States),!.
1215
1216put_one_obj_together1(se(Sort,Obj,ST),[],[se(Sort,Obj,ST)]):-!.
1217put_one_obj_together1(se(Sort,Obj,ST),[se(Sort,Obj,ST00)|List],[se(Sort,Obj,ST1)|List]):-
1218 set_append_e(ST,ST00,ST1),!.
1219put_one_obj_together1(se(Sort,Obj,ST),[se(Sort1,Obj1,ST1)|List],[se(Sort1,Obj1,ST1)|List1]):-
1220 Obj\==Obj1,
1221 put_one_obj_together1(se(Sort,Obj,ST),List,List1),!.
1222
1224all_found([],States,Statics):-!.
1225all_found([se(Sort,Obj,ST)|States],Pre,Statics):-
1226 member(se(Sort,Obj,SPre),Pre),
1227 subtract(SPre,ST,Diff),
1228 isemptylist(Diff),
1229 all_found(States,Pre,Statics),!.
1232find_related_goal(Statics,I1,I):-
1233 env_call gOperator(OPID,ID,operator(Name,Prev,Nec,Cond)),
1234 find_related_goal_nec(OPID,Name,Prev,Nec,Statics,I1,I),
1235 find_related_goal_cond(OPID,Name,Prev,Nec,Cond,Statics,I1,I),
1236 fail.
1237find_related_goal(Statics,I1,I).
1238
1239find_related_goal_nec(ID,Name,Prev,Nec,Statics,I1,I):-
1240 env_call goal_related(se(Sort,Obj,SE),Ops,I1),
1241 member(sc(Sort,Obj,Lhs=>Rhs),Nec),
1242 state_match(Sort,Obj,SE,Rhs),
1243 statics_consist(Statics),
1245 assert_goal_related(Prev,Nec,ID,I).
1246
1247find_related_goal_cond(ID,Name,Prev,Nec,[],Statics,I1,I):-
1248 !.
1249find_related_goal_cond(ID,Name,Prev,Nec,Cond,Statics,I1,I):-
1250 env_call goal_related(se(Sort,Obj,SE),Ops,I1),
1251 member(sc(Sort0,Obj,LHS=>RHS),Cond),
1252 env_call is_of_sort(Obj,Sort0),
1253 env_call is_of_sort(Obj,Sort), 1254 filterInvars(LHS,LInVars,LIsOfSorts,LNEs,FLHS),
1255 filterInvars(RHS,RInVars,RIsOfSorts,RNEs,FRHS),
1256 can_achieve_g([se(Sort,Obj,FRHS)],[se(Sort,Obj,SE)],Statics),
1257 statics_consist(Statics),
1258 checkInVars(LInVars),
1259 checkInVars(RInVars),
1260 checkIsOfSorts(LIsOfSorts),
1261 checkIsOfSorts(RIsOfSorts),
1262 obeysNEs(LNEs),
1263 obeysNEs(RNEs),
1265 assert_goal_related(Prev,[sc(Sort,Obj,FLHS=>FRHS)|Nec],ID,I).
1266
1267
1269filterInvars([],[],[],[],[]):-!.
1270filterInvars([is_of_sort(A,B)|State],InVars,[is_of_sort(A,B)|IsOfSorts],NEs,FState):-
1271 !,
1272 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1273filterInvars([ne(A,B)|State],InVars,IsOfSorts,[ne(A,B)|NEs],FState):- dif(A,B),
1274 !,
1275 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1276filterInvars([is_of_primitive_sort(A,B)|State],InVars,[is_of_sort(A,B)|IsOfSorts],NEs,FState):-
1277 !,
1278 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1279filterInvars([Pred|State],[Pred|InVars],IsOfSorts,NEs,FState):-
1280 functor(Pred,FF,NN),
1281 functor(Pred1,FF,NN),
1282 env_call(atomic_invariantsC(Atom)),
1283 member(Pred1,Atom),!,
1284 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1285filterInvars([Pred|State],InVars,IsOfSorts,NEs,[Pred|FState]):-
1286 filterInvars(State,InVars,IsOfSorts,NEs,FState).
1287
1289filterNes([],[],[]):-!.
1290filterNes([ne(A,B)|State],[ne(A,B)|NEs],FState):- dif:dif(A,B),
1291 !,
1292 filterNes(State,NEs,FState).
1293filterNes([Pred|State],NEs,[Pred|FState]):-
1294 filterNes(State,NEs,FState).
1295
1296assert_related_op(OP,I):-
1297 env_call related_op(OP,_),!.
1298assert_related_op(OP,I):-
1299 env_asserta(related_op(OP,I)),!.
1300
1302can_achieve_g([],State2,Statics):-!.
1303can_achieve_g(State1,State2,Statics):-
1304 can_achieve_g(State1,State2),
1305 statics_consist(Statics).
1306
1307can_achieve_g([se(Sort,Obj,ST1)|State1],[se(Sort,Obj,ST2)]):-
1308 state_match(Sort,Obj,ST2,ST1).
1309can_achieve_g([Head|State1],State2):-
1310 can_achieve_g(State1,State2).
1311
1313assert_goal_related(Prev,Nec,OP,I):-
1314 assert_goal_related1(Prev,OP,I),!,
1315 assert_goal_related1(Nec,OP,I).
1316
1317assert_goal_related1([],Op,I):-!.
1318assert_goal_related1([se(Sort,Obj,SE)|Prev],Op,I):-
1319 assert_goal_related2(se(Sort,Obj,SE),Op,I),
1320 assert_goal_related1(Prev,Op,I),!.
1321assert_goal_related1([sc(Sort,Obj,LH=>RH)|Nec],Op,I):-
1322 ground(Obj), 1323 env_call is_of_primitive_sort(Obj,PSort),!,
1324 assert_goal_related2(se(PSort,Obj,LH),Op,I),
1325 assert_goal_related1(Nec,Op,I).
1326assert_goal_related1([sc(Sort,Obj,LH=>RH)|Nec],Op,I):-
1327 var(Obj),
1328 assert_related_goals_varible(Sort,Obj,LH,Op,I),
1329 assert_goal_related1(Nec,Op,I).
1330
1331assert_goal_related2(se(Sort,Obj,SE),goal,I):-
1332 env_assert(goal_related(se(Sort,Obj,SE),[],I)),!.
1333assert_goal_related2(se(Sort,Obj,SE),Op,I):-
1334 env_call goal_related(se(Sort,Obj,SE1),Ops,I),
1335 not(is_diff(SE,SE1)),
1336 env_retract(goal_related(se(Sort,Obj,SE),Ops,I)),
1337 env_assert(goal_related(se(Sort,Obj,SE),[Op|Ops],I)),!.
1338assert_goal_related2(se(Sort,Obj,SE),Op,I):-
1339 env_assert(goal_related(se(Sort,Obj,SE),[Op],I)),!.
1340
1341assert_related_goals_varible(Sort,Obj,SE,Op,I):-
1342 find_prim_sort(Sort,PSorts),
1343 member(Sort1,PSorts),
1344 assert_goal_related2(se(Sort1,Obj,SE),Op,I),
1345 fail.
1346assert_related_goals_varible(Sort,Obj,SE,Op,I).
1347
1349assert_op_score(OP,OPB):-
1350 env_call op_score(OP,_),!.
1351assert_op_score(OP,OPB):-
1352 env_call op_score(OPB,I),
1353 I1 is I+1,
1354 env_assert(op_score(OP,I1)),!.
1355
1356get_score(PR,Post,Steps,Score):-
1357 env_call tp_goal(Pre,Goal,Statics),
1358 get_distance(Pre,Post,Goal,0,Dis), 1360 get_tnode_length(PR,1,Len),
1364 Score is Dis+Len,!.
1365
1366get_distance(Pre,[],Goal,Dis,Dis):-!.
1367get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1368 member(se(Sort,Obj,SE0),Goal),
1369 state_match(Sort,Obj,SE0,SE), 1370 get_distance(Pre,Post,Goal,Dis1,Dis),!.
1371get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1372 env_call goal_related(se(Sort,Obj,SE0),_,Level),
1373 state_match(Sort,Obj,SE0,SE),
1374 Dis2 is Dis1+Level,
1375 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1376get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1377 member(se(Sort,Obj,SE0),Pre),
1378 state_match(Sort,Obj,SE,SE0), 1379 Dis2 is Dis1+1,
1380 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1381get_distance(Pre,[se(Sort,Obj,SE)|Post],Goal,Dis1,Dis):-
1382 Dis2 is Dis1+100,
1383 get_distance(Pre,Post,Goal,Dis2,Dis),!.
1384
1385get_tnode_length(init,Len,Len):-!.
1386get_tnode_length(TP,Len1,Len):-
1387 env_call closed_node(TP,_,_,from(PR),_,_),
1388 Len2 is Len1+1,
1389 get_tnode_length(PR,Len2,Len),!.
1390
1391
1396find_relate_state:-
1397 operatorC(A,Pre,Post,Cond,ST),
1398 assert_related_states(A,Pre,Post,Cond,ST),
1399 fail.
1400find_relate_state.
1401
1402assert_related_states(A,Pre,Post,Cond,ST):-
1403 assert_related_states1(A,Pre,Post,ST),
1404 assert_related_states2(A,Pre,Cond,ST).
1407assert_related_states1(A,Pre,[],ST):-!.
1409assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
1410 u_mem_cut(se(Sort,Obj,SE),Pre),
1411 assert_related_states1(A,Pre,Post,ST),!.
1413assert_related_states1(A,Pre,[se(Sort,Obj,SE)|Post],ST):-
1414 assert(produce(se(Sort,Obj,SE),A,Pre,ST)),
1415 assert_related_states1(A,Pre,Post,ST),!.
1416
1419assert_related_states2(A,Pre,SC,ST):-
1420 make_sc_primitive(SC,PSC),
1421 assert_related_states21(A,Pre,PSC,ST).
1422
1423assert_related_states21(A,Pre,[],ST):-!.
1424assert_related_states21(A,Pre,[sc(Sort,Obj,SE=>SS)|Trans],ST):-
1425 rem_statics([se(Sort,Obj,SE)],[se(Sort,Obj,SER)],St1),
1426 rem_statics([se(Sort,Obj,SS)],[se(Sort,Obj,SSR)],St2),
1427 append_dcut(ST,St1,ST1),
1428 append_dcut(ST1,St2,ST21),
1429 remove_unneed(ST21,[],ST2),
1430 append_dcut(Pre,[se(Sort,Obj,SER)],Pre1),
1431 env_assert(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
1432 must(produce(se(Sort,Obj,SSR),A,Pre1,ST2)),
1433 assert_related_states21(A,Pre,Trans,ST),!.
1434
1439remove_temp([],HP1,List,List):-!.
1440remove_temp([before(HP1,HP2)|Temp],HP1,List,Temp1):-
1441 remove_temp_before(List,before(HP1,HP2),List2),
1442 remove_temp(Temp,HP1,List2,Temp1),!.
1443remove_temp([before(HP2,HP1)|Temp],HP1,List,Temp1):-
1444 remove_temp_after(List,before(HP2,HP1),List2),
1445 remove_temp(Temp,HP1,List2,Temp1),!.
1446remove_temp([before(HPX,HPY)|Temp],HP1,List,Temp1):-
1447 remove_temp(Temp,HP1,List,Temp1),!.
1448
1450remove_temp_before([],before(HP1,HP2),[]):-!.
1451remove_temp_before([before(HP1,HP2)|T],before(HP1,HP2),T1):-
1452 remove_temp_before(T,before(HP1,HP2),T1),!.
1453remove_temp_before([before(HP3,HP1)|T],before(HP1,HP2),[before(HP3,HP2)|T1]):-
1454 remove_temp_before(T,before(HP1,HP2),T1),!.
1455remove_temp_before([before(HPX,HPY)|T],before(HP1,HP2),[before(HPX,HPY)|T1]):-
1456 remove_temp_before(T,before(HP1,HP2),T1),!.
1458remove_temp_after([],before(HP1,HP2),[]):-!.
1459remove_temp_after([before(HP2,HP1)|T],before(HP2,HP1),T1):-
1460 remove_temp_after(T,before(HP2,HP1),T1),!.
1461remove_temp_after([before(HP1,HP3)|T],before(HP2,HP1),[before(HP2,HP3)|T1]):-
1462 remove_temp_after(T,before(HP2,HP1),T1),!.
1463remove_temp_after([before(HPX,HPY)|T],before(HP2,HP1),[before(HPX,HPY)|T1]):-
1464 remove_temp_after(T,before(HP2,HP1),T1),!.
1465
1466remove_dec(HPid,[],[]):-!.
1467remove_dec(HPid,[step(HPid,_,_,_,_)|Dec],Dec1):-
1468 remove_dec(HPid,Dec,Dec1),!.
1469remove_dec(HPid,[step(A,B,C,D,F)|Dec],[step(A,B,C,D,F)|Dec1]):-
1470 remove_dec(HPid,Dec,Dec1),!.
1471
1475state_achieved(undefd,State,Statics):-!.
1476state_achieved([],State2,Statics):-!.
1477state_achieved(State1,State2,Statics):-
1478 state_achieved(State1,State2),
1479 statics_consist(Statics).
1480
1481state_achieved(undefd,State):-!.
1482state_achieved([],State2).
1483state_achieved([se(Sort,Obj,ST1)|State1],State2):-
1484 member(se(Sort,Obj,ST2),State2),
1485 env_call is_of_sort(Obj,Sort),
1486 state_match(Sort,Obj,ST1,ST2),
1487 list_take(State2,[se(Sort,Obj,ST2)],State21),
1488 state_achieved(State1,State21).
1489state_achieved([se(Sort,Obj,ST1)|State1],State2):-
1490 not(member(se(Sort,Obj,ST2),State2)),
1491 state_achieved(State1,State2),!.
1492
1496state_match(Sort,Obj,ST,ST1):-
1497 not(is_diff(ST,ST1)),!.
1502state_match(Sort,Obj,ST,ST1):-
1503 is_achieved(ST,ST1),
1504 env_call(gsubstate_classes(Sort,Obj,Substateclasses)),
1505 not(in_different_states(ST,ST1,Substateclasses)),!.
1508state_match(Sort,Obj,ST,ST1):-
1509 not(is_achieved(ST,ST1)),
1510 set_append(ST,ST1,ST0),
1511 env_call(gsubstate_classes(Sort,Obj,Substateclasses)),
1512 in_same_sub_states(ST0,Substateclasses),!.
1513
1515in_same_sub_states(ST0,[State|SCls]):-
1516 is_achieved(ST0,State),!.
1517in_same_sub_states(ST0, [State|SCls]):-
1518 in_same_sub_states(ST0,SCls),!.
1519
1521in_different_states(ST,ST1, [State|SCls]):-
1522 max_member(ST,Substateclasses,MSub, _),
1523 max_member(ST1,Substateclasses,MSub1, _),
1524 MSub\==MSub1,!.
1525
1526max_member(State, Stateclass, MSub, Others):-
1527 max_member1(State, Stateclass, 0, [],MSub),
1528 subtract(State,MSub,Others),!.
1529
1531max_member1(State, [], Num, MSub, MSub):-!.
1533max_member1(State, [State1|SCls], Num, MSub1, MSub):-
1534 same_items(State1,State,MSSt),
1535 length(MSSt,Len),
1536 Len > Num,
1537 max_member1(State, SCls, Len, MSSt, MSub),!.
1538max_member1(State, [State1|SCls], Num, MSub1,MSub):-
1539 max_member1(State, SCls, Num, MSub1,MSub),!.
1540
1542same_items([],List2,[]):-!.
1543same_items([X|List1],List2,[X|Same]):-
1544 member(X,List2),
1545 same_items(List1,List2,Same),!.
1546same_items([X|List1],List2,Same):-
1547 same_items(List1,List2,Same),!.
1548
1549
1551is_achieved([],_):-!.
1552is_achieved([H|T], State) :-
1553 is_statics(H),
1554 is_achieved(T,State),!.
1555is_achieved([H|T], State) :-
1556 member(H,State),
1557 is_achieved(T,State),!.
1558
1560is_statics(ne(A,B)):-!.
1561is_statics(is_of_sort(A,B)):-!.
1562is_statics(is_of_primitive_sort(A,B)):-!.
1563is_statics(Pred):-
1564 functor(Pred,FF,NN),
1565 functor(Pred1,FF,NN),
1566 env_call(atomic_invariantsC(Atom)),
1567 member(Pred1,Atom),!.
1568
1576state_change([],Pre0,Post0,[]):-!.
1577state_change(Pre,[],[],Pre):-!.
1578state_change([se(Sort,Obj,SPre)|Pre],Pre0,Post0,[se(Sort,Obj,STPost)|Post]):-
1579 state_achieved([se(Sort,Obj,SPre)],Pre0),
1580 state_change0(Sort,Obj,SPre,Pre0,Post0,Pre1,Post1,STPost),
1581 state_change(Pre,Pre1,Post1,Post).
1582state_change([se(Sort,Obj,SPre)|Pre],Pre0,Post0,[se(Sort,Obj,STPost)|Post]):-
1583 not(member(se(Sort,Obj,SPre0),Pre0)),
1584 state_change(Pre,Pre1,Post1,Post).
1585
1587state_change0(Sort,Obj,SPre,[],[],[],[],SPre):-!.
1588state_change0(Sort,Obj,SPre,[se(Sort,Obj,SPre0)|Pre0],[se(Sort,Obj,SPost0)|Post0],Pre0,Post0,STPost):-
1589 state_change1(SPre,SPre0,SPost0,STPost).
1590state_change0(Sort,Obj,SPre,[se(Sort1,Obj1,SPre0)|Pre0],[se(Sort1,Obj1,SPost0)|Post0],[se(Sort1,Obj1,SPre0)|Pre1],[se(Sort1,Obj1,SPost0)|Post1],STPost):-
1591 Obj\==Obj1,
1592 state_change0(Sort,Obj,SPre,Pre0,Post0,Pre1,Post1,STPost).
1593
1594state_change1([],SPre0,SPost0,SPost0):-!.
1595state_change1(Pre,[],[],Pre):-!.
1597state_change1([Head|SPre],SPre0,SPost0,STPost):-
1598 member(Head,SPre0),
1599 not(member(Head,SPost0)),
1600 state_change1(SPre,SPre0,SPost0,STPost).
1602state_change1([Head|SPre],SPre0,SPost0,[Head|STPost]):-
1603 not(member(Head,SPre0)),
1604 not(member(Head,SPost0)),
1605 state_change1(SPre,SPre0,SPost0,STPost),!.
1608state_change1([Head|SPre],SPre0,SPost0,STPost):-
1609 member(Head,SPost0),
1610 state_change1(SPre,SPre0,SPost0,STPost).
1611
1613rough_state_change(Pre,[],[],Pre):-!.
1614rough_state_change([],_,_,[]):-!.
1615rough_state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SS0)|Post]):-
1616 member(se(Sort,Obj,SE0),Pre0),
1617 member(se(Sort,Obj,SS0),Post0),
1618 env_call is_of_sort(Obj,Sort),
1619 state_achieved([se(Sort,Obj,SE0)],[se(Sort,Obj,SE)]),
1620 list_take(Pre0,[se(Sort,Obj,SE0)],Pre01),
1621 list_take(Post0,[se(Sort,Obj,SS0)],Post01),
1622 rough_state_change(Pre,Pre01,Post01,Post),!.
1623rough_state_change([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|Post]):-
1624 rough_state_change(Pre,Pre0,Post0,Post),!.
1625
1627state_changeG([],Pre0,Post0,[]):-!.
1628state_changeG([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,RHS)|State]):-
1629 member(se(Sort,Obj,LHS),Pre0),
1630 member(se(Sort,Obj,RHS),Post0),
1631 state_match(Sort,Obj,SE,LHS),
1632 state_changeG(Pre,Pre0,Post0,State),!.
1633state_changeG([se(Sort,Obj,SE)|Pre],Pre0,Post0,[se(Sort,Obj,SE)|State]):-
1634 not(member(se(Sort,Obj,LHS),Pre0)),
1635 state_changeG(Pre,Pre0,Post0,State),!.
1636
1637find_lower_sort(Sort,Sort,Sort):-!.
1638find_lower_sort(Sort,Sort1,Sort1):-
1639 subsorts(Sort,Sortls),
1640 member(Sort1,Sortls),!.
1641find_lower_sort(Sort,Sort1,Sort):-
1642 subsorts(Sort1,Sortls),
1643 member(Sort,Sortls),!.
1649nec_state_change([],Nec,[]):-!.
1650nec_state_change([se(Sort,Obj,SE)|Pre],Nec,[se(Sort,Obj,Post)|State]):-
1651 member(sc(Sort,Obj,Lhs=>Rhs),Nec),
1652 state_match(Sort,Obj,Lhs,SE),
1653 state_change1(SE,Lhs,Rhs,Post),
1654 nec_state_change(Pre,Nec,State),!.
1655nec_state_change([se(Sort,Obj,SE)|Pre],Nec,[se(Sort,Obj,SE)|State]):-
1656 not(member(sc(Sort,Obj,Lhs=>Rhs),Nec)),
1657 nec_state_change(Pre,Nec,State),!.
1662cond_state_change([],Cond,[]):-!.
1663cond_state_change(State,[],State):-!.
1664cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[NewSS|State]):-
1665 member(sc(Sort1,Obj1,SE0=>SS0),Cond),
1667 subsorts(Sort1,Subsorts),
1668 member(Sort,Subsorts),
1669 copy_states(se(Sort1,Obj1,SE0),se(Sort,Obj,SE2)),
1670 copy_states(se(Sort1,Obj1,SS0),se(Sort,Obj,SS2)),
1672 filterInvars(SE2,LInVars,LIsOfSorts,LNEs,FSE),
1673 filterInvars(SS2,RInVars,RIsOfSorts,RNEs,FSS),
1674 state_match(Sort,Obj,SE,FSE),
1675 state_change([se(Sort,Obj,SE)],[se(Sort,Obj,FSE)],
1676 [se(Sort,Obj,FSS)],[NewSS]),
1677 checkInVars(LInVars),
1678 checkInVars(RInVars),
1679 checkIsOfSorts(LIsOfSorts),
1680 checkIsOfSorts(RIsOfSorts),
1681 obeysNEs(LNEs),
1682 obeysNEs(RNEs),
1683 cond_state_change(Pre,Cond,State),!.
1684cond_state_change([se(Sort,Obj,SE)|Pre],Cond,[se(Sort,Obj,SE)|State]):-
1685 cond_state_change(Pre,Cond,State),!.
1686
1688copy_states(se(Sort1,Obj1,SE0),se(Sort,Obj,SE2)):-
1689 copy_states1(Obj1,SE0,Obj,SE2),!.
1690copy_states1(Obj1,[],Obj,[]):-!.
1691copy_states1(Obj1,[Pred|SE0],Obj,[Pred2|SE2]):-
1692 functor(Pred,FF,NN),
1693 functor(Pred2,FF,NN),
1694 Pred=..[Name|Vars],
1695 Pred2=..[Name|Vars2],
1696 copy_pred(Obj1,Obj,Vars,Vars2),
1697 copy_states1(Obj1,SE0,Obj,SE2),!.
1698
1699copy_pred(Obj1,Obj,[],[]):-!.
1700copy_pred(Obj1,Obj,[Var|Vars],[Var2|Vars2]):-
1701 Obj1==Var,
1702 Var2=Obj,
1703 copy_pred(Obj1,Obj,Vars,Vars2),!.
1704copy_pred(Obj1,Obj,[Var|Vars],[Var|Vars2]):-
1705 copy_pred(Obj1,Obj,Vars,Vars2),!.
1708all_achieved(undefd,Statics,List2):-!.
1709all_achieved([],Statics,List2):-!.
1710all_achieved(List1,Statics,List2):-
1711 all_achieved(List1,List2),
1712 statics_consist(Statics).
1713
1714all_achieved([],List2).
1715all_achieved([se(Sort,Obj,SL)|List1],List2):-
1716 member(se(Sort1,Obj,SR),List2),
1717 env_call is_of_sort(Obj,Sort1),
1718 env_call is_of_sort(Obj,Sort),
1719 env_call is_of_primitive_sort(Obj,PSort),
1720 state_match(PSort,Obj,SL,SR),
1721 all_achieved(List1,List2).
1724may_achieved(undefd,Statics,Post):-!.
1725may_achieved([],Statics,Post):-!.
1726may_achieved(Pre,Statics,Post):-
1727 may_achieved(Pre,Post),
1728 statics_consist(Statics),!.
1729may_achieved([],Post).
1730may_achieved([se(Sort,Obj,SL)|Pre],Post):-
1731 member(se(Sort1,Obj,SR),Post),
1732 env_call is_of_sort(Obj,Sort1),
1733 env_call is_of_sort(Obj,Sort),
1734 env_call is_of_primitive_sort(Obj,PSort),
1735 state_may_achieved(PSort,Obj,SL,SR),
1736 may_achieved(Pre,Post),!.
1737
1739state_may_achieved(Sort,Obj,[],ST2):-!.
1740state_may_achieved(Sort,Obj,ST1,ST2):-
1741 is_achieved(ST1,ST2),!.
1745post_instant(Post0,Cond,Statics,undefd):-!.
1746post_instant(Post0,Cond,Statics,[]):-!.
1747post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1748 member(se(Sort,Obj,SE0),Post0),
1749 statics_consist(Statics).
1750post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1751 member(sc(Sort,Obj,SE1=>SS),Cond),
1752 statics_consist(Statics).
1753post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1754 member(sc(Sort0,Obj,SE1=>SS),Cond),
1755 not(env_call(objectsC(Sort0,_))),
1756 subsorts(Sort0,Sortls),
1757 not(not(member(Sort,Sortls))),
1758 statics_consist(Statics).
1759post_instant(Post0,Cond,Statics,[se(Sort,Obj,SE)|Post]):-
1760 post_instant(Post0,Cond,Statics,Post),!.
1761
1764statics_consist([]):-!.
1765statics_consist(Statics):-
1766 get_invariants(Invs),
1767 statics_consist(Invs,Statics),!.
1768statics_consist(Invs,[]):-!.
1769statics_consist(Invs,[ne(A,B)|Statics]):- dif(A,B),
1770 not(A==B),!,
1771 statics_consist(Invs,Statics).
1772statics_consist(Invs,[is_of_sort(Obj,Sort)|Statics]):-
1773 not(not(env_call is_of_sort(Obj,Sort))),!,
1774 statics_consist(Invs,Statics).
1775statics_consist(Invs,[is_of_primitive_sort(Obj,Sort)|Statics]):-
1776 not(not( env_call is_of_primitive_sort(Obj,Sort))),!,
1777 statics_consist(Invs,Statics).
1778statics_consist(Invs,[Pred|Statics]):-
1779 pred_member(Pred,Invs),!,
1780 statics_consist(Invs,Statics).
1781
1783statics_consist_instance([]):-!.
1784statics_consist_instance(Statics):-
1785 get_invariants(Invs),
1786 statics_consist_instance(Invs,Statics).
1787
1788statics_consist_instance(Invs,[]):-!.
1789statics_consist_instance(Invs,[is_of_sort(Obj,Sort)|Atom]):-
1790 ground(Obj),
1791 env_call is_of_sort(Obj,Sort),!,
1792 statics_consist_instance(Invs,Atom).
1793statics_consist_instance(Invs,[is_of_sort(Obj,Sort)|Atom]):-
1794 var(Obj),
1795 env_call is_of_sort(Obj,Sort),
1796 statics_consist_instance(Invs,Atom).
1797statics_consist_instance(Invs,[is_of_primitive_sort(Obj,Sort)|Atom]):-
1798 ground(Obj),
1799 env_call is_of_primitive_sort(Obj,Sort),!,
1800 statics_consist_instance(Invs,Atom).
1801statics_consist_instance(Invs,[is_of_primitive_sort(Obj,Sort)|Atom]):-
1802 var(Obj),
1803 env_call is_of_primitive_sort(Obj,Sort),
1804 statics_consist_instance(Invs,Atom).
1805statics_consist_instance(Invs,[ne_back(A,B)|Atom]):-
1806 A\==B, dif(A,B),
1807 statics_consist_instance(Invs,Atom).
1808statics_consist_instance(Invs,[ne(A,B)|Atom]):-
1809 append_dcut(Atom,[ne_back(A,B)],Atom1),!,
1810 statics_consist_instance(Invs,Atom1).
1811statics_consist_instance(Invs,[Pred|Atom]):-
1812 ground(Pred),
1813 member(Pred,Invs),!,
1814 statics_consist_instance(Invs,Atom).
1815statics_consist_instance(Invs,[Pred|Atom]):-
1816 not(ground(Pred)),
1817 member(Pred,Invs),
1818 statics_consist_instance(Invs,Atom).
1819
1820
1821
1826
1827make_problem_into_node(I,goal(L,TM,STATS), NN) :-
1828 must_det_l(( make_problem_up(L, STEPS),
1829 make_num_hp(TM,Temp),
1830 sort_steps(STEPS,Temp,STEPS1),
1831 make_ss_to_se(I,I_Pre),
1832 NN = node(root,I_Pre,STEPS1 ,Temp, STATS))),!.
1833make_problem_into_node(I,L, NN) :-
1834 must_det_l(( make_problem_up([achieve(L)], STEPS),
1835 make_num_hp(TM,Temp),
1836 sort_steps(STEPS,Temp,STEPS1),
1837 make_ss_to_se(I,I_Pre),
1838 NN = node(root,I_Pre,STEPS1 ,Temp, STATS))),!.
1839
1841make_problem_up([],[]):-!.
1842make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,[L1],unexp)|RS]):-
1843 1844 make_ss_to_se([L],[L1]),
1845 gensym_special(hp,HP),
1846 make_problem_up(R, RS),!.
1847make_problem_up([achieve(L)|R],[step(HP,achieve(L1),undefd,L1,unexp)|RS]):-
1848 1849 make_ss_to_se(L,L1),
1850 gensym_special(hp,HP),
1851 make_problem_up(R, RS),!.
1852make_problem_up([O|R],[step(HP,O,undefd,undefd,unexp)|RS]):-
1853 env_call methodC(O,Pre,Post,Statics1,Temp,ACH,Dec),
1854 gensym_special(hp,HP),
1855 make_problem_up(R, RS),!.
1856make_problem_up([O|R],
1857 [step(HP,O,undefd,undefd,unexp)|RS]):-
1858 env_call operatorC(O,Pre,Post,Cond,Statics1),
1859 gensym_special(hp,HP),
1860 make_problem_up(R, RS),!.
1861
1862make_num_hp([],[]):-!.
1863make_num_hp([before(N1,N2)|TM],[before(H1,H2)|Temp]):-
1864 gensym_num(hp,N1,H1),
1865 gensym_num(hp,N2,H2),
1866 make_num_hp(TM,Temp),!.
1867
1870sort_steps(Steps,[],Steps):-!.
1871sort_steps([Steps|[]],[],[Steps]):-!.
1872sort_steps(Steps,Temp,OrderedST):-
1873 steps_in_temp(Temp,[],ST),
1874 sort_steps1(Temp,ST,OrderedSTID),
1875 sort_steps2(Steps,OrderedSTID,[],OrderedST),!.
1876
1878steps_in_temp([],ST,ST):-!.
1879steps_in_temp([before(H1,H2)|TT],List,ST):-
1880 set_append_e(List,[H1,H2],List1),
1881 steps_in_temp(TT,List1,ST),!.
1882
1884sort_steps1(Temp,[],[]):-!.
1885sort_steps1(Temp,[HP1|TST],[HPF|OST]):-
1886 earliest_step(HP1,HPF,Temp,TST,TST1),
1887 sort_steps1(Temp,TST1,OST),!.
1888
1889earliest_step(HPF,HPF,Temp,[],[]):-!.
1890earliest_step(HP1,HPF,Temp,[HP2|TST],[HP1|TST1]):-
1891 member(before(HP2,HP1),Temp),
1892 earliest_step(HP2,HPF,Temp,TST,TST1),!.
1893earliest_step(HP1,HPF,Temp,[HP2|TST],[HP2|TST1]):-
1894 earliest_step(HP1,HPF,Temp,TST,TST1),!.
1895
1897sort_steps2(OtherST,[],OrderedST1,OrderedST):-
1898 append_dcut(OrderedST1,OtherST,OrderedST),!.
1899sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1900 member(step(HP,N,Pre,Post,F),Steps),
1901 append_dcut(List,[step(HP,N,Pre,Post,F)],List1),
1902 list_take(Steps,[step(HP,N,Pre,Post,F)],Steps1),
1903 sort_steps2(Steps1,THPS,List1,OrderedST),!.
1904sort_steps2(Steps,[HP|THPS],List,OrderedST):-
1905 sort_steps2(Steps,THPS,List,OrderedST),!.
1907
1909make_ss_to_se([],[]):-!.
1910make_ss_to_se([ss(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1911 make_ss_to_se(TPost,TPre),!.
1912make_ss_to_se([se(Sort,Obj,Post)|TPost],[se(Sort,Obj,Post)|TPre]):-
1913 make_ss_to_se(TPost,TPre),!.
1914
(Node,PHPs,SIZE1,TNList) :-
1920 1921 getN_decomp(Node, HPs),
1922 push_to_primitive(HPs,[],PHPs,[],TNList),
1923 pprint(PHPs,1,SIZE),
1924 SIZE1 is SIZE -1,!.
1925
1929change_op_representation :-
1930 env_call(method(A,B,C,Stat,T,Dec)),
1931 make_ss_to_se(B,B0),
1932 make_se_primitive(B0,B1),
1933 make_sc_primitive(C,C1),
1934 get_preconditions(C1,B1,Pre,Post),
1935 rem_statics(Post, PostR,St1),
1936 rem_statics(Pre, PreR,St2),
1937 append_dcut(St1,St2,Statics),
1938 append_dcut(Stat,Statics,Statics1),
1939 remove_unneed(Statics1,[],Statics2),
1940 get_achieval(A,Dec,T,Dec1,T1,ACH),
1941 env_assert(methodC(A,PreR,PostR,Statics2,T1,achieve(ACH),Dec1)),
1942 fail.
1943change_op_representation :-
1944 env_call(operator(A,B,C,D)),
1945 make_ss_to_se(B,B0),
1946 make_se_primitive(B0,B1),
1947 make_sc_primitive(C,C1),
1949 1950 get_preconditions(C1,B1,Pre,Post),
1951 rem_statics(Post, PostR,St1),
1952 rem_statics(Pre, PreR,St2),
1953 append_dcut(St1,St2,Statics1),
1954 remove_unneed(Statics1,[],Statics),
1955 statics_consist(Statics),
1956 env_assert(operatorC(A,PreR,PostR,D,Statics)),
1957 fail.
1958change_op_representation:-
1959 env_retractall(current_num(sm,_)),!.
1960
1961get_preconditions([],Prev,Prev,Prev) :-!.
1962get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From1)|Pre],[se(S,X,To1)|Post]):-
1963 member_e(se(S,X,PSE),Prev),
1964 append_dcut(PSE,From,From1),
1965 append_dcut(PSE,To,To1),
1966 list_take(Prev,[se(S,X,PSE)],Prev1),
1967 get_preconditions(Rest,Prev1, Pre,Post),!.
1968get_preconditions([sc(S,X,From =>To)|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
1969 get_preconditions(Rest,Prev, Pre,Post),!.
1970get_preconditions([],Prev,Prev,Prev) :-!.
1971
1973get_achieval(A,Dec,T,Dec1,T1,Achieval):-
1974 env_retractall(current_num(sm,_)),
1975 make_dec(A,Dec,Dec1,T,T1,[],Achieval),!.
1976make_dec(A,[],[],Temp,Temp,Achieval,Achieval):-!.
1977make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1978 HD=..[achieve|Goal],
1979 env_call current_num(sm,Num),
1980 replace_achieval_temp(Temp,Temp0,Num),
1981 make_ss_to_se(Goal,Goal0),
1982 append_dcut(Achieval,Goal0,Achieval0),
1983 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1),!.
1984make_dec(A,[HD|TD],TD1,Temp,Temp1,Achieval,Achieval1):-
1985 HD=..[achieve|Goal],
1986 not(env_call current_num(sm,_)),
1987 replace_achieval_temp(Temp,Temp0,1),
1988 make_ss_to_se(Goal,Goal0),
1989 append_dcut(Achieval,Goal0,Achieval0),
1990 make_dec(A,TD,TD1,Temp0,Temp1,Achieval0,Achieval1).
1991make_dec(A,[HD|TD],[HD|TD1],Temp,Temp1,Achieval,Achieval1):-
1992 HD=..[DecName|Goal],
1993 DecName\==achieve,
1994 gensym_special(sm,SM),
1995 env_call current_num(sm,Num),
1996 make_dec(A,TD,TD1,Temp,Temp1,Achieval,Achieval1),!.
1997
1999replace_achieval_temp(Temp,Temp1,Num):-
2000 change_all_numbers(Temp,Num,Temp00),
2001 tidy_temp(Temp00,Temp1).
2002
2003change_all_numbers([],Num,[]):-!.
2004change_all_numbers([HTemp|TTemp],Num,[HTemp00|TTemp00]):-
2005 HTemp=..[before|Nums],
2006 change_nums(Nums,Num,Nums1),
2007 HTemp00=..[before|Nums1],
2008 change_all_numbers(TTemp,Num,TTemp00).
2009
2010change_nums([],Num,[]):-!.
2011change_nums([Num1|TN],Num,[Num1|TN1]):-
2012 Num1<Num,
2013 change_nums(TN,Num,TN1),!.
2014change_nums([Num1|TN],Num,[Num2|TN1]):-
2015 Num1>Num,
2016 Num2 is Num1-1,
2017 change_nums(TN,Num,TN1),!.
2018change_nums([Num|TN],Num,[0|TN1]):-
2019 change_nums(TN,Num,TN1),!.
2020
2022tidy_temp(Temp,Temp1):-
2023 member(before(Num,0),Temp),
2024 list_take(Temp,[before(Num,0)],Temp0),
2025 change_laters(Temp0,Num,Temp01),
2026 tidy_temp(Temp01,Temp1).
2027tidy_temp([],[]):-!.
2028tidy_temp([before(0,Num)|Temp],Temp0):-
2029 tidy_temp(Temp,Temp0),!.
2030tidy_temp([before(Num1,Num2)|Temp],[before(Num1,Num2)|Temp0]):-
2031 tidy_temp(Temp,Temp0),!.
2032
2033change_laters([before(0,Num2)|Temp],Num,[before(Num,Num2)|Temp0]):-
2034 change_laters(Temp,Num,Temp0).
2035change_laters([before(Num1,0)|Temp],Num,[before(Num1,0)|Temp0]):-
2036 change_laters(Temp,Num,Temp0).
2037change_laters([before(Num1,Num2)|Temp],Num,[before(Num1,Num2)|Temp0]):-
2038 change_laters(Temp,Num,Temp0).
2039
2041make_se_primitive([],[]).
2042make_se_primitive([se(Sort,Obj,ST)|SE],[se(Sort,Obj,ST)|SE0]):-
2043 env_call objectsC(Sort,Objls),!,
2044 make_se_primitive(SE,SE0).
2045make_se_primitive([se(Sort,Obj,ST)|SE],[se(PSort,Obj,ST)|SE0]):-
2046 find_prim_sort(Sort,PSorts),
2047 member(PSort,PSorts),
2048 make_se_primitive(SE,SE0).
2049
2051make_sc_primitive([],[]).
2052make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(Sort,Obj,SE1=>SE2)|ST0]):-
2053 env_call objectsC(Sort,Objls),!,
2054 make_sc_primitive(ST,ST0).
2055make_sc_primitive([sc(Sort,Obj,SE1=>SE2)|ST],[sc(PSort,Obj,SE1=>SE2)|ST0]):-
2056 find_prim_sort(Sort,PSorts),
2057 member(PSort,PSorts),
2058 make_sc_primitive(ST,ST0).
2059
2060
2063make_tn(TN,Name,Pre,Post,Temp,Dec):-
2064 find_only_changed(Pre,Post,[],Pre1,[],Post1),
2065 not(isemptylist(Post1)),
2066 not(exist_tn(Pre,Post)),
2067 gensym_special(tn,TN),
2069 env_assert(tn(TN,Name,Pre1,Post1,Temp,Dec)),!.
2070
2071exist_tn(Pre,Post):-
2072 env_call(tn(_,_,Pre,Post1,_,_)),
2073 state_achieved(Post,Post1),!.
2074find_only_changed([],[],Pre,Pre,Post,Post):-!.
2076find_only_changed([se(Sort,Obj,ST)|Pre],[se(Sort,Obj,ST)|Post],Pre0,Pre1,Post0,Post1):-
2077 find_only_changed(Pre,Post,Pre0,Pre1,Post0,Post1),!.
2078find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
2079 member(se(Sort,Obj,ST1),Post),
2080 list_take(Post,[se(Sort,Obj,ST1)],Post2),
2081 append_changed(se(Sort,Obj,ST),se(Sort,Obj,ST1),Pre0,Pre3,Post0,Post3),
2082 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
2083find_only_changed([se(Sort,Obj,ST)|Pre],Post,Pre0,Pre1,Post0,Post1):-
2084 member(se(SortN,Obj,ST1),Post),
2085 list_take(Post,[se(SortN,Obj,ST1)],Post2),
2086 append_changed(se(Sort,Obj,ST),se(SortN,Obj,ST1),Pre0,Pre3,Post0,Post3),
2087 find_only_changed(Pre,Post2,Pre3,Pre1,Post3,Post1),!.
2089
2092append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre0,Post0,Post0):-
2093 state_match(Sort,Obj,ST,ST1),!.
2094append_changed(se(Sort,Obj,ST),se(Sort1,Obj,ST1),Pre0,Pre3,Post0,Post3):-
2095 append_dcut(Pre0,[se(Sort,Obj,ST)],Pre3),
2096 append_dcut(Post0,[se(Sort,Obj,ST1)],Post3),!.
2097
2099push_to_primitive([],PHPs,PHPs,TNLst,TNLst) :-!.
2100push_to_primitive([step(HPID,_,_,_,exp(TN))|HPs],List,PHPs,TNSoFar,TNFinal) :-
2101 env_call(tn(TN,Name,Pre,Post,Temp,Dec)),
2102 push_to_primitive(Dec,List,Dec1,[tn(TN,Name,Pre,Post,Temp,Dec)|TNSoFar],TNNext),
2103 push_to_primitive(HPs,Dec1,PHPs,TNNext,TNFinal),!.
2104push_to_primitive([step(HPID,_,_,_,exp(Name))|HPs],List,PHPs,TNSoFar,TNFinal):-
2105 append_dcut(List,[Name],List1),
2106 push_to_primitive(HPs,List1,PHPs,TNSoFar,TNFinal),!.
2107
2109
2110possibly_before(I,J,Temps) :-
2111 \+ necessarily_before(J,I,Temps), !.
2112
2113necessarily_before(J,I,Temps) :-
2114 member(before(J,I),Temps),!.
2115necessarily_before(J,I,Temps) :-
2116 member(before(J,Z),Temps),
2117 necessarily_before(Z,I,Temps),!.
2118
2119select_node(node(Name,Pre,Temp,Decomp,Statics)) :-
2120 env_retract(node(Name,Pre,Temp,Decomp,Statics)),
2125 !.
2126
2127get_obj_prim_sort([],[]):-!.
2128get_obj_prim_sort([HSort|TV],[HObj|TS]):-
2129 env_call is_of_primitive_sort(HObj,HSort),
2130 get_obj_prim_sort(TV,TS),!.
2139find_all_upper([],[]).
2140find_all_upper([HVars|TV],[HSorts|TS]):-
2141 uppersorts(HSorts,Upsorts),
2142 member(HVars,Upsorts),
2143 find_all_upper(TV,TS).
2144
2146find_prim_sort(Sort,PS):-
2147 subsorts(Sort,Subsorts),
2148 split_prim_noprim(Subsorts,PS,NP),!.
2149
2151get_sort_objects(Sort,Objs):-
2152 find_prim_sort(Sort,PSorts),
2153 get_objects1(PSorts,Objls),
2154 flatten(Objls,[],Objs),!.
2155
2156get_objects1([],[]):-!.
2157get_objects1([PS1|RS],[Objls1|Objls]):-
2158 env_call objectsC(PS1,Objls1),
2159 get_objects1(RS,Objls),!.
2160
2163subsortse(Sort,Subsorts):-
2164 subsorts(Sort,Subsorts1),
2165 subtract(Subsorts1,[Sort],Subsorts),!.
2167subsorts(Sort,Subsorts):-
2168 sort_down([Sort],[],Subsorts),!.
2169
2170sort_down([],Subsorts,Subsorts):-!.
2171sort_down([HOpen|TOpen],List,Subsorts):-
2172 env_call objectsC(HOpen,Objls),
2173 append_dcut(List,[HOpen],List1),
2174 sort_down(TOpen,List1,Subsorts),!.
2175sort_down([HOpen|TOpen],List,Sortslist):-
2176 env_call sorts(HOpen,Sorts),
2177 sort_down(Sorts,List,List2),
2178 sort_down(TOpen,[HOpen|List2],Sortslist),!.
2179sort_down([HOpen|TOpen],List,Sortslist):-
2180 sort_down(TOpen,List,Sortslist),!.
2181
2183uppersortse(Sort,Uppersorts):-
2184 uppersorts(Sort,Uppersortsf),
2185 subtract(Uppersortsf,[Sort],Uppersorts),!.
2187uppersorts(Sort,Uppersorts):-
2188 env_call objectsC(Sort,Objls),
2189 sort_up(Sort,[Sort],Uppersorts),!.
2190uppersorts(Sort,Uppersorts):-
2191 env_call sorts(Sort,Sortls),
2192 sort_up(Sort,[Sort],Uppersorts),!.
2193uppersorts(Obj,Sortls):-
2194 env_call objectsC(Sort,Objls),
2195 member(Obj, Objls),
2196 sort_up(Sort,[Sort],Sortls),!.
2197
2198sort_up(Sort, List,Sortslist):-
2199 env_call sorts(NPSort, NPSortls),
2200 not((special_sorts(PS), NPSort == PS )),
2201 member(Sort,NPSortls),
2202 sort_up(NPSort,[NPSort|List],Sortslist).
2203sort_up(Sort, List,List):-!.
2204
2205special_sorts(primitive_sorts).
2206special_sorts(non_primitive_sorts).
2207
2208
2210sametree(Sort1,Sort2):-
2211 Sort1==Sort2,!.
2212sametree(Sort1,Sort2):-
2213 var(Sort1),!.
2214sametree(Sort1,Sort2):-
2215 var(Sort2),!.
2216sametree(Sort1,Sort2):-
2217 uppersorts(Sort2,Sortls),
2218 member(Sort1,Sortls),!.
2219sametree(Sort1,Sort2):-
2220 uppersorts(Sort1,Sortls),
2221 member(Sort2,Sortls),!.
2222
2224split_prim_noprim([],[],[]):-!.
2225split_prim_noprim([HS|TS],[HS|TP],NP):-
2226 env_call objectsC(HS,Obj),
2227 split_prim_noprim(TS,TP,NP),!.
2228split_prim_noprim([HS|TS],PS,[HS|NP]):-
2229 split_prim_noprim(TS,PS,NP),!.
2230
2232
2234get_invariants(Invs) :-
2235 env_call(atomic_invariantsC(Invs)),!.
2236
2237get_one_isa(S,X,Preds):- \+ is_list(Preds),!,get_one_isa(S,X,[Preds]).
2238get_one_isa(S,X,Preds):-member(is_of_sort(X,S),Preds).
2239get_one_isa(S,X,Preds):-member(ss(S,X,_),Preds).
2240get_one_isa(S,X,Preds):-member(se(S,X,_),Preds).
2241get_one_isa(S,X,Preds):- member(Pred,Preds),Pred=..[F|Args],functor(Pred,F,A),get_one_isa_fa(S,X,F,A,Args).
2242
2243get_one_isa_fa(S,X,S,1,[X]):- is_sort(S).
2244get_one_isa_fa(S,X,_,_,[X|_]):- nonvar(X), is_decl_of_sort(X,S),!.
2245get_one_isa_fa(S,X,F,N,[X|_]):- functor(P,F,N),(env_call predicates(SS)),member(P,SS),arg(1,P,S),S\== physical_obj, nonvar(S).
2246
2247is_decl_of_sort(X,S):- env_call is_of_sort(X,S).
2248is_decl_of_sort(X,S):- env_call objects(S,L),member(X,L).
2250is_decl_of_sort(X,S):- name(X,Codes),append(NameCodes,NumberCodes,Codes),catch(number_codes(Number,NumberCodes),error(syntax_error(_),_),fail),name(Thing,NameCodes),downcase_atom(Thing,S).
2251
2252is_sort(V):-var(V),!,fail.
2253is_sort(obj).
2254is_sort(driver).
2255is_sort(truck).
2256is_sort(ball).
2257is_sort(room).
2258is_sort(gripper).
2259is_sort(S):- (env_call sorts(_,SS)),member(S,SS).
2260is_sort(S):- (env_call sorts(S,_)).
2261
2262rem_statics([sc(S,X,Lhs=>Rhs)|ST], [sc(S,X,LhsR=>RhsR)|STR],Rt1) :-
2263 split_st_dy(Lhs,[],LR, [],LhsR),
2264 split_st_dy(Rhs,[],RR,[],RhsR),
2265 append_dcut(LR,RR,R),
2266 rem_statics(ST, STR,Rt),
2267 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2268rem_statics([ss(S,X,Preds)|Post], [ss(S,X,PredR)|PostR],Rt1) :-
2269 split_st_dy(Preds,[],R, [],PredR),
2270 rem_statics(Post, PostR,Rt),
2271 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2272rem_statics([se(S,X,Preds)|Post], [se(S,X,PredR)|PostR],Rt1) :-
2273 split_st_dy(Preds,[],R, [],PredR),
2274 rem_statics(Post, PostR,Rt),
2275 append_dcut(Rt,[is_of_sort(X,S)|R],Rt1),!.
2276rem_statics([], [],[]) :-!.
2277
2278
2280
2281
2282isemptylist([]):-!.
2283
2290
2291member_cut(X,[X|_]) :- !.
2292member_cut(X,[_|Y]) :- member_cut(X,Y),!.
2293
2295member_e(X,[Y|_]):-
2296 X==Y,!.
2297member_e(X,[Y|L]):-
2298 var(Y),
2299 member_e(X,L),!.
2300member_e(ss(Sort,Obj,SE),[ss(Sort,Obj1,SE)|_]):-
2301 Obj==Obj1,!.
2302member_e(se(Sort,Obj,SE),[se(Sort,Obj1,SE)|_]):-
2303 Obj==Obj1,!.
2304member_e(sc(Sort,Obj,SE1=>SE2),[sc(Sort,Obj1,SE1=>SE2)|_]):-
2305 Obj==Obj1,!.
2306member_e(X,[Y|L]):- member_e(X,L),!.
2307
2308
2311u_mem_cut(_,[]):-!,fail.
2312u_mem_cut(X,[Y|_]) :- X == Y,!.
2313u_mem_cut(X,[_|L]) :- u_mem_cut(X,L),!.
2318obj_member(X,[X|[]]):-!.
2319obj_member(X,List):-
2320 obj_member0(X,List),!.
2321obj_member0(X,[Y|_]):-
2322 var(X),!. 2323obj_member0(X,[Y|_]):-
2324 X==Y,!.
2325obj_member0(X,[_|Y]) :- obj_member0(X,Y),!.
2326
2331pred_member(X,List):-
2332 ground(X),
2333 member(X,List),!.
2334pred_member(X,List):-
2335 setof(X,member(X,List),Refined),
2336 pred_member0(X,Refined),!.
2337pred_member0(X,[X|[]]):-!.
2338pred_member0(X,Y):-
2339 pred_member1(X,Y),!.
2340pred_member1(X,[Y|_]):-
2341 X=..[H|XLs],
2342 Y=..[H|YLs],
2343 vequal(XLs,YLs),!.
2344pred_member1(X,[_|Y]):- pred_member1(X,Y),!.
2345
2346statics_append([],L,L):-
2347 statics_consist(L),!.
2348statics_append(L,[],L):-
2349 statics_consist(L),!.
2350statics_append(List1,List2,L):-
2351 statics_consist(List1),
2352 statics_consist(List2),
2353 statics_append1(List1,List2,[],L),
2354 statics_consist(L),!.
2355
2356statics_append1([],List2,L1,L):-
2357 append_dcut(List2,L1,L),!.
2358statics_append1([H|List1],List2,L,Z) :-
2359 statics_append0(H,List2,L,L1),
2360 statics_append1(List1,List2,L1,Z),!.
2361
2362statics_append0(H,[],L,[H|L]):-!.
2363statics_append0(H,[H|Z],L,L):-!.
2364statics_append0(H,[X|Z],L1,L):-
2365 statics_append0(H,Z,L1,L),!.
2366
2367append_dcut([],L,L):-!.
2368append_dcut([H|T],L,[H|Z]) :- append_dcut(T,L,Z),!.
2369
2370
2371
2372
2377append_st(ST1,ST2,ST):-
2378 append_dcut(ST1,ST2,ST0),
2379 remove_unneed(ST0,[],ST),!.
2380
2383remove_unneed([],C,C):-!.
2384remove_unneed([A|B], Z, C):-
2385 var(A),
2386 member_e(A,Z),
2387 remove_unneed(B, Z, C),! .
2388remove_unneed([A|B], Z, C):-
2389 var(A),
2390 append_dcut(Z,[A],D),
2391 remove_unneed(B, D, C),!.
2392remove_unneed([A|B], Z, C):-
2393 ground(A),
2394 remove_unneed(B, Z, C),!.
2395remove_unneed([A|B], Z, C):-
2396 A=..[ne|Paras],
2397 append_dcut(Z,[A],D),
2398 remove_unneed(B, D, C),!.
2399remove_unneed([A|B], Z, C):-
2400 A=..[Pred|Paras],
2401 same_var_member(A,Z),
2402 remove_unneed(B, Z, C),!.
2403remove_unneed([A|B], Z, C):-
2404 append_dcut(Z,[A],D),
2405 remove_unneed(B, D, C),!.
2406
2407same_var_member(Pred,[Pred1|List]):-
2408 var(Pred1),
2409 same_var_member(Pred,List),!.
2410same_var_member(Pred,[Pred1|List]):-
2411 Pred==Pred1,!.
2412same_var_member(Pred,[Pred1|List]):-
2413 Pred=..[H|T],
2414 Pred1=..[H|T1],
2415 same_var_member1(T,T1),!.
2416same_var_member(Pred,[Pred1|List]):-
2417 same_var_member(Pred,List),!.
2418
2419same_var_member1([],[]):-!.
2420same_var_member1([H1|T],[H2|T]):-
2421 var(H1),
2422 H1==H2,!.
2423same_var_member1([H|T1],[H|T2]):-
2424 var(T1),
2425 T1==T2,!.
2426same_var_member1([H1|T1],[H2|T2]):-
2427 H1==H2,
2428 same_var_member1(T1,T2),!.
2429
2431is_equal_list(List1,List2):-
2432 List1==List2,!.
2433is_equal_list([],[]):-!.
2434is_equal_list(List1,List2):-
2435 length(List1,L),
2436 length(List2,L),
2437 is_equal_list1(List1,List2),!.
2438is_equal_list1([],[]):-!.
2439is_equal_list1([Head1|List1],[Head2|List2]):-
2440 Head1==Head2,
2441 is_equal_list1(List1,List2),!.
2442is_equal_list1([se(Sort,Obj,Head1)|List1],[se(Sort,Obj,Head2)|List2]):-
2443 is_equal_list(Head1,Head2),
2444 is_equal_list1(List1,List2),!.
2445is_equal_list1([Head1|List1],[Head2|List2]):-
2446 Head1=..[FF|Var1],
2447 Head2=..[FF|Var2],
2448 FF\==se,
2449 vequal(Var1,Var2),
2450 is_equal_list1(List1,List2),!.
2451is_equal_list1([Head1|List1],List2):-
2452 member(Head1,List2),
2453 append_dcut(List1,[Head1],List10),
2454 is_equal_list1(List10,List2),!.
2455
2457is_diff(List1,List2):-
2458 length(List1,L1),
2459 length(List2,L2),
2460 L1\==L2,!.
2461is_diff([Head|List1],List2):-
2462 not_exist(Head,List2),!.
2463is_diff([Head|List1],List2):-
2464 list_take(List2,[Head],List21),
2465 is_diff(List1,List21),!.
2466
2467not_exist(Pred,List2):-
2468 not(member(Pred,List2)),!.
2469not_exist(se(Sort,Obj,Head1),List2):-
2470 not(member(se(Sort,Obj,Head),List2)),!.
2471not_exist(se(Sort,Obj,Head1),List2):-
2472 member(se(Sort,Obj,Head2),List2),
2473 is_diff(Head1,Head2),!.
2474
2478set_append([], Z, Z):-! .
2479set_append([A|B], Z, C) :-
2480 not(not(member(A, Z))) ,
2481 set_append(B, Z, C),! .
2482set_append([A|B], Z, [A|C]) :-
2483 set_append(B, Z, C) .
2484
2488set_append_e(A,B,C):-
2489 append_dcut(A,B,D),
2490 remove_dup(D,[],C),!.
2491
2493remove_dup([],C,C):-!.
2494remove_dup([A|B],Z,C) :-
2495 member_e(A, Z),
2496 remove_dup(B, Z, C),! .
2497remove_dup([A|B], Z, C):-
2498 append_dcut(Z,[A],D),
2499 remove_dup(B, D, C),!.
2500
2502vequal([],[]):-!.
2503vequal([X|XLs],[Y|YLs]):-
2504 X==Y,
2505 vequal(XLs,YLs),!.
2506vequal([X|XLs],[Y|YLs]):-
2507 var(X),
2508 vequal(XLs,YLs),!.
2509vequal([X|XLs],[Y|YLs]):-
2510 var(Y),
2511 vequal(XLs,YLs),!.
2512
2513
2524
2526
2527list_take(R,[E|R1],R2):-
2528 remove_el(R,E,RR),
2529 list_take(RR,R1,R2),!.
2530list_take(R,[_|R1],R2):-
2531 list_take(R,R1,R2),!.
2532list_take(A,[],A) :- !.
2533
2536remove_el([],_,[]) :- ! .
2537remove_el([A|B],A,B) :- ! .
2538remove_el([A|B],C,[A|D]) :-
2539 remove_el(B,C,D) .
2540
2542
2543
2544gensym_special(Root,Atom) :-
2545 getnum(Root,Num),
2546 name(Root,Name1),
2547 name(Num,Name2),
2548 append_dcut(Name1,Name2,Name),
2549 name(Atom,Name).
2550
2551getnum(Root,Num) :-
2552 env_retract(current_num(Root,Num1)),!,
2553 Num is Num1+1,
2554 env_asserta(current_num(Root,Num)).
2555
2556getnum(Root,1) :- env_asserta(current_num(Root,1)).
2557
2558gensym_num(Root,Num,Atom):-atom_concat(Root,Num,Atom),!.
2559gensym_num(Root,Num,Atom):-
2560 name(Root,Name),
2561 name(Num,Name1),
2562 append_dcut(Name,Name1,Name2),
2563 name(Atom,Name2),!.
2564
2565
2566pprint([],SIZE,SIZE):-!.
2567pprint([HS|TS],Size0,SIZE):-
2568 is_list(HS),
2569 pprint(HS,Size0,Size1),
2570 pprint(TS,Size1,SIZE),!.
2571pprint([HS|TS],Size0,SIZE):-
2574 Size1 is Size0+1,
2575 pprint(TS,Size1,SIZE),!.
2576
2578
2579split_st_dy([],ST,ST,DY,DY):-!.
2580split_st_dy([Pred|TStates],ST0,ST,DY0,DY):-
2581 is_statics(Pred),
2582 append_dcut(ST0,[Pred],ST1),
2583 split_st_dy(TStates,ST1,ST,DY0,DY),!.
2584split_st_dy([Pred|TStates],ST0,ST,DY0,DY):-
2585 append_dcut(DY0,[Pred],DY1),
2586 split_st_dy(TStates,ST0,ST,DY1,DY),!.
2587
2589
2590flatten([HO|TO], List, O_List):-
2591 append_dcut(HO, List, List_tmp),
2592 flatten(TO, List_tmp, O_List),!.
2593flatten([H|TO], List,O_List):-
2594 append_dcut([H], List, List_tmp),
2595 flatten(TO, List_tmp, O_List).
2596flatten([], [HList|T], O_List):-
2597 HList = [],
2598 flatten(T, [], O_List).
2599flatten([], [HList|T], O_List):-
2600 is_list(HList),
2601 flatten([HList|T],[], O_List),!.
2602flatten([], L,L):-!.
2603
2605set_flatten([HO|TO], List, O_List):-
2606 set_append_e(HO, List, List_tmp),
2607 set_flatten(TO, List_tmp, O_List),!.
2608set_flatten([H|TO], List,O_List):-
2609 set_append_e([H], List, List_tmp),
2610 set_flatten(TO, List_tmp, O_List).
2611set_flatten([], [HList|T], O_List):-
2612 HList = [],
2613 set_flatten(T, [], O_List).
2614set_flatten([], [HList|T], O_List):-
2615 is_list(HList),
2616 set_flatten([HList|T],[], O_List),!.
2617set_flatten([], L,L):-!.
2618
2619
2630
2631reverse_unused(L,RL) :-
2632 revSlave(L,[],RL).
2633
2634revSlave([],RL,RL).
2635revSlave([H|T],Sofar,Final) :-
2636 revSlave(T,[H|Sofar],Final).
2637
2639:- assert_if_new(time_taken(0)). 2640:- assert_if_new(sum(0)). 2641:- assert_if_new(soln_size(0)). 2642
2643solve(N,FN):-
2644 N < FN,
2645 nl,write('task '), write(N),write(': '),nl,
2646 solve(N),
2647 Ni is N+1,
2648 solve(Ni,FN).
2649solve(FN,FN):-
2650 nl,write('task '), write(FN),write(': '),nl,
2651 solve(FN),
2652 retractall(sum(_)),
2653 assert(sum(0)),
2654 sum_time(CP),
2655 retractall(sum(_)),
2656 assert(sum(0)),
2657 sum_size(SIZE),
2658 TIM is CP /1000,
2659 retractall(time_taken(_)),
2660 retractall(soln_size(_)),
2661 nl,write('total time '),write(TIM),write(' seconds'),
2662 nl,write('total size '),write(SIZE),nl.
2663solve(N,N).
2664
2665sum_time(TIM):-
2666 time_taken(CP),
2667 env_retract(sum(N)),
2668 N1 is N +CP,
2669 assert(sum(N1)),
2670 fail.
2671sum_time(TIM):-
2672 sum(TIM).
2673sum_size(SIZE):-
2674 soln_size(S),
2675 retract(sum(N)),
2676 N1 is N +S,
2677 assert(sum(N1)),
2678 fail.
2679sum_size(SIZE):-
2680 sum(SIZE).
2681
2682stoppoint.
2684state_related(Post,Cond,undefd):-!.
2685state_related(Post,Cond,[]):-!.
2686state_related(Post,Cond,State2):-
2687 append_dcut(Post,Cond,State1),
2688 state_related(State1,State2).
2689
2692state_related([se(Sort,Obj,SE1)|State1],State2):-
2693 member(se(Sort,Obj,SE2),State2),
2694 state_related0(SE1,SE2).
2696state_related([sc(Sort1,Obj,SE1=>SS1)|State1],State2):-
2697 member(se(Sort,Obj,SE2),State2),
2698 env_call is_of_sort(Obj,Sort1),
2699 env_call is_of_sort(Obj,Sort).
2700state_related([se(Sort,Obj,SE)|State1],State2):-
2701 state_related(State1,State2),!.
2702state_related([sc(Sort,Obj,SE=>SS)|State1],State2):-
2703 state_related(State1,State2),!.
2704
2706state_related0([],SE2):-!.
2707state_related0([Head|SE1],SE2):-
2708 member(Head,SE2),
2709 state_related0(SE1,SE2).
2710state_related0([Head|SE1],SE2):-
2711 state_related0(SE1,SE2).
2712
2715change_obj_list(I):-
2716 find_dynamic_objects(I),
2717 collect_dynamic_obj,
2718 change_obj_list1,
2719 change_atomic_inv,!.
2720
2721change_obj_list1:-
2722 forall(objects(Sort,_),change_obj_list2(Sort)).
2723
2725change_obj_list2(Sort):-
2726 env_call(objectsC(Sort,Objls)),!.
2728change_obj_list2(Sort):-
2729 findall(Obj1s,env_call(objects(Sort,Objls)),Lists),
2730 flatten(Lists,Objls),
2731 env_assert(objectsC(Sort,Objls)),!.
2732
2734change_atomic_inv:-
2735 findall(Atom1,(env_call(atomic_invariants(Atom)),change_atomic_inv1(Atom,Atom1)),Lists),
2736 flatten(Lists,List),
2737 env_assert(atomic_invariantsC(List)),!.
2738change_atomic_inv.
2739
2740change_atomic_inv1([],[]).
2741change_atomic_inv1([Pred|Atom],[Pred|Atom1]):-
2742 Pred=..[Name|Objs],
2743 just_dynamic_objects(Objs),
2744 change_atomic_inv1(Atom,Atom1).
2745change_atomic_inv1([Pred|Atom],Atom1):-
2746 change_atomic_inv1(Atom,Atom1).
2747
2748just_dynamic_objects([]).
2749just_dynamic_objects([Head|Objs]):-
2750 env_call(objectsC(Sort,Objls)),
2751 member(Head,Objls),!,
2752 just_dynamic_objects(Objs).
2753
2754find_dynamic_objects([]):-!.
2755find_dynamic_objects([SE|Rest]):-
2756 find_dynamic_objects(SE),
2757 find_dynamic_objects(Rest),!.
2758find_dynamic_objects(ss(Sort,Obj,List)):-
2759 env_assert(objectsD(Sort,Obj)),!,
2760 forall((member(is_of_sort(X,S),List),
2761 nonvar(X),nonvar(S)),
2762 env_assert(objectsD(S,X))),!.
2763
2764collect_dynamic_obj:-
2765 env_call(objectsD(Sort,_)),
2766 setof(Obj, env_call(objectsD(Sort,Obj)), Objls),
2767 env_retractall(objectsD(Sort,_)),
2768 env_assert(objectsC(Sort,Objls)),
2769 fail.
2770collect_dynamic_obj.
2771
2772get_preconditions_g([],Prev,Prev,Prev):-!.
2773get_preconditions_g([sc(S,X,(From =>To))|Rest],Prev,[se(S,X,From)|Pre],[se(S,X,To)|Post]):-
2774 !,
2775 get_preconditions_g(Rest,Prev, Pre,Post).
2776
2779
2780ground_op :-
2781 assert_sort_objects,
2782 enumerateOps,
2783 instOps,
2784 flag(opCounter,Top,Top),
2785 write(opCounter = Top),nl,!.
2786
2787enumerateOps :-
2788 flag(opCounter,_,1),
2789 enumOps.
2790
2791enumOps :-
2792 env_call operator(Name,Prev,Nec,Cond),
2793 flag(opCounter,Count,Count),
2794 containsInvars(operator(Name,Prev,Nec,Cond),InVars,IsOfSorts,FPrev,FNec),
2795 2796 findVarsAndTypes(operator(Name,Prev,Nec,Cond),VT,NEs),
2797 env_assert(opParent(Count,operator(Name,FPrev,FNec,Cond),VT,NEs,InVars,IsOfSorts)),
2798 Next is Count + 1,
2799 flag(opCounter,Count,Next),
2800 fail.
2801
2802enumOps.
2803
2804
2813
2814
2815findVarsAndTypes(operator(_,Pre,Nec,Cond),Vars,NEs) :-
2816 vtPrevail(Pre,PreVars,PreNEs),
2817 vtEffects(Nec,NecVars,NecNEs),
2818 append_dcut(PreVars,NecVars,Vars),
2819 append_dcut(PreNEs,NecNEs,NEs),
2820 !.
2821
2824
2825vtEffects([],[],[]).
2826
2827vtEffects([sc(Type,Obj1,Preds)|Rest],VT,NEs) :-
2828 vtPreds(Preds,Related,NEs1),
2829 append_dcut([Type,Obj1],Related,Obj1VT),
2830 vtEffects(Rest,RestVT,RestNEs),
2831 append_dcut(Obj1VT,RestVT,VT),
2832 append_dcut(NEs1,RestNEs,NEs).
2833
2836
2837vtPrevail([],[],[]).
2838
2839vtPrevail([se(Type,Obj1,Preds)|Rest],VT,NEs) :-
2840 vtPLst(Preds,Related,NEs1),
2841 append_dcut([Type,Obj1],Related,Obj1VT),
2842 vtPrevail(Rest,RestVT,RestNEs),
2843 append_dcut(Obj1VT,RestVT,VT),
2844 append_dcut(NEs1,RestNEs,NEs).
2845
2848
2849vtPreds((Pre => Add),Res,NEs) :-
2850 vtPLst(Pre,VTPre,NEs1),
2851 vtPLst(Add,VTAdd,NEs2),
2852 append_dcut(VTPre,VTAdd,Res),
2853 append_dcut(NEs1,NEs2,NEs).
2854
2857
2858vtPLst([],[],[]).
2859
2860vtPLst([ne(X,Y)|Rest],Res,[ne(X,Y)|RestNEs]) :-
2861 !,
2862 vtPLst(Rest,Res,RestNEs).
2863
2864vtPLst([Pred|Preds],Res,NEs) :-
2865 functor(Pred,_,1),
2866 !,
2867 vtPLst(Preds,Res,NEs).
2868
2869vtPLst([is_of_sort(_,_)|Preds],Res,NEs) :-
2870 !,
2871 vtPLst(Preds,Res,NEs).
2872
2876
2877vtPLst([Pred|Preds],Res,NEs) :-
2878 functor(Pred,Name,Arity),
2879 Pred =.. [Name,Obj1|Rest],
2880 VNeeded is Arity - 1,
2881 createVarList(VNeeded,VN),
2882 DummyPred =.. [Name,X|VN],
2883 env_call predicates(PList),
2884 member(DummyPred,PList),
2885 pair(VN,Rest,This),
2886 vtPLst(Preds,RestPre,NEs),
2887 append_dcut(This,RestPre,Res).
2888
2891
2892createVarList(1,[X]) :-
2893 !.
2894
2895createVarList(N,[X|Rest]) :-
2896 Next is N - 1,
2897 createVarList(Next,Rest).
2898
2901
2902pair([],[],[]).
2903
2904pair([Type|Types],[Var|Vars],[Type,Var|Rest]) :-
2905 pair(Types,Vars,Rest).
2906
2907
2908
2913
2914instOps :-
2915 flag(opCounter,_,1),
2916 env_call opParent(No,Operator,VT,NEs,InVars,IsOfSorts),
2917 checkIsOfSorts(IsOfSorts),
2918 checkInVars(InVars),
2919 chooseVals(VT,NEs,InVars,Vals),
2920 obeysNEs(NEs),
2921 flag(opCounter,Count,Count),
2922 (operator(Name,Prev,Nec,Cond) = Operator),
2923 filterSE(Prev,FPrev),
2924 filterSC(Nec,FNec),
2925 env_assert(gOperator(Count,No,operator(Name,FPrev,FNec,Cond))),
2926 Next is Count + 1,
2927 flag(opCounter,Count,Next),
2928 fail.
2929
2930instOps.
2931
2932
2933checkInVars([]):- !.
2934checkInVars(Preds):-
2935 env_call(atomic_invariantsC(Invars)),
2936 doCheckInvars(Preds,Invars).
2937
2938doCheckInvars([],_).
2939doCheckInvars([Pred|Rest],Invars) :-
2940 member(Pred,Invars),
2941 doCheckInvars(Rest,Invars).
2942
2943checkIsOfSorts([]).
2944checkIsOfSorts([is_of_sort(V,Sort)|Rest]) :-
2945 env_call objectsOfSort(Sort,Objs),
2946 member(V,Objs),
2947 checkIsOfSorts(Rest).
2948
2949
2951
2952filterSE([],[]) :- !.
2953filterSE([se(Sort,Id,Preds)|Rest],[se(Sort,Id,FPreds)|FRest]) :-
2954 filterPreds(Preds,FPreds),!,
2955 filterSE(Rest,FRest).
2956
2958
2959filterSC([],[]) :- !.
2960filterSC([sc(Sort,Id,(Pre => Post))|Rest],[sc(Sort,Id,(FPre => FPost))|FRest]) :-
2961 filterPreds(Pre,FPre),
2962 filterPreds(Post,FPost),
2963 !,
2964 filterSC(Rest,FRest).
2965
2967
2968filterPreds([],[]).
2969filterPreds([ne(_,_)|Rest],FRest) :-
2970 !,
2971 filterPreds(Rest,FRest).
2972filterPreds([is_of_sort(_,_)|Rest],FRest) :-
2973 !,
2974 filterPreds(Rest,FRest).
2980filterPreds([H|T],[H|FT]) :-
2981 filterPreds(T,FT).
2982
2983
2985
2986collectAllConds(_,_,_,_,[],[]) :- !.
2987
2988collectAllConds(CondVT,NEs,InVars,CondVals,Cond,_) :-
2989 env_retractall(temp_assertIndivConds(_)),
2990 chooseVals(CondVT,NEs,InVars,Vals),
2991 assertIndivConds(Cond),
2992 fail.
2993
2994collectAllConds(_,_,_,_,_,NewConds) :-
2995 setof(Cond,env_call(temp_assertIndivConds(Cond)),NewConds),
2996 dmsg(collectAllConds=NewConds),!.
2997
2998assertIndivConds([]) :- !.
2999
3000assertIndivConds([H|T]) :-
3001 env_assert(temp_assertIndivConds(H)),
3002 assertIndivConds(T).
3003
3005
3006containsInvars(operator(Name,Prev,Nec,Cond),InVars,IsOfSorts,FPrev,FNec) :-
3007 prevInvars(Prev,PInVars,PIsOfSorts,FPrev),
3008 necInvars(Nec,NecInVars,NIsOfSorts,FNec),
3009 append_dcut(NecInVars,PInVars,InVars),
3010 append_dcut(PIsOfSorts,NIsOfSorts,IsOfSorts),
3011 !.
3012
3013prevInvars([],[],[],[]).
3014prevInvars([se(Type,Obj,Props)|Rest],InVars,IsOfSorts,[se(Type,Obj,FProps)|RFPrev]) :-
3015 propsInvars(Props,PInvars,PIsOfSorts,FProps),
3016 prevInvars(Rest,RInVars,RIsOfSorts,RFPrev),
3017 append_dcut(PInVars,RInVars,InVars),
3018 append_dcut([is_of_sort(Obj,Type)|PIsOfSorts],RIsOfSorts,IsOfSorts).
3019
3020necInvars([],[],[],[]).
3021necInvars([sc(Type,Obj,(Props => Adds))|Rest],Invars,IsOfSorts,[sc(Type,Obj,(FProps => FAdds))|RFNec]) :-
3022 propsInvars(Props,PInvars,PIsOfSorts,FProps),
3023 propsInvars(Adds,AInvars,AIsOfSorts,FAdds),
3024 necInvars(Rest,RInvars,RIsOfSorts,RFNec),
3025 append_dcut(AInvars,PInvars,Temp),
3026 append_dcut(Temp,RInvars,Invars),
3027 append_dcut(PIsOfSorts,AIsOfSorts,SortsTemp),
3028 append_dcut([is_of_sort(Obj,Type)|SortsTemp],RIsOfSorts,IsOfSorts).
3029
3030propsInvars([],[],[],[]).
3031propsInvars([Prop|Props],[Prop|Rest],IsOfSorts,FProps) :-
3032 isInvariant(Prop),
3033 !,
3034 propsInvars(Props,Rest,IsOfSorts,FProps).
3035propsInvars([is_of_sort(X,Y)|Props],InVars,[is_of_sort(X,Y)|IsOfSorts],FProps):-
3036 !,
3037 propsInvars(Props,InVars,IsOfSorts,FProps).
3038
3039propsInvars([Pred|Props],Rest,IsOfSorts,[Pred|FProps]) :-
3040 propsInvars(Props,Rest,IsOfSorts,FProps).
3041
3042isInvariant(Prop) :-
3043 env_call atomic_invariantsC(Invars),
3044 functor(Prop,Name,Arity),
3045 createVarList(Arity,VN),
3046 Pred =.. [Name | VN],
3047 member(Pred,Invars).
3048
3052
3053chooseVals([],_,_,[]).
3054
3055chooseVals([Type,Var|TypeVars],NEs,InVars,Vals) :-
3056 ground(Var),
3057 !,
3058 chooseVals(TypeVars,NEs,InVars,Vals).
3059
3060chooseVals([Type,Var|TypeVars],NEs,InVars,[Var|Vals]) :-
3061 env_call objectsOfSort(Type,AllVals),
3062 member(Var,AllVals),
3063 chooseVals(TypeVars,NEs,InVars,Vals).
3070assert_sort_objects :-
3071 env_call objectsC(Type,Objects),
3072 env_assert(objectsOfSort(Type,Objects)),
3073 fail.
3074
3075assert_sort_objects :-
3076 env_call sorts(Type,SubTypes),
3077 not((special_sorts(PS), Type == PS )),
3078 all_objects(Type,Objs),
3079 env_assert(objectsOfSort(Type,Objs)),
3080 fail.
3081
3082assert_sort_objects.
3083
3084all_objects(Type,Objs) :-
3085 env_call objectsC(Type,Objs),
3086 !.
3087all_objects(Type,Objs) :-
3088 env_call sorts(Type,SubSorts),
3089 !,
3090 collect_subsort_objects(SubSorts,Objs).
3091
3092collect_subsort_objects([],[]).
3093collect_subsort_objects([Sort|Rest],Objs ) :-
3094 all_objects(Sort,SortObjs),
3095 !,
3096 collect_subsort_objects(Rest,RestObjs),
3097 append_dcut(SortObjs,RestObjs,Objs).
3098
3099obeysNEs([]).
3100
3101obeysNEs([ne(V1,V2)|Rest]) :-
3102 V1 \== V2, dif(V1,V2), 3103 obeysNEs(Rest),!.
3104
3105obeysInVars([]).
3106obeysInVars([Prop|Rest]) :-
3107 env_call(atomic_invariantsC(Invars)),
3108 member(Prop,Invars),
3109 !.
3110
3114
3115
3117
3118prettyPrintOp(gOperator(No,Par,Op)) :-
3119 write('gOperator('),
3120 write(No),write(','),
3121 write(Par),write(','),nl,
3122 writeOp(4,Op),
3123 !.
3124
3125writeOp(TabVal,operator(Name,Prev,Nec,Cond)) :-
3126 tab(TabVal),
3127 write('operator('),write(Name),write(','),nl,
3128 tab(8),write('% Prevail'),nl,
3129 tab(8),write('['),nl,
3130 writePrevailLists(8,Prev),
3131 tab(8),write('],'),nl,
3132 tab(8),write('% Necessary'),nl,
3133 tab(8),write('['),nl,
3134 writeChangeLists(10,Nec),
3135 tab(8),write('],'),nl,
3136 tab(8),write('% Conditional'),nl,
3137 tab(8),write('['),nl,
3138 writeChangeLists(10,Cond),
3139 tab(8),write('])).'),nl.
3140
3141writePropList(TabVal,[]) :-
3142 tab(TabVal),
3143 write('[]').
3144
3145writePropList(TabVal,[ne(_,_)|Props]) :-
3146 !,
3147 writePropList(Indent,Props).
3148
3149writePropList(TabVal,[Prop|Props]) :-
3150 env_call(atomic_invariantsC(Invars)),
3151 member(Prop,Invars),
3152 writePropList(TabVal,Props).
3153
3154writePropList(TabVal,[Prop|Props]) :-
3155 tab(TabVal),
3156 write('['),
3157 write(Prop),
3158 Indent is TabVal + 1,
3159 writePList(Indent,Props).
3160
3161writePList(TabVal,[]) :-
3162 nl,
3163 tab(TabVal),
3164 write(']').
3165
3166writePList(TabVal,[ne(_,_)]) :-
3167 !,
3168 nl,
3169 tab(TabVal),
3170 write(']').
3171
3172writePList(TabVal,[Prop]) :-
3173 env_call(atomic_invariantsC(Invars)),
3174 member(Prop,Invars),
3175 !,
3176 nl,
3177 tab(TabVal),
3178 write(']').
3179
3180writePList(TabVal,[Prop]) :-
3181 write(','),
3182 nl,
3183 tab(TabVal),
3184 write(Prop),
3185 write(']').
3186
3187writePList(TabVal,[ne(_,_),P2|Rest]) :-
3188 !,
3189 writePList(TabVal,[P2|Rest]).
3190
3191writePList(TabVal,[Prop,P2|Rest]) :-
3192 env_call(atomic_invariantsC(Invars)),
3193 member(Prop,Invars),
3194 !,
3195 writePList(TabVal,[P2|Rest]).
3196
3197writePList(TabVal,[P1,P2|Rest]) :-
3198 write(','),
3199 nl,
3200 tab(TabVal),
3201 write(P1),
3202 writePList(TabVal,[P2|Rest]).
3203
3204writeChangeLists(_,[]).
3205
3206writeChangeLists(TabVal,[sc(Type,Obj,(Req => Add))|Rest]) :-
3207 tab(TabVal),
3208 write('sc('),write(Type),write(','),write(Obj),write(',('),nl,
3209 Indent is TabVal + 12,
3210 writePropList(Indent,Req),
3211 nl,
3212 tab(Indent),
3213 write('=>'),
3214 nl,
3215 writePropList(Indent,Add),
3216 write('))'),writeComma(Rest),
3217 nl,
3218 writeChangeLists(TabVal,Rest).
3219
3220writeComma([]).
3221writeComma(_) :-
3222 write(',').
3223
3224writePrevailLists(_,[]).
3225
3226writePrevailLists(TabVal,[se(Type,Obj,Props)|Rest]) :-
3227 tab(TabVal),
3228 write('se('),write(Type),write(','),write(Obj),write(','),nl,
3229 Indent is TabVal + 12,
3230 writePropList(Indent,Props),
3231 write(')'),writeComma(Rest),
3232 nl,
3233 writePrevailLists(TabVal,Rest).
3234
3235
3236assert_is_of_sort :-
3237 env_call objectsOfSort(Type,Objects),
3238 member(Obj,Objects),
3239 assert_is_of_sort1(Type,Obj),
3240 fail.
3241assert_is_of_sort :-
3242 env_call objectsC(Type,Objects),
3243 member(Obj,Objects),
3244 assert_is_of_primitive_sort(Type,Obj),
3245 fail.
3246assert_is_of_sort.
3247
3248assert_is_of_sort1(Type,Obj):- env_assert(is_of_sort(Obj,Type)),!.
3249assert_is_of_primitive_sort(Type,Obj):-
3250 env_assert(is_of_primitive_sort(Obj,Type)),!.
3251
3254prim_substate_class:-
3255 env_call substate_classes(Sort,Obj,Substate),
3256 find_prim_sort(Sort,PS),
3257 assert_subclass(PS,Obj,Substate),
3258 fail.
3259prim_substate_class:-
3260 collect_prim_substates.
3261
3262assert_subclass([],Obj,Substate).
3263assert_subclass([HS|TS],Obj,Substate):-
3264 env_assert(gsstates(HS,Obj,Substate)),
3265 assert_subclass(TS,Obj,Substate).
3266
3267collect_prim_substates:-
3268 env_call gsstates(Sort,Obj,_),
3269 setof(SStates,(env_call gsstates(Sort,Obj,SStates)),GSStates),
3270 env_retractall(gsstates(Sort,Obj,_)),
3271 all_combined(GSStates,GSStates0),
3272 env_assert(gsubstate_classes(Sort,Obj,GSStates0)),
3273 fail.
3274collect_prim_substates.
3275
3276all_combined(SStates,CSStates):-
3277 xprod(SStates,CSStates1),
3278 flat_interal(CSStates1,CSStates),!.
3279
3280flat_interal([],[]):-!.
3281flat_interal([HSS1|TSS1],[HSS|TSS]):-
3282 flatten(HSS1,[],HSS),
3283 flat_interal(TSS1,TSS),!.
3284
3287xprod(A,B,C) :-
3288 xprod([A,B],C) .
3289
3290xprod([],[]).
3291xprod(A,E) :-
3292 xprod(A,B,C,D) ,
3293 F =..[^,env_call(C),D] ,
3294 call(setof(B,F,E)) .
3295
3296xprod([X],[A],A,member(A,X)) .
3297xprod([X,Y],[A,B],C,(D,E)) :-
3298 C =..[^,A,B] ,
3299 D =..[member,A,X] ,
3300 E =..[member,B,Y] .
3301xprod([X|Y],[A|E],D,(F,G)) :-
3302 D =..[^,A,C] ,
3303 F =..[member,A,X] ,
3304 xprod(Y,E,C,G).
3305
3306
3307:-retractall(solution_file(_)). 3308:-asserta(solution_file(user)). 3309
3312
3313
3314
3315lws:- listing(ocl:[method,
3316operator,implied_invariant,atomic_invariants,inconsistent_constraint,predicates,objects,substate_classes,sorts,domain_name,planner_task_slow,planner_task,
3317htn_task,tp_node,tn,current_num,goal_related,goal_related_search,solved_node,closed_node,tp_goal,final_node,node,op_score,gsstates,gsubstate_classes,related_op,
3318objectsOfSort,atomic_invariantsC,objectsD,objectsC,gOperator,operatorC,opParent,methodC,is_of_sort,is_of_primitive_sort,temp_assertIndivConds]).
3319
3320lws(F):-tell(F),lws,told.
3321
3322:-export(rr/0). 3323:-export(rr1/0). 3324rr:- test_ocl('domains_ocl/chameleonWorld.ocl').
3325rr1:- test_ocl('domains_ocl/translog.ocl').
3326
3327:-rr.
logicmoo_hyhtn_code
% Provides a prolog database env % % % % Logicmoo Project PrologMUD: A MUD server written in Prolog % Maintainer: Douglas Miles % Denton, TX 2005, 2010, 2014 % Dec 13, 2035 % */