How to automate planning program?
The predicate final() takes a term, but uses it as a pattern, so, if I put him an instantiated variable before calling plan predicate, then it didnt work, evaluates empty plan or error, even nothing.
I want to search a plan for various inputs, it
painful to manualy write them every time.
If I rewrite it for constraint solver(standart
modules), this probably, may work...
The question is general, but I'll focus on my program:
import planner.
main => solve_puzzle(Start_state).
solve_puzzle => best_plan_unbounded([b,b,b,c,w,w,w],Plan), writeln(Plan), writef("%i+%i\n",Plan.len(),1).
final([w,w,w,c,b,b,b])=>true.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[Y,c|Z],LastS),
NextS=X++[c,Y]++Z,
Action=move_R,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[c,Y|Z],LastS),
NextS=X++[Y,c]++Z,
Action=move_L,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[Y,Z,c|D],LastS),
NextS=X++[c,Z,Y]++D,
Action=jump_R,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[c,Z,Y|D],LastS),
NextS=X++[Y,Z,c]++D,
Action=jump_L,
ActionCost=1.
It solves one simple puzzle
move black balls "b" and white balls "w" to the opposite side of a list, using one empty space "c" and 4 possible moves: to the near empty space left/right, jump over one ball on empty space left/right.
These puzzle expands easily, so, I wanted to search solutions for bigger versions. Probably, we can put any amount of white or black balls.. and solution is still exist. Even more empty spaces is possible.
But here is the problem - how to put various data(puzzle version) and call search predicate. Ive tried several solutions, but without succes unfortunately.
Next code is what I want(not working):
import planner.
main =>
print("Write amount of black balls:"),
N_black=read_int(),
print("Write amount of white balls:"),
N_white=read_int(),
Start_state=new_list(N_black, b)++[c]++new_list(N_white, w),
End_state=Start_state.reverse(),
solve_puzzle(Start_state).
solve_puzzle(Start_state) =>
best_plan_unbounded(Start_state,Plan), writeln(Plan), writef("%i+%i\n",Plan.len(),1).
final(End_state)=>true.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[Y,c|Z],LastS),
NextS=X++[c,Y]++Z,
Action=move_R,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[c,Y|Z],LastS),
NextS=X++[Y,c]++Z,
Action=move_L,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[Y,Z,c|D],LastS),
NextS=X++[c,Z,Y]++D,
Action=jump_R,
ActionCost=1.
action(LastS,NextS,Action,ActionCost)?=>
append(X,[c,Z,Y|D],LastS),
NextS=X++[Y,Z,c]++D,
Action=jump_L,
ActionCost=1.
Emm, "Note that the existing final/1 clause must be commented. " but, Why? cl_facts() predicate rewrites the code itself(program change program), and it needs a "place" to rewrite?
I even dont know about cl_facts() predicate... it seems, it provides a way to add clauses to predicates and possibly functions ...at a runtime, like a kind of exec() function in python, yes? Sound logical.
Hmm, my best ideas were confirmed, it should have entered data as a pattern, but an instantiated variable can't be used as a pattern, it should enter the data inline, using direct literals.
But this more like rewriting the code every time data changes. And I see, probably, exactly this solution.
bonus
If we have same amount of white & black balls, number of all puzzle states (with start state) equal to perfect square. N_black=N_white=N , then Plan.len()+1=(N+1)² .
Even more - the plan(+start state) seems like some recursion, so here is exact algorythm to solve it, and it must be recursive. I put visual representation if anyone want.