python – 优化算法寻找渐进学习曲线

让我们说一门课程由5个教程组成,从A到E命名,在课程中学生学习7个独特的信息,我从1到7编号.在给定的教程中学到的信息是一成不变的,但是我我可以任意顺序教授教程.例如,如果我愿意,我可以先教Tut C.

Tut A: (1,2,3,4)
Tut B: (5,6,7)
Tut C: (2,3)
Tut D: (5,6)
Tut E: (1,2,3)

所以我想说我按照这个顺序教授教程:

Ordering 1:

Tut A: (1,2,3,4)
Tut B: (5,6,7)
Tut C: (2,3)
Tut D: (5,6)
Tut E: (1,2,3)
Tut F: (1,3)

然后,学生将在第一个教程中学习4条信息,在第二个教程中学习3条新信息.在后续教程中不遗余力.这不是订购教程的好方法,因为学生应该在课程开始时学习太多新信息(陡峭的学习曲线).以下排序更好:

Ordering 2:

Tut C: (2,3)
Tut F: (1,3)
Tut E: (1,2,3) 
Tut A: (1,2,3,4)
Tut D: (5,6)
Tut B: (5,6,7)

在这里,学生在第一个教程中学到两件事,在第二个教程中学习一件新东西,在第三部分学习1件新东西,在第三部分学习新东西,在第四部分学习新东西,在第五部分学习两件新东西,在上一个教程中学习一件新东西.

这种排序给出了几乎相同的结果:

Ordering 3:

Tut C: (2,3)
Tut E: (1,2,3)
Tut F: (1,3) 
Tut A: (1,2,3,4)
Tut D: (5,6)
Tut B: (5,6,7)

我写了以下函数:

def curve(tutorials):
    covered = set()
    for t in tutorials:
        new = set(t).difference(covered)
        covered.update(new)
        yield len(new)


# Ordering 1:
print(tuple(curve(((1,2,3,4), (5,6,7), (2,3), (5,6), (1,2,3), (1,3)))))

# Ordering 2:
print(tuple(curve(((2,3), (1,3), (1,2,3,4), (1,2,3), (5,6), (5,6,7)))))

# Ordering 3:
print(tuple(curve(((2,3), (1,2,3), (1,3), (1,2,3,4), (5,6), (5,6,7)))))

我用上述三个排序的数据调用.这导致以下输出:

(4, 3, 0, 0, 0, 0)
(2, 1, 1, 0, 2, 1)
(2, 1, 0, 1, 2, 1)

然后,我使用以下函数测量这些学习曲线的陡度:

def steepness(r):
    return sum((r[i]*(len(r)-i) for i in range(len(r))))

对于那里的订单,它给出了以下结果:

39
26
25

最好的解决方案是为函数陡度返回最低值的排序.

所以这是解决这个问题的完整解决方案:

import itertools

def curve(tutorials):
    covered = set()
    for t in tutorials:
        new = set(t).difference(covered)
        covered.update(new)
        yield len(new)

def steepness(r):
    r = tuple(r)
    return sum((r[i]*(len(r)-i) for i in range(len(r))))

tutorials = ((1,2,3,4), (5,6,7), (2,3), (5,6), (1,2,3), (1,3))
print(min(itertools.permutations(tutorials), key=lambda x: steepness(curve(x))))

哪个输出:

((2, 3), (1, 2, 3), (1, 3), (1, 2, 3, 4), (5, 6), (5, 6, 7))

现在这一切都很好,但我实际上有30个教程,我需要订购与上面给出的5个相关,我有大约20个独特的信息.我如何优化我的解决方案,以便不需要永远找到解决方案?

最佳答案 这是一个动态程序,它在主题数量(~20)中是指数(基数2),而在教程数量(30)中只有多项式.

由于目标函数的属性,一旦教程不涉及新主题,就应该教它.准备一个节点是主题子集的图形.如果存在教程T使得S2 = S1联合T,则存在从集合S1到设置S2的弧.该弧的权重是| S2-S1 |. (新主题的数量)乘以不是S1子集的教程数量.

#!/usr/bin/env python3
import itertools


def optimize(tutorials):
    tutorials = [frozenset(tutorial) for tutorial in tutorials]
    topics = frozenset(topic for tutorial in tutorials for topic in tutorial)
    cost = {frozenset(): 0}
    predecessor = {}
    for r in range(len(topics)):
        for s1_tuple in itertools.combinations(topics, r):
            s1 = frozenset(s1_tuple)
            if s1 not in cost:
                continue
            cost1 = cost[s1]
            marginal_cost = sum(not tutorial.issubset(s1) for tutorial in tutorials)
            for tutorial in tutorials:
                s2 = s1 | tutorial
                cost2 = cost1 + len(s2 - s1) * marginal_cost
                if s2 not in cost or cost2 < cost[s2]:
                    cost[s2] = cost2
                    predecessor[s2] = s1
    order = []
    s2 = topics
    while s2 in predecessor:
        s1 = predecessor[s2]
        order.extend(tutorial for tutorial in tutorials if tutorial.issubset(s2) and not tutorial.issubset(s1))
        s2 = s1
    order.reverse()
    return order


print(optimize([{1, 2, 3, 4}, {5, 6, 7}, {2, 3}, {5, 6}, {1, 2, 3}, {1, 3}]))
点赞