% nomystery.pi (in Picat) % https://www.mat.unical.it/aspcomp2013/Nomystery % by Roman Bartak, Feb. 2013 updated July 2013 % translated to Picat by Neng-Fa Zhou % % use the following OS command to solve an instance file % % picat nomystery < instance_file % % or use the picat top-level command 'test' to solve an included instance % % picat> test % import planner. go => test1. main => % read facts from stdin read_file_terms().asp(). asp(Facts), cl_facts(Facts, $[fuelcost(-,+,-), at(+,-)]), fuel(T,Fuel), % exactly one truck is supposed in data! at(T,Loc), % initial location of the truck Cargoes = findall([Cargo,CargoLoc,CargoGoal], (goal(Cargo,CargoGoal), at(Cargo,CargoLoc))), Cargoes1 = [[_CargoLoc|_CargoGoal] : [_Cargo,_CargoLoc,_CargoGoal] in Cargoes], NSteps = findall(S,step(S)).max(), plan($s(Loc,Fuel,[],Cargoes1),NSteps,PlanX), do_plan(PlanX,1,Cargoes, Plan) => println('ANSWER'), foreach(Step in Plan) write(Step),print('. ') end. asp(_) => println('INCONSISTENT'). final(s(_Loc,_Fuel,[],[])) => true. action(s(Loc,Fuel,LCGs,WCGs),NextState,Action,ActionCost), select(Loc,LCGs,LCGs1) => Action = $unload(Loc,Loc), ActionCost = 1, NextState= $s(Loc,Fuel,LCGs1,WCGs). % unload cargo at its destination action(s(Loc,Fuel,LCGs,WCGs),NextState,Action,ActionCost), select([Loc|CargoGoal],WCGs,WCGs1) => Action = $load(Loc,CargoGoal), ActionCost = 1, NextState=$s(Loc,Fuel,LCGs1,WCGs1), % load cargo LCGs1 = insert_ordered(LCGs,CargoGoal). action(s(Loc,Fuel,LCGs,WCGs),NextState,Action,ActionCost) => % drive from Loc to Loc1 Action=$drive(Loc,Loc1), ActionCost = 1, NextState=$s(Loc1,Fuel1,LCGs,WCGs), fuelcost(Cost,Loc,Loc1), Fuel1 = Fuel-Cost, Fuel1 >= 0. % fill actions with additional parameters such as steps, cargo nad truck names do_plan([],_,[], Plan) => Plan=[]. do_plan([drive(Loc,Loc1)|RestPlan],Step,Cargoes, Plan) => Plan=[$drive(T,Loc,Loc1,Step)|Plan1], fuel(T,_), do_plan(RestPlan,Step+1,Cargoes,Plan1). do_plan([load(Loc,CargoGoal)|RestPlan],Step,Cargoes, Plan) => Plan=[$load(CargoName,T,Loc,Step)|Plan1], fuel(T,_), select([CargoName,Loc,CargoGoal],Cargoes,Cargoes1), do_plan(RestPlan,Step+1,[[CargoName,CargoGoal]|Cargoes1],Plan1). do_plan([unload(Loc,CargoGoal)|RestPlan],Step,Cargoes, Plan), Plan=[$unload(CargoName,T,Loc,Step)|Plan1], fuel(T,_), select([CargoName,CargoGoal],Cargoes,Cargoes2), (Loc=CargoGoal -> Cargoes1 = Cargoes2 ; Cargoes1 = [[CargoName,Loc,CargoGoal]|Cargoes2]) => do_plan(RestPlan,Step+1,Cargoes1,Plan1). test => asp( $[ fuelcost(8,l0,l2), fuelcost(25,l0,l3), fuelcost(12,l0,l5), fuelcost(21,l1,l5), fuelcost(8,l2,l0), fuelcost(9,l2,l3), fuelcost(5,l2,l4), fuelcost(24,l2,l5), fuelcost(25,l3,l0), fuelcost(9,l3,l2), fuelcost(5,l3,l4), fuelcost(5,l4,l2), fuelcost(5,l4,l3), fuelcost(6,l4,l5), fuelcost(12,l5,l0), fuelcost(21,l5,l1), fuelcost(24,l5,l2), fuelcost(6,l5,l4), at(t0,l3), fuel(t0,41), at(p0,l0), at(p1,l4), at(p2,l4), at(p3,l0), at(p4,l3), at(p5,l4), goal(p0,l4), goal(p1,l2), goal(p2,l3), goal(p3,l5), goal(p4,l0), goal(p5,l0), step(1), step(2), step(3), step(4), step(5), step(6), step(7), step(8), step(9), step(10), step(11), step(12), step(13), step(14), step(15), step(16), step(17), step(18), step(19), step(20), step(21), step(22), step(23), step(24), step(25)]). ipc20 => asp($[ fuelcost(1,l0,l5), fuelcost(21,l0,l12), fuelcost(20,l0,l14), fuelcost(20,l1,l6), fuelcost(23,l1,l12), fuelcost(12,l1,l13), fuelcost(25,l2,l5), fuelcost(2,l2,l13), fuelcost(2,l3,l5), fuelcost(13,l4,l12), fuelcost(1,l5,l0), fuelcost(25,l5,l2), fuelcost(2,l5,l3), fuelcost(7,l5,l8), fuelcost(3,l5,l9), fuelcost(17,l5,l11), fuelcost(20,l6,l1), fuelcost(18,l6,l9), fuelcost(25,l7,l8), fuelcost(24,l7,l9), fuelcost(2,l7,l11), fuelcost(11,l7,l13), fuelcost(7,l8,l5), fuelcost(25,l8,l7), fuelcost(5,l8,l11), fuelcost(3,l9,l5), fuelcost(18,l9,l6), fuelcost(24,l9,l7), fuelcost(10,l10,l14), fuelcost(17,l11,l5), fuelcost(2,l11,l7), fuelcost(5,l11,l8), fuelcost(7,l11,l12), fuelcost(21,l12,l0), fuelcost(23,l12,l1), fuelcost(13,l12,l4), fuelcost(7,l12,l11), fuelcost(15,l12,l14), fuelcost(12,l13,l1), fuelcost(2,l13,l2), fuelcost(11,l13,l7), fuelcost(20,l14,l0), fuelcost(10,l14,l10), fuelcost(15,l14,l12), at(t0,l3), fuel(t0,177), at(p0,l6), at(p1,l1), at(p2,l14), at(p3,l6), at(p4,l14), at(p5,l14), at(p6,l3), at(p7,l5), at(p8,l9), at(p9,l8), at(p10,l11), at(p11,l1), at(p12,l14), at(p13,l14), at(p14,l1), goal(p0,l5), goal(p1,l8), goal(p2,l13), goal(p3,l2), goal(p4,l3), goal(p5,l0), goal(p6,l13), goal(p7,l10), goal(p8,l6), goal(p9,l10), goal(p10,l6), goal(p11,l5), goal(p12,l13), goal(p13,l8), goal(p14,l5), step(500)]).