artificial intelligence - How to go about creating a prolog program that can work backwards to determine steps needed to reach a goal -


i'm not sure i'm trying ask. want able make code can take initial , final state , rules, , determine paths/choices there.

so think, example, in game starcraft. build factory need have barracks , command center built. if have nothing , want factory might ->command center->barracks->factory. each thing takes time , resources, , should noted , considered in path. if want factory @ 5 minutes there less options if want @ 10.

also, engine should able calculate available resources , utilize them effectively. 3 buildings might cost 600 total minerals engine should plan command center when have 200 (or w/e costs).

this have requirements similar 10 marines @ 5 minutes, infantry weapons upgrade @ 6:30, 30 marines @ 10 minutes, factory @ 11, etc...

so, how go doing this? first thought use procedural language , make decisions ground up. simulate system , branching , making different choices. ultimately, choices going make impossible reach goals later (if build 20 supply depots i'm prob not going make factory on time.)

so thought weren't functional languages designed this? tried write prolog i've been having trouble stuff time , distance calculations. , i'm not sure best way return "plan".

i thinking write:

depends_on(factory, barracks) depends_on(barracks, command_center) builds_from(marine, barracks) build_time(command_center, 60) build_time(barracks, 45) build_time(factory, 30) minerals(command_center, 400) ... build(x) :-    depends_on(x, y),   build_time(x, t),   minerals(x, m), ... 

here's confused. i'm not sure how construct function , query close want. have somehow account rate @ minerals gathered during time spent building , other possible paths gold. if want 1 marine in 10 minutes want engine generate lots of plans because there lots of ways end 1 marine @ 10 minutes (maybe cut off after many, not sure how in prolog).

i'm looking advice on how continue down path or advice other options. haven't been able find more useful towers of hanoi , ancestry examples ai articles explaining how use prolog real things amazing. , if somehow can these rules set in useful way how "plans" prolog came (ways solve query) other writing stdout towers of hanoi examples do? or preferred way?

my other question is, main code in ruby (and potentially other languages) , options communicate prolog calling prolog program within ruby, accessing virtual file system within prolog, or kind of database structure (unlikely). i'm using swi-prolog atm, better off doing procedurally in ruby or constructing in functional language prolog or haskall worth effort integrating?

i'm sorry if unclear, appreciate attempt help, , i'll re-word things unclear.

your question typical , common users of procedural languages first try prolog. easy solve: need think in terms of relations between successive states of world. state of world consists example of time elapsed, minerals available, things built etc. such state can represented prolog term, , example time_minerals_buildings(10, 10000, [barracks,factory])). given such state, need describe state's possible successor states like. example:

state_successor(state0, state) :-      state0 = time_minerals_buildings(time0, minerals0, buildings0),      time time0 + 1,      can_build_new_building(buildings0, building),      building_minerals(building, mb),      minerals minerals0 - mb,      minerals >= 0,      state = time_minerals_buildings(time, minerals, building). 

i using explicit naming convention (state0 -> state) make clear talking successive states. can of course pull unifications clause head. example code purely hypothetical , rather different in final application. in case, describing new state's elapsed time old state's time + 1, new amount of minerals decreases amount required build building, , have predicate can_build_new_building(bs, b), true when new building b can built assuming buildings given in bs built. assume non-deterministic predicate in general, , yield possible answers (= new buildings can built) on backtracking, , leave exercise define such predicate.

given such predicate state_successor/2, relates state of world direct possible successors, can define path of states lead desired final state. in simplest form, similar following dcg describes list of successive states:

states(state0) -->      (   { final_state(state0) } -> []      ;   [state0],          { state_successor(state0, state1) },          states(state1)      ). 

you can use example iterative deepening search solutions:

?- initial_state(s0), length(path, _), phrase(states(s0), path). 

also, can keep track of states considered , avoid re-exploring them etc.

the reason confused example code posted build/1 not have enough arguments describe want. need @ least 2 arguments: 1 current state of world, , other possible successor given state. given such relation, else need can described easily. hope answers question.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -