$\newcommand{\xv}{\mathbf{x}} \newcommand{\Xv}{\mathbf{X}} \newcommand{\yv}{\mathbf{y}} \newcommand{\zv}{\mathbf{z}} \newcommand{\av}{\mathbf{a}} \newcommand{\Wv}{\mathbf{W}} \newcommand{\wv}{\mathbf{w}} \newcommand{\tv}{\mathbf{t}} \newcommand{\Tv}{\mathbf{T}} \newcommand{\muv}{\boldsymbol{\mu}} \newcommand{\sigmav}{\boldsymbol{\sigma}} \newcommand{\phiv}{\boldsymbol{\phi}} \newcommand{\Phiv}{\boldsymbol{\Phi}} \newcommand{\Sigmav}{\boldsymbol{\Sigma}} \newcommand{\Lambdav}{\boldsymbol{\Lambda}} \newcommand{\half}{\frac{1}{2}} \newcommand{\argmax}[1]{\underset{#1}{\operatorname{argmax}}} \newcommand{\argmin}[1]{\underset{#1}{\operatorname{argmin}}}$

# A5: Min-Conflicts¶

• A5.1: New version of A5grader.py. Download and extract from A5grader.tar. This version includes 5pm as a class meeting time, uses a different random number seed and another few classes in the final test.

For this assignment, you will use the min-conflicts code from the lecture notes to solve the following scheduling problem. Do not change this code in completing this assignment.

You are in charge of assigning classes to classrooms and times for the department. The scheduling is simplified by the fact at this imaginary university each class meets every day.

You have been asked to schedule these four class rooms:

• CSB 130
• CSB 325
• CSB 425
• Clark 101

Classes start on the hour. You can only assign classes to the hours of

• 8 am
• 9 am
• 10 am
• 11 am
• 12 pm
• 1 pm
• 2 pm
• 3 pm
• 4 pm
• 5 pm

You must schedule these 37 classes:

• CS160, CS163, CS164, CS165, CS192, CS199,
• CS220, CS270, CS253, CS245, CS250, CS280,
• CS320, CS314, CS356, CS370, CS380, CS390,
• CS410, CS414, CS420, CS425, CS430, CS435, CS440, CS445, CS453,
• CS510, CS514, CS520, CS530, CS533, CS535, CS540, CS545, CS548, CS553

Your schedule must not violate any of the following constraints.

• Two classes cannot meet in the same room at the same time.
• Classes with the same first digit cannot meet at the same time, because students might take a subset of these in one semester. There is one exception to this rule. CS163 and CS164 can meet at the same time.

In addition to these constraints, let's add some preferences, which turns this CSP problem into a COP problem. Let's prefer schedules with the fewest number of classes scheduled at 8 am and 5 pm.

The variables for this COP problem are the classes. The values you assign to each class will be a tuple containing a room and a time.

## Required Functions¶

### assignments = schedule(classes, times, rooms, max_steps, n_solutions_to_test,)¶

Given:

• classes: list of all class names, like 'CS410'
• times: list of all start times, like '10 am' and ' 1 pm'
• rooms: list of all rooms, like 'CSB 325'
• max_steps: maximum number of assignments to try, passed to min_conflicts as the last argument
• n_solutions_to_test: call min_conflicts this many times. For each solution, count the number of classes scheduled at 8 am or 5 pm. Keep track of the lowest number of classes at 8 am and 5 pm and the corresponding assignments of values to variables.

Return:

• assignments: dictionary of best values assigned to variables (ones that have lowest number of classes scheduled at 8 am or 5pm), like {'CS410': ('CSB 425', '10 am'), ...}

assignments will each be None if a solution was not found.

### result = constraints_ok(class_name_1, value_1, class_name_2, value_2)¶

Given:

• class_name_1: as above, like 'CS410'
• value_1: tuple containing room and time
• class_name_2: a second class name
• value_2: another tuple containing a room and time

Returns:

• result: True if the assignment of value_1 to class_name 1 and value_2 to class_name 2 does not violate any constraints. False otherwise.

### dataframe = display(assignments, rooms, times)¶

Given

• assignments: returned from your schedule function
• rooms: list of all rooms as above
• times: list of all times as above Returns:
• dataframe: a pandas.DataFrame of the solution, as shown in the example below.

## Examples¶

In [1]:
classes = ['CS160', 'CS163', 'CS164', 'CS165', 'CS192', 'CS199',
'CS220', 'CS270', 'CS253', 'CS245', 'CS250', 'CS280',
'CS320', 'CS314', 'CS356', 'CS370', 'CS380', 'CS390',
'CS410', 'CS414', 'CS420', 'CS425', 'CS430', 'CS435', 'CS440', 'CS445', 'CS453',
'CS510', 'CS514', 'CS520', 'CS530', 'CS533', 'CS535', 'CS540', 'CS545', 'CS548', 'CS553']

times = [' 8 am',
' 9 am',
'10 am',
'11 am',
'12 pm',
' 1 pm',
' 2 pm',
' 3 pm',
' 4 pm',
' 5 pm']

rooms = ['CSB 130', 'CSB 325', 'CSB 425',
'Clark 101'] #, 'Clark 102']

In [2]:
len(classes), len(times) * len(rooms)

Out[2]:
(37, 40)
In [7]:
random.seed(555)

n_solutions_to_test = 1000
max_steps = 100
assignments = schedule(classes, times, rooms, max_steps, n_solutions_to_test)

Solution 1 is new best solution found in 2 steps, with 8 classes at 8 or 5.
Solution 3 is new best solution found in 0 steps, with 7 classes at 8 or 5.
Solution 6 is new best solution found in 8 steps, with 6 classes at 8 or 5.
Solution 287 is new best solution found in 20 steps, with 5 classes at 8 or 5.

In [8]:
display(assignments, rooms, times)

Out[8]:
CSB 130 CSB 325 CSB 425 Clark 101
8 am CS553 CS280 CS420
9 am CS535 CS356 CS250 CS410
10 am CS540 CS160 CS320 CS245
11 am CS390 CS199 CS435 CS530
12 pm CS533 CS370 CS425 CS253
1 pm CS165 CS314 CS453 CS520
2 pm CS430 CS220 CS163 CS510
3 pm CS164 CS545 CS270 CS440
4 pm CS548 CS192 CS445 CS380
5 pm CS514 CS414

## Check-in¶

Do not include this section in your notebook.

Name your notebook Lastname-A5.ipynb. So, for me it would be Anderson-A5.ipynb. Submit the file using the Assignment 5 link on Canvas.

Download A5grader.tar and extract A5grader.py from it.

In [2]:
%run -i A5grader.py

======================= Code Execution =======================

['Anderson-A5.ipynb']
Extracting python code from notebook named 'Anderson-A5.ipynb' and storing in notebookcode.py
Removing all statements that are not function or class defs or import statements.

Testing

constraints_ok('CS410', ('CSB 130', ' 9 am'), 'CS510', ('CSB 130', ' 9 am'))

--- 10/10 points. Your constraints_ok function correctly returned False

Testing

constraints_ok('CS410', ('CSB 130', ' 9 am'), 'CS510', ('CSB 130', '10 am'))

--- 10/10 points. Your constraints_ok function correctly returned True

Testing

constraints_ok('CS410', ('CSB 130', '10 am'), 'CS430', ('CSB 425', '10 am'))

--- 10/10 points. Your constraints_ok function correctly returned False

Testing

classes = ['CS160', 'CS163', 'CS164',
'CS220', 'CS270', 'CS253',
'CS320', 'CS314', 'CS356', 'CS370',
'CS410', 'CS414', 'CS420', 'CS430', 'CS440', 'CS445', 'CS453', 'CS464',
'CS510', 'CS514', 'CS535', 'CS540', 'CS545']

times = [' 8 am',
' 9 am',
'10 am',
'11 am',
'12 pm',
' 1 pm',
' 2 pm',
' 3 pm',
' 4 pm',
' 5 pm']

rooms = ['CSB 130', 'CSB 325', 'CSB 425']

random.seed(111)
result = schedule(classes, times, rooms, 100, 10)

Solution 1 is new best solution found in 0 steps, with 5 classes at 8 or 5.
Solution 3 is new best solution found in 0 steps, with 3 classes at 8 or 5.

--- 30/30 points. Your schedule function returned a valid schedule.

Testing a call to schedule again with two more classes

classes += ['CS554', 'CS551', 'CS552', 'CS555', 'CS898', 'CS899']
random.seed(333)
result = schedule(classes, times, rooms, 1000, 10)
n_classes_at_8 = len([(room, time) for
(room, time) in assignment.values()
if time.endswith('8 am')])
n_classes_at_5 = len([(room, time) for
(room, time) in assignment.values()
if time.endswith('5 pm')])

Solution 1 is new best solution found in 6 steps, with 6 classes at 8 or 5.
Solution 3 is new best solution found in 2 steps, with 5 classes at 8 or 5.

--- 30/30 points. Your schedule function correctly returned solution with 3 classes at 8 am or 5 pm.

======================================================================
A5 EXECUTION Grade is 90 / 90
======================================================================

___ / 10 points. Your display function returns the correct pandas.DataFrame.

======================================================================
======================================================================

======================================================================
A5 FINAL GRADE is  _  / 100
======================================================================

Extra Credit: Earn one point of extra credit for correct result from schedule_best_for_freshman and discussion about it..

A5 EXTRA CREDIT is 0 / 1


# Extra Credit¶

Let's give the freshman a break and try to schedule their courses in the middle of the day. So, solve the scheduling problem again but with the additional preference of:

• prefer schedules with CS1xx courses scheduled at 11 am, 12 pm, or 1pm.

Create a second version of schedule called schedule_best_for_freshman that includes this additional preference.