【问题标题】:Prolog: Exam schedule generator - How to avoid permutations in solutionsProlog:考试时间表生成器 - 如何避免解决方案中的排列
【发布时间】:2016-03-30 07:35:25
【问题描述】:

我正在 Prolog 中构建考试调度程序。 调度程序基于此示例: https://metacpan.org/source/DOUGW/AI-Prolog-0.741/examples/schedule.pl

如何确保我的解决方案中没有排列?

例如解决方案

-> ((exam1, teacher1, time1, room1), (exam2, teacher2, time2, room2))

后期解决方案:

-> ((exam2, teacher2, time2, room2),(exam1, teacher1, time1, room1))

我怎样才能避免这种情况? 谢谢!

【问题讨论】:

  • 持续时间呢?所有课程都需要相同的时间吗?

标签: prolog permutation schedule


【解决方案1】:

1) 最接近/最简单的方法是检查您选择的课程是否严格按照顺序比前一个课程大。

例如,通过添加一个额外的谓词,该谓词还包括组合中的上一门课程。

%%makeListPrev(PreviousTakenCourse, ResultCombinationOfCourses, NrOfCoursesToAdd)
makeListPrev(_,[], 0). 
makeListPrev(course(Tprev,Ttime,Troom),[course(Teacher,Time,Room)|Rest], N) :- 
    N > 0,
    teacher(Teacher), 
    classtime(Time),
    classroom(Room), 
    course(Tprev,Ttime,Troom) @< course(Teacher,Time,Room), %% enforce unique combinations
    is(M,minus(N,1)), 
    makeListPrev(course(Teacher,Time,Room),Rest,M).

通过这种方式,您可以通过始终采用字典顺序最小的方式来消除同一组合的所有重复排列。

例如,如果您有 4 门课程:

(a,b,c,d)
(a,b,d,c) % d can't be before c
(a,c,b,d) % c can't be before b
...

2) 另一种很容易解决此问题的方法是首先创建所有可能课程的列表。然后依次取出N的所有可能组合。

scheduler(L) :- 
    %% Find all possible courses
    findall(course(Teacher,Time,Room),(teacher(Teacher),classtime(Time),classroom(Room)),Courses), 
    makeList(Courses,4,L),
    different(L).
makeList([],0,[]) :- !. %% list completed
makeList([H|T],N,[H|Res]) :- %% list including H
    M is N-1, 
    makeList(T,M,Res).
makeList([_|T], N, Res) :- makeList(T, N, Res). %% list without H

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多