-3
# Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
  File "/Users/maninder/git/TrialScheduler/TrialScheduler/main.py", line 27, in Loop
    Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
  File "/Users/maninder/git/TrialScheduler/TrialScheduler/main.py", line 27, in Loop
    Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
   File "/Users/maninder/git/TrialScheduler/TrialScheduler/main.py", line 27, in Loop #

编码:

from CProfessor import Professor 
    from CCourse import Course
    from CClassroom import Classroom
    import copy
def Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize):
    for i in range(0, loopsize):
        Mutated = Classroom()
        #Mutated = Classroom1.CopyOfClassroom(Mutated)
        Mutated = copy.deepcopy(Classroom1)#Modification
        Mutated.Mutate()
        HardConstraintClassroomMet = Classroom1.ComputeHardConstraint()
        HardConstraintMutatedMet = Mutated.ComputeHardConstraint()
        if HardConstraintClassroomMet == False and HardConstraintMutatedMet == False:
            h_count + 1
            t_count + 1
            Classroom1.Mutate()
            continue
        if Mutated.FitnessValue() > Classroom1.FitnessValue():
            m_count + 1
            t_count + 1
            Classroom1 = Mutated
        if Classroom1.ComputeHardConstraint() == False:
            l_count + 1
            temp = l_count*loopsize
            print "Unable to meet hard consraints in %d" % temp
        Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)

    Professor0 = Professor(ProfessorId = 0, PreferedStartTime = 8, PreferedEndTime = 11)
    Professor1 = Professor(ProfessorId = 1, PreferedStartTime  = 10, PreferedEndTime = 13.75)
    Professor2 = Professor(ProfessorId = 2, PreferedStartTime  = 4, PreferedEndTime = 7.75)

# intializing Courses
    Course0 = Course(CourseId = 0, ProfessorId = 0, CourseSlot = 0, CourseType ="UnderGraduate")
    Course1 = Course(CourseId = 1, ProfessorId = 4, CourseSlot = 1) # we decided to leave this 
coursetype to be emmpty and it is set none in the Course class
    Course2 = Course(CourseId = 2, ProfessorId = 1, CourseSlot = 2, CourseType ="UnderGraduate")
    Course3 = Course(CourseId = 3, ProfessorId = 4, CourseSlot = 3)
    Course4 = Course(CourseId = 4, ProfessorId = 1, CourseSlot = 4, CourseType ="UnderGraduate")
    Course5 = Course(CourseId = 5, ProfessorId = 4, CourseSlot = 5)
    Course6 = Course(CourseId = 6, ProfessorId = 2, CourseSlot = 6, CourseType ="Graduate")
    Course7 = Course(CourseId = 7, ProfessorId = 2, CourseSlot = 7, CourseType ="UnderGraduate")

# intializing 
    Classroom1 = Classroom(FirstCourseStartTime = 8, LastCourseEndTime = 7.75)
# Add all courses to Classroom1
    Classroom1.AddCourse(Course0)
    Classroom1.AddCourse(Course1)
    Classroom1.AddCourse(Course2)
    Classroom1.AddCourse(Course3)
    Classroom1.AddCourse(Course4)
    Classroom1.AddCourse(Course5)
    Classroom1.AddCourse(Course6)
    Classroom1.AddCourse(Course7)

# Add Professors to the classroom.

    Classroom1.AddProfessor(Professor0)
    Classroom1.AddProfessor(Professor1)
    Classroom1.AddProfessor(Professor2) 

    m_count = 0 # mutations caused by grea
    h_count = 0
    t_count = 0
    l_count = 0
    loopsize = 10000
    Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize)
    """
    Printing out the results:
    """
    print '\n'
    print "Mutation caused by greater fitness value %d" % m_count
    print "Mutation caused by Hard Constraint Violations %d" % h_count
    print "Number of Mutations before Converging" % t_count
    Classroom1.PrintClassroom()
4

1 回答 1

0

这是代码的相关部分:

def Loop(Classroom1, m_count, h_count, t_count, l_count, loopsize):
    for i in range(0, loopsize):
        Loop(Classroom1.Mutate(), m_count, h_count, t_count, l_count, loopsize)

换句话说,当你用 loopsize=10 调用 Loop 时,它会用 loopsize 10 再调用 Loop 10 次……这将再调用 Loop 100 次……这将再调用它 1000 次。所以最终,你会用完堆栈空间。

跳过递归情况的唯一方法是使用 Classroom1.ComputeHardConstraint(),在这种情况下,您跳过了一个对 Loop 的调用,但您仍然有 loopcount-1 其他调用。一个正确定义的算法可能会完成,但看起来你正在发散——你切断了递归树的成功分支,但其余的永远继续下去。即使它确实收敛了,递归也几乎肯定会变得非常深和宽,可能足以杀死堆栈。(如果您指望尾递归消除,Python 不会这样做,而且它无论如何也无济于事,除非可能从每个循环计数中敲出 1 个调用。)

作为旁注,您的大部分代码实际上并没有做任何事情。例如:

h_count + 1

这将评估表达式“h_count + 1”,然后丢弃结果。它不会将其存储在任何地方,将其用于任何事情,更改 h_count 的值或其他任何事情。

于 2012-06-28T04:27:53.783 回答