我正在尝试为prolog中的总线驱动程序创建计划。我希望找到有限数量的解决方案。但我得到了"Out of local stack"
错误,我想这是因为我得到了太多解决方案。
如果给出以下代码,我该如何防止该错误?无论我做得不好的任何提示都会有很大的帮助。
count_drivers: counts the number of drivers with D_id as driver_id
( I need them to work less than "max_hours").
vehicle: represents the bus and respective routes.
connected: represents the connection between the relief opportunities
( a route consists of a group of relief points and the respective "connection"
between them)
workpiece: is a segment of work in the same vehicle between two relief points
spell: is a group of workpieces done by the same driver
spreadover: is the whole shift one driver has to do.
以下是代码:
?- use_module(library(clpfd)).
?- use_module(library(lists)).
?- use_module(library(aggregate)).
%workpiece(Bus,[Ro1,Ro2],Weight).
workpiece(1,[1,2],1).
workpiece(1,[2,3],2).
workpiece(1,[3,4],1).
workpiece(1,[4,5],2).
workpiece(1,[5,6],1).
workpiece(2,[7,8],2).
workpiece(2,[8,9],2).
workpiece(2,[9,10],1).
workpiece(2,[10,11],2).
workpiece(2,[11,12],1).
workpiece(3,[13,14],2).
workpiece(3,[14,15],1).
workpiece(3,[15,16],2).
workpiece(3,[16,17],1).
workpiece(3,[17,18],2).
%spell
spell(Vehicle,[[Ro1,Ro2]|Tail]):-Vars = [Ro1,Ro2], Vars in 1..18, workpiece(Vehicle,[Ro1,Ro2],_),spell(Vehicle,Tail,Ro2),labeling([],Vars).
spell(_,[],_).
spell(Vehicle,[[Ro1,Ro2]|Tail],Ro3):- Vars = [Ro3], Vars in 1..18, Ro3 #= Ro1, workpiece(Vehicle,[Ro1,Ro2],_),spell(Vehicle,Tail,Ro2), labeling([],Vars).
%spreadover de cada driver
spreadover(_,List):- Vars = I, Vars in 1..15, length(List,I), I #>= 1.
spreadover(Driver,[Head|Tail]):- Vars = [Vehicle,I], Vars in 1..9, Vehicle #>= 1, Vehicle #=< 3, spell(Vehicle,Head), length(Head,I), I #>= 1, spreadover(Driver,Tail), labeling([],Vars).
%ocupar as workpieces todas
%minimizando os shifts
%cobrir todas as routes
%length 15
%drivershifts
drivershifts(_,List):- Vars = I, Vars in 1..15, length(List,I), I #= 15.
drivershifts(NumDrivers,[[Driver|List]|Tail]):-Vars = Driver, Vars in 1..NumDrivers, Driver #>= 1, Driver #=< NumDrivers, spreadover(Driver,List), labeling([],Vars).
我提前感谢你们,无论何时你都可以帮助我。
编辑:我改变了一点代码,现在我从查询中获得了大量未分配的变量 FORALL(spreadover(1,列表),writeln(列表))。 或者一个未分配的变量 spreadover(1,列表)。 我尽可能限制域名,但不确定我是否正确地执行此操作。 从上面的查询中我应该为驱动程序1生成扩展(一组法术)。
我不确定是否应该发布新问题或重写此问题,因此决定重写此问题。
答案 0 :(得分:0)
你有许多来自Singleton变量的警告,并且是解决它们的好方法。 至少,您知道的前缀变量未使用下划线,以避免警告。
现在循环:你用自由变量调用图表,导致无限递归,“构造”一个部分实例化变量的无限列表。
我无法理解图表/ 1的预期含义。当然,你会错过基本情况:添加类似
的内容diagram([]).