-
-
Save zubinmehta/1807165 to your computer and use it in GitHub Desktop.
ga code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| COURSES=[] | |
| DAYS = 5 | |
| DAILY_SLOTS = 5 | |
| import random | |
| import numpy | |
| import itertools | |
| class Course(object): | |
| counter = 0 | |
| def __init__(self, prof, students=set([])): | |
| self.students = students | |
| # put students here as you can then intersect sets of students from 2 courses | |
| self.prof = prof | |
| self.idx = Course.counter # you dont need this index. remove it later. | |
| Course.counter += 1 | |
| def __repr__(self): | |
| return "c" + str(Course.counter) | |
| class Prof(object): | |
| counter = 0 | |
| def __init__(self, name): | |
| self.name = name | |
| self.idx = Prof.counter | |
| Prof.counter += 1 | |
| def __repr__(self): | |
| return "p" + str(Prof.counter) | |
| class Student(object): | |
| counter = 0 | |
| def __init__(self): | |
| self.courses = [] | |
| Student.counter += 1 | |
| def __repr__(self): | |
| return "s" + str(Student.counter) | |
| def create_course_choices(self): | |
| # create a choice of 5 courses out of all the available courses. How do you find all the courses? | |
| self.courses = random.sample(COURSES, 5) | |
| for course in self.course: | |
| course.students.add(self) | |
| # courses.append() | |
| class GA: | |
| def __init__(self): | |
| #self.P = {} # this is the current population variable. This has a fscore as the value of the dict. | |
| self.P = [] | |
| def initial_population(num_creatures = 1000): # make a global constant for this also. later on :) | |
| [self.P.append(TimeTable()) for i in range(1000)] | |
| return self.P | |
| def fitness_score(creature): | |
| # you calculate the score of each creature | |
| f = 0 | |
| for i in range(DAILY_SLOTS): | |
| for j in range(DAYS): | |
| if len(creature.matrix[i][j]) == 1: | |
| continue | |
| else: | |
| for c1,c2 in itertools.combinations(creature.matrix[i][j], 2): | |
| f = f - len(c1.students & c2.students) | |
| return f | |
| def selection(P): | |
| # select 2 parents based on the given population based on the fscores converted into probabilities | |
| return | |
| def crossover(creature1, creature2): | |
| L = COURSES*3 | |
| S = set([L]) | |
| return | |
| def mutation(): | |
| return | |
| class Crossover(object): | |
| def __init__(self): | |
| self.name = [] | |
| class TimeTable: | |
| def __init__(self, days = 5, daily_slots = 5): # remove this days and daily slots from here. They are not a parameter. its a constant for a given run. | |
| # here we want a ds for the matrix of the daywise course slots. every slot is filled with an int which is the id of the course. | |
| self.matrix = numpy.empty([daily_slots, days], dtype=numpy.ndarray) | |
| self.days = days | |
| self.daily_slots = daily_slots | |
| def create_random_timetable(self, COURSES, num_repeat_course = 3): # a course is repeated 3 times a week! | |
| # this is a dummy randomised making of the timetable which can be populated by existing integers of courses. | |
| for course in COURSES: | |
| which_days = random.sample(self.days, num_repeat_course) | |
| which_slots = random.sample(self.daily_slots, num_repeat_course) | |
| for x,y in zip(which_slots, which_days): | |
| self.matrix[x][y].append(course) | |
| def main(): | |
| s=[] | |
| [s.append(Student("s"+str(i))) for i in range(1,6)] | |
| p = Prof("babubhai") | |
| c = Course(p) | |
| for i in range(10): | |
| COURSES.append(Course(p)) | |
| print COURSES | |
| if __name__ == "__main__": | |
| main() | |
| # Apply constraints in the fitness score fucntion and dont worry about it in the definition or whatever for the time being. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment