#!/usr/bin/env python # coding: utf-8 # $\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](https://www.cs.colostate.edu/~anderson/cs440/notebooks/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. # *Type your name here.* # 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) # In[7]: random.seed(555) n_solutions_to_test = 1000 max_steps = 100 assignments = schedule(classes, times, rooms, max_steps, n_solutions_to_test) # In[8]: display(assignments, rooms, times) # ## 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](https://colostate.instructure.com/courses/109411). # ## Grading # # Download [A5grader.tar](http://www.cs.colostate.edu/~anderson/cs440/notebooks/A5grader.tar) and extract `A5grader.py` from it. # In[2]: get_ipython().run_line_magic('run', '-i A5grader.py') # # 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.