def InitPop(data):
    import random
    from itertools import product
    from FeasibilityCheck import Feasibility_Check
    Placements = 0

    MinCriteria = min(data.rooms_max * data.total_timeslots,
                      sum([len(data.sol[i]) for i in data.sol]))
    Sorted_list_c = [(sum(data.F_ct[c]), c) for c in range(data.Courses_max)]
    Sorted_list_c.sort()
    while Placements < data.params['Initiate'] * MinCriteria:
        # Define the initial entry'
        try:
            c1 = Sorted_list_c[-1][1]
            # courses_index = random.randint(0, len(courses) - 1)
        except Exception as e:
            break

        # Assemble the list of indices corresponding to non-populated entries in the solution dictionary
        np_indexlist = []
        CountNP = 0
        for i, j in enumerate(data.sol[c1]):
            if j == (None, None):
                np_indexlist.append(i)
                CountNP = CountNP + 1

        if CountNP == 0:
            # If there are no unscheduled entries, we have no reason to revisit this course for now
            del Sorted_list_c[-1]

        else:
            np = random.randint(0, len(np_indexlist) - 1)
            c1_index = np_indexlist[np]
            del np_indexlist[np]

            Possibilities = list(
                product(list(range(data.total_timeslots)),
                        list(range(data.rooms_max))))

            Feasibility = False

            #Probe to see where this lecture may be placed
            while Feasibility == False:
                #If we've iterated through all possibilities and the course cannot be placed, move on
                if len(Possibilities) == 0:
                    del Sorted_list_c[-1]
                    break

                #Randomly generate the timeslot and room, then remove this from the Possibilities list
                p_index = random.randint(0, len(Possibilities) - 1)
                t1, r1 = Possibilities[p_index]
                del Possibilities[p_index]

                #Perform a feasibility check, if feasible, schedule the course
                Feasibility = Feasibility_Check(c1, t1, r1, data)

                if Feasibility == True:
                    data.sol[c1][c1_index] = (t1, r1)
                    data.timetable[c1, t1, r1] = 1
                    Placements = Placements + 1
def InitPop_roomsVsStudents(data):
    import random
    from FeasibilityCheck import Feasibility_Check
    Placements = 0
    times = [i for i in range(data.total_timeslots)]
    Sorted_list_room = [(data.C_r[r], r) for r in range(data.rooms_max)]
    Sorted_list_c = [(data.S_c[c], c) for c in range(data.Courses_max)]
    Sorted_list_room.sort()
    Sorted_list_c.sort()

    start = time.time()
    while time.time() - start < data.params['sek']:

        try:
            c1 = Sorted_list_c[-1][1]
        except Exception as e:
            break
        np_indexlist = []
        CountNP = 0
        for i, j in enumerate(data.sol[c1]):
            if j == (None, None):
                np_indexlist.append(i)
                CountNP = CountNP + 1

        if CountNP == 0:
            # If there are no unscheduled entries, we have no reason to revisit this course for now
            del Sorted_list_c[-1]

        else:
            random.shuffle(times)
            np = random.randint(0, len(np_indexlist) - 1)
            c1_index = np_indexlist[np]
            del np_indexlist[np]
            #Possibilities = list(product(list(range(data.total_timeslots)), list(range(data.rooms_max))))
            Feasibility = False
            # Probe to see where this lecture may be placed
            for i, r1 in reversed(Sorted_list_room):

                for t1 in times:
                    # Perform a feasibility check, if feasible, schedule the course
                    Feasibility = Feasibility_Check(c1, t1, r1, data)

                    if Feasibility == True:
                        data.sol[c1][c1_index] = (t1, r1)
                        data.timetable[c1, t1, r1] = 1
                        Placements = Placements + 1
                        break
                    if time.time() - start > data.params['sek']:
                        break
                    i += 1
                if Feasibility == True or time.time(
                ) - start > data.params['sek']:
                    break
Example #3
0
def RoomSwapSelection(Data, rPriorityList, rlist, tlist, c1, c1_index, c1Null, CurrentObj, t_old, r_old):
    FeasCount = 0
    FeasMax = Data.params['Beta']
    ObjectiveList = []
    

    
    #Cycle through all required instances of t1 such that we acquire the desired number of feasible solutions
    while FeasCount != FeasMax:
        #If none of the timeslots are beneficial, exit to the upper program
        if len(tlist) == 0:
            break
        
        #Select the timeslot to be fixed (t1) and delete it from the candidate list
        t1_index = random.randint(0, len(tlist)-1)
        t1 = tlist[t1_index]
        del tlist[t1_index]
        
        rCount = 0

        #Feasibility has already been handled in creating the tlist and rlist candidate lists, so we need not test it
        #For a given t1, we cycle through all possibilities of r in the rPriorityList
        while rCount != (len(rPriorityList) + len(rlist)) and FeasCount != FeasMax:
            
            #Select the room list we will operate upon
            if rCount == 0 and len(rPriorityList) != 0:
                UsedList = rPriorityList
            else:
                UsedList = rlist
                
            for r in UsedList:           
                rCount = rCount + 1
                
                #Check if a course already exists in the target room durng timeslot t1
                c2Null = True
                c2 = None
                for c in range(Data.Courses_max):
                    if Data.timetable[(c, t1, r)] != 0:
                        c2Null = False
                        c2 = c
                
                if c2 not in Data.qua.QL:                        
                    if c2Null == False and c1Null == False:
                        Data.timetable[(c1, t_old, r_old)] = 0
                        Data.timetable[(c2, t1, r)] = 0
                        
                        #Check the feasibility of the moves
                        if Feasibility_Check(c2, t_old, r_old, Data) and Feasibility_Check(c1, t1, r, Data):
                            FeasCount = FeasCount + 1
                            
                            Data.timetable[(c1, t_old, r_old)] = 1
                            Data.timetable[(c2, t1, r)] = 1
                            
                            #Calculate the provisional objective
                            Obj = Set_obj_delta(Data, ((c2, t1, r),(c2, t_old, r_old)), ((c1, t_old, r_old),(c1, t1, r)))  
                            ObjectiveList.append((Obj, t1, r, c2Null, c2))
                            
                            if FeasCount == FeasMax:
                                break     
                        else:
                            Data.timetable[(c1, t_old, r_old)] = 1
                            Data.timetable[(c2, t1, r)] = 1
                            
                    elif c2Null == False and c1Null == True:
                        Data.timetable[(c2, t1, r)] = 0
                        
                        if Feasibility_Check(c1, t1, r, Data):
                            #Feasibility is always possible for drops (c2), feasibility is passively ensured already for c1
                            FeasCount = FeasCount + 1
                            
                            Data.timetable[(c2, t1, r)] = 1
                            
                            #Calculate the provisional objective
                            Obj = Set_obj_delta(Data, ((c2, t1, r),(c2, t_old, r_old)), ((c1, t_old, r_old),(c1, t1, r)))  
                            ObjectiveList.append((Obj, t1, r, c2Null, c2))
                            
                            if FeasCount == FeasMax:
                                break     
                        else:
                            Data.timetable[(c2, t1, r)] = 1
                            
                    elif c2Null == True and c1Null == False:
                        Data.timetable[(c1, t_old, r_old)] = 0
                        
                        if Feasibility_Check(c1, t1, r, Data):
                            FeasCount = FeasCount + 1
                            
                            Data.timetable[(c1, t_old, r_old)] = 1
                            
                            #Calculate the provisional objective
                            Obj = Set_obj_delta(Data, ((c1, t_old, r_old),(c1, None, None)), ((c1, None, None),(c1, t1, r)))
                            ObjectiveList.append((Obj, t1, r, c2Null, None))
                            
                            if FeasCount == FeasMax:
                                break 
                        else:
                            Data.timetable[(c1, t_old, r_old)] = 1
    
    Accepted = False
    if len(ObjectiveList) == 0:
        pass
    else:
        #Perform the best swap discovered
        ObjectiveList.sort(key=lambda elem: elem[0])
        Obj_index = 0
        
        #Iterate until we can perform a move
        while not Accepted:
            
            if Obj_index == len(ObjectiveList):
                break
            
            CurrentObj, t1, r1, c2Null, c2 = ObjectiveList[Obj_index]
            
            if CurrentObj < Data.BestObj:

                Accepted = True
                
                if not c2Null and not c1Null:
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.timetable[(c2, t_old, r_old)] = 1
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)            

                    
                elif not c2Null and c1Null:
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)

                    
                elif c2Null and not c1Null:
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c1, t1, r1)] = 1     
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)

                Data.BestObj = CurrentObj
                Data.BestSol = copy.deepcopy(Data.sol)
                
            else:
                
                if not c2Null and not c1Null and not Data.tab.CheckTab((c2, t_old, r_old)) and not Data.tab.CheckTab((c1, t1, r1)):
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.timetable[(c2, t_old, r_old)] = 1
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)  

                    
                    Accepted = True
                    
                elif not c2Null and c1Null and not Data.tab.CheckTab((c2, t_old, r_old)) and not Data.tab.CheckTab((c1, t1, r1)):
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)

                    
                    Accepted = True
                    
                elif c2Null and not c1Null and not Data.tab.CheckTab((c1, t1, r1)):
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c1, t1, r1)] = 1     
                    
                    Data.sol[c1][c1_index] = (t1, r1)
                    
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    

                    
                    Accepted = True
                    

            Obj_index = Obj_index + 1

    return CurrentObj, Accepted
def BasicSwap(Data, CurrentObj, Iteration):
    #Initiate the c1 candidate list
    c1list = list(range(0, Data.Courses_max))
    ObjectiveList = []
    FeasMax = 30
    FeasCount = 0
    #Choose the c1 to be worked with
    c1 = c1list[random.randint(0, len(c1list) - 1)]
    c1_lectures = list(enumerate(Data.sol[c1]))
    local_index = random.randint(0, len(c1_lectures) - 1)
    c1_index, (t_old, r_old) = c1_lectures[local_index]

    if (t_old, r_old) == (None, None):
        c1Null = True
    else:
        c1Null = False
    Possibilities = list(
        product(list(range(Data.total_timeslots)),
                list(range(Data.rooms_max))))

    for t, r in Possibilities:
        if Data.tab.CheckTab((c1, t, r)):
            del Possibilities[Possibilities.index((t, r))]

    while len(Possibilities) != 0 and FeasCount != FeasMax:
        #Choose the entries in the Possibilities list
        poss_index = random.randint(0, len(Possibilities) - 1)
        t1, r1 = Possibilities[poss_index]
        del Possibilities[poss_index]
        #Check if a course already exists in the target room durng timeslot t1
        c2Null = True
        for c in range(Data.Courses_max):
            if Data.timetable[(c, t1, r1)] != 0:
                c2Null = False
                c2 = c

        if c2Null == False and c1Null == False:
            Data.timetable[(c1, t_old, r_old)] = 0
            Data.timetable[(c2, t1, r1)] = 0
            #Check the feasibility of the moves
            if Feasibility_Check(c1, t1, r1, Data) and Feasibility_Check(
                    c2, t_old, r_old, Data):
                FeasCount = FeasCount + 1

                Data.timetable[(c1, t_old, r_old)] = 1
                Data.timetable[(c2, t1, r1)] = 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data, ((c2, t1, r1), (c2, t_old, r_old)),
                                    ((c1, t_old, r_old), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, c2))

            else:
                Data.timetable[(c1, t_old, r_old)] = 1
                Data.timetable[(c2, t1, r1)] = 1

        elif c2Null == False and c1Null == True:
            Data.timetable[(c2, t1, r1)] = 0

            #Check the feasibility of the moves
            if Feasibility_Check(c1, t1, r1, Data):
                FeasCount = FeasCount + 1

                Data.timetable[(c2, t1, r1)] = 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data, ((c2, t1, r1), (c2, t_old, r_old)),
                                    ((c1, t_old, r_old), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, c2))

            else:
                Data.timetable[(c2, t1, r1)] = 1

        else:
            if Feasibility_Check(c1, t1, r1, Data):
                FeasCount = FeasCount + 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data,
                                    ((c1, t_old, r_old), (c1, None, None)),
                                    ((c1, None, None), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, None))

    if len(ObjectiveList) == 0: pass
    else:
        #Perform the best swap discovered
        ObjectiveList.sort(key=lambda elem: elem[0])
        CurrentObj, t1, r1, c2Null, c2 = ObjectiveList[0]

        if not c2Null and not c1Null:
            Data.timetable[(c1, t_old, r_old)] = 0
            Data.timetable[(c2, t1, r1)] = 0
            Data.timetable[(c1, t1, r1)] = 1
            Data.timetable[(c2, t_old, r_old)] = 1
            Data.sol[c1][c1_index] = (t1, r1)
            Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
            Data.tab.AddTab((c1, t_old, r_old), Data)
            Data.tab.AddTab((c2, t1, r1), Data)

        elif not c2Null and c1Null:
            Data.timetable[(c2, t1, r1)] = 0
            Data.timetable[(c1, t1, r1)] = 1
            Data.sol[c1][c1_index] = (t1, r1)
            Data.sol[c2][(Data.sol[c2]).index((t1, r1))] = (t_old, r_old)
            Data.tab.AddTab((c1, t_old, r_old), Data)
            Data.tab.AddTab((c2, t1, r1), Data)

        else:
            Data.timetable[(c1, t_old, r_old)] = 0
            Data.timetable[(c1, t1, r1)] = 1
            Data.sol[c1][c1_index] = (t1, r1)
            Data.tab.AddTab((c1, t_old, r_old), Data)

        Data.BestObj = CurrentObj
        Data.BestSol = copy.deepcopy(Data.sol)

    return CurrentObj, Iteration
def BasicSwapAsp(Data, CurrentObj, Iteration):
    #Initiate the c1 candidate list
    c1list = list(range(0, Data.Courses_max))
    ObjectiveList = []
    FeasMax = 30
    FeasCount = 0

    #Choose the c1 to be worked with
    c1 = c1list[random.randint(0, len(c1list) - 1)]

    #Choose the lecture in the sol dictionary that will be assessed
    c1_lectures = list(enumerate(Data.sol[c1]))
    local_index = random.randint(0, len(c1_lectures) - 1)
    c1_index, (t_old, r_old) = c1_lectures[local_index]

    if (t_old, r_old) == (None, None):
        c1Null = True
    else:
        c1Null = False

    Possibilities = list(
        product(list(range(Data.total_timeslots)),
                list(range(Data.rooms_max))))

    while len(Possibilities) != 0 and FeasCount != FeasMax:
        #Choose the entries in the Possibilities list
        poss_index = random.randint(0, len(Possibilities) - 1)
        t1, r1 = Possibilities[poss_index]
        del Possibilities[poss_index]

        #Check if a course already exists in the target room durng timeslot t1
        c2Null = True
        for c in range(Data.Courses_max):
            if Data.timetable[(c, t1, r1)] != 0:
                c2Null = False
                c2 = c

        if c2Null == False and c1Null == False:
            Data.timetable[(c1, t_old, r_old)] = 0
            Data.timetable[(c2, t1, r1)] = 0

            #Check the feasibility of the moves
            if Feasibility_Check(c2, t_old, r_old, Data) and Feasibility_Check(
                    c1, t1, r1, Data):
                FeasCount = FeasCount + 1
                Data.timetable[(c1, t_old, r_old)] = 1
                Data.timetable[(c2, t1, r1)] = 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data, ((c2, t1, r1), (c2, t_old, r_old)),
                                    ((c1, t_old, r_old), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, c2))

                if FeasCount == FeasMax:
                    break
            else:
                Data.timetable[(c1, t_old, r_old)] = 1
                Data.timetable[(c2, t1, r1)] = 1

        elif c2Null == False and c1Null == True:
            Data.timetable[(c2, t1, r1)] = 0

            if Feasibility_Check(c1, t1, r1, Data):
                #Feasibility is always possible for drops (c2), feasibility is passively ensured already for c1
                FeasCount = FeasCount + 1
                Data.timetable[(c2, t1, r1)] = 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data, ((c2, t1, r1), (c2, t_old, r_old)),
                                    ((c1, t_old, r_old), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, c2))

                if FeasCount == FeasMax:
                    break
            else:
                Data.timetable[(c2, t1, r1)] = 1
        else:
            Data.timetable[(c1, t_old, r_old)] = 0

            if Feasibility_Check(c1, t1, r1, Data):
                FeasCount = FeasCount + 1

                Data.timetable[(c1, t_old, r_old)] = 1

                #Calculate the provisional objective
                Obj = Set_obj_delta(Data,
                                    ((c1, t_old, r_old), (c1, None, None)),
                                    ((c1, None, None), (c1, t1, r1)))
                ObjectiveList.append((Obj, t1, r1, c2Null, None))

                if FeasCount == FeasMax:
                    break
            else:
                Data.timetable[(c1, t_old, r_old)] = 1

    Accepted = False

    if len(ObjectiveList) == 0: pass
    else:
        #Perform the best swap discovered
        ObjectiveList.sort(key=lambda elem: elem[0])
        Obj_index = 0

        #Iterate until we can perform a move
        while not Accepted:
            if Obj_index == len(ObjectiveList):
                break

            CurrentObj, t1, r1, c2Null, c2 = ObjectiveList[Obj_index]

            if CurrentObj < Data.BestObj:
                Accepted = True

                if not c2Null and not c1Null:
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.timetable[(c2, t_old, r_old)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index(
                        (t1, r1))] = (t_old, r_old)
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)

                elif not c2Null and c1Null:
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index(
                        (t1, r1))] = (t_old, r_old)
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)

                else:
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.tab.AddTab((c1, t_old, r_old), Data)

                Data.BestObj = CurrentObj
                Data.BestSol = copy.deepcopy(Data.sol)

            else:
                if not c2Null and not c1Null and not Data.tab.CheckTab(
                    (c2, t_old, r_old)) and not Data.tab.CheckTab(
                        (c1, t1, r1)):
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.timetable[(c2, t_old, r_old)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index(
                        (t1, r1))] = (t_old, r_old)
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)
                    Accepted = True

                elif not c2Null and c1Null and not Data.tab.CheckTab(
                    (c2, t_old, r_old)) and not Data.tab.CheckTab(
                        (c1, t1, r1)):
                    Data.timetable[(c2, t1, r1)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.sol[c2][(Data.sol[c2]).index(
                        (t1, r1))] = (t_old, r_old)
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Data.tab.AddTab((c2, t1, r1), Data)
                    Accepted = True

                elif not Data.tab.CheckTab((c1, t1, r1)):
                    Data.timetable[(c1, t_old, r_old)] = 0
                    Data.timetable[(c1, t1, r1)] = 1
                    Data.sol[c1][c1_index] = (t1, r1)
                    Data.tab.AddTab((c1, t_old, r_old), Data)
                    Accepted = True

            Obj_index = Obj_index + 1
    return CurrentObj, Iteration
def Pert(datau):
    #create a list of 30 promising courses
    CourseOld = SortBoth(datau)
    Courses = CourseOld[0:30]

    count = 0
    #while len(Courses)!=0 and count<=params['K']:
    while len(Courses) != 0 and count < 30:
        Swap = False
        #choose a c1 from the 30 courses with the highest penalty
        c1_index = random.randint(0, len(Courses))
        c1 = Courses[c1_index - 1]

        c1_lectures = list(enumerate(datau.sol[c1]))

        #if the course has lecture take one and delete it from the list, otherwise pick another course and delete the current one from the candidate course list
        if len(c1_lectures) != 0:
            c1lec_index = random.randint(0, len(c1_lectures))
            c1lec = c1_lectures[c1lec_index - 1]
            del c1_lectures[c1lec_index - 1]
        else:
            del Courses[c1_index - 1]
            break

        #create list of feasible t given c1, the two list should be complementary
        tbrutta = []
        tlist = []
        for t in range(datau.total_timeslots):
            Addition = False

            if datau.F_ct[c1][t] != 1:  #the fist constraint is ensured here
                Addition = True
                for c2 in range(datau.Courses_max
                                ):  #the fourth constraint is ensured here
                    if datau.Chi_cc[c1][c2] == 1 and sum([
                            datau.timetable[(c2, t, r)]
                            for r in range(datau.rooms_max)
                    ]) >= 1:
                        Addition = False
                if sum(datau.timetable[(c1, t, r)] for r in range(
                        datau.rooms_max)) >= 1:  #fifth constraint
                    Addition = False

            if Addition == True:
                tlist.append(t)
            else:
                tbrutta.append(t)
        while len(tlist) != 0 and Swap == False:
            t1_index = random.randint(0, len(tlist))
            t1 = tlist[t1_index - 1]
            del tlist[t1_index - 1]
            rlist = []
            rBrutta = []
            for r in range(datau.rooms_max):
                if datau.S_c[c1] <= datau.C_r[r]:
                    rlist.append(r)
                else:
                    rBrutta.append(r)
            while len(rlist) != 0 and Swap == False:
                r1_index = random.randint(0, len(rlist))
                r1 = rlist[r1_index - 1]
                del rlist[r1_index - 1]
                CEC2 = False
                if c1lec[1] != (None, None):
                    a, b = c1lec[1]
                    for c2 in range(datau.Courses_max):
                        if datau.timetable[(c2, t1, r1)] == 1:
                            CEC2 = True
                            datau.timetable[(c1, a, b)] = 0
                            datau.timetable[(c2, t1, r1)] = 0
                            Fe = Feasibility_Check(c1, t1, r1, datau)
                            Fe2 = Feasibility_Check(c2, a, b, datau)
                            if Fe2 == False or Fe == False:
                                datau.timetable[(c1, a, b)] = 1
                                datau.timetable[(c2, t1, r1)] = 1
                            else:
                                datau.timetable[(c2, a, b)] = 1
                                datau.timetable[(c1, t1, r1)] = 1
                                datau.sol[c2][(datau.sol[c2]).index(
                                    (t1, r1))] = (a, b)

                    if CEC2 == False:
                        Fe2 = True
                        datau.timetable[(c1, a, b)] = 0
                        Fe = Feasibility_Check(c1, t1, r1, datau)

                else:
                    Fe2 = True
                    Fe = Feasibility_Check(c1, t1, r1, datau)

                if Fe2 == True and Fe == True:
                    datau.timetable[(c1, t1, r1)] = 1
                    datau.timetable[(c1, a, b)] = 0
                    datau.sol[c1][c1lec[0]] = (t1, r1)
                    Swap = True
                    count = count + 1
                    break
                    break
                else:
                    datau.timetable[(c1, t1, r1)] = 0
                    datau.timetable[(c1, a, b)] = 1

    return Set_obj(datau)