Search code examples
pythontimetable

Generate random blocks Timetable python


I need to create an automatic generator of Timetables.

My idea is to generate "n" timetables and then give it scores according to my conditions. The timetable with high score, wins.

I have these variables:

  • Disciplines
  • Professors
  • List item
  • Class room

Each discipline has a code and the number of hours it needs per week. Lets call the number of hours "block".

So I have an array of blocks.

array = [5,4,3,2,1];

This array means the number of disciplines we have (in this case 5) and the number of hours they need per week. The first discipline needs 5 hours, the second 4 hours, third 3 hours, fourth 2 hours and fifth 1 hour.

My timetable is a 5x5 bidimensional array (Monday to Friday) and I need to allocate the array of disciplines into the 5x5 array. So I need all the solutions it can generate.

Example:

array = [5,4,3,2,1];

I can put the first discipline (5 hours) in Monday, second (4 hours) in Tuesday, third and fourth in Wednesday (3 hours and 2 hours) and the last discipline (1 hour) in Tuesday. Something like this:

discipline = {A,B,C,D,E};
array = {5,4,3,2,1};

ABC
ABC
ABC
ABD
AED

This is just 1 possible solution but I need all the combinations and save that combinations inside an array.

I've made a function to translate the 5x5 timetable array into a code I understand and vice-versa. So each time a solution is generated, I save that code into an array.

Basically I want an array filled with 5x5 timetable array codes but I'm stuck in this part.

Sorry about my English.


Solution

  • I think maybe something like this

    def greedy_bag(items,limit):
        weighted_items = sorted(items,key=lambda x:x["cost"],reverse = True)
        items_in_bag =  []
        for item in weighted_items:
             if item["cost"] < limit:
                items_in_bag.append(item)
                limit -= item["cost"]
        return items_in_bag
    disiplines = "ABCDE"
    costs = [5,4,3,2,1]
    items = [{"item":d,"cost":c} for d,c in zip(disiplines,costs)]
    schedule = []
    while items:
        bagged_items = greedy_bag(items,5)
        if not bagged_items:
            break
        for item in bagged_items:
              items.remove(item)
        schedule.append(bagged_items)
    
    print schedule
    

    I think that would work ... (although if you have any items that dont fit at all it will break ...)