import json import gurobipy as gp from gurobipy import GRB from datetime import datetime def load_data_from_json(jsonfile): with open(jsonfile, 'r') as file: data = json.load(file) return data def optimize_course_scheduling(data): # Create a new Gurobi model model = gp.Model("course_scheduling") # Create decision variables x = {} # Classroom assignment variables classroom_ids = [] # List to store ClassroomIds section_ids = [] # List to store SectionIds for classroom in data['Classrooms']: id = str(classroom["Id"]) # Convert Id to string capacity = classroom["Capacity"] name = classroom["Name"] x[id] = {} for section in data['Sections']: section_id = str(section["Id"]) # Convert Id to string section_ids.append(section_id) section_start_time = datetime.strptime(section["StartTime"], "%H:%M:%S") section_end_time = datetime.strptime(section["EndTime"], "%H:%M:%S") x[id][section_id] = model.addVar(vtype=GRB.BINARY, name=f"x_{id}_{section_id}") classroom_ids.append(id) # Constraint: Classroom capacity should not be exceeded model.addConstr(classroom["Capacity"] <= capacity * x[id][section_id]) # Constraint: No time conflicts for other_section in data['Sections']: if other_section["Id"] != section_id: other_start_time = datetime.strptime(other_section["StartTime"], "%H:%M:%S") other_end_time = datetime.strptime(other_section["EndTime"], "%H:%M:%S") # Check for time overlap if section_start_time < other_end_time and other_start_time < section_end_time: model.addConstr(x[id][section_id] + x[id][str(other_section["Id"])] <= 1) # Set objective function model.setObjective(gp.quicksum(x[id][section_id] for id in x for section_id in x[id]), GRB.MAXIMIZE) # Optimize the model model.optimize() # Print the solution if model.status == GRB.OPTIMAL: print("Optimal solution found!") solution = [] assigned_classrooms = [] for id in x: for section_id in x[id]: if x[id][section_id].x > 0.5: classroom_id = classroom_ids.index(id) + 1 # Find the index of the id in classroom_ids and add 1 to get ClassroomId section_id = int(section_id) # Convert section_id back to int to get SectionId # Get additional data for ClassroomId classroom_data = data['Classrooms'][classroom_id - 1] # Subtract 1 to match the index classroom_name = classroom_data['Name'] classroom_capacity = classroom_data['Capacity'] # Get additional data for SectionId section_data = data['Sections'][section_id - 1] # Subtract 1 to match the index section_start_time = section_data['StartTime'] section_end_time = section_data['EndTime'] section_name = section_data['CourseCode'] section_day = section_data['Day'] tempdata = { 'StartTime': section_start_time, 'EndTime': section_end_time, 'CourseCode': section_name, 'Name