#coding=UTF-8
import json
import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
import numpy as np
# import geatpy as ea
# import matplotlib.pyplot as plt
import time
import datetime
from dateutil import rrule
from clustering import cluster
import error_control
from genetic import *
import random
class generation():
with open('input.json', 'r', encoding='gb2312') as f:
content = json.load(f)
students = content['students']
teachers = content['teachers']
classroom = content['classrooms']
subject = content['subjects']
courses = content['courses']
tools = content['tools']
schedule = content['schedule']
def __init__(self):
generation.subject_info(self)
generation.schedule_info_read(self)
generation.tool_info(self)
generation.room_info(self)
return
# 根据校历计算课时数
def schedule_info_read(self):
week_mask = str()
week_on = [0] * 7
date_list = list()
week_on[0] = int(generation.schedule["monday"])
week_on[1] = int(generation.schedule["tuesday"])
week_on[2] = int(generation.schedule["wednesday"])
week_on[3] = int(generation.schedule["thursday"])
week_on[4] = int(generation.schedule["friday"])
week_on[5] = int(generation.schedule["saturday"])
week_on[6] = int(generation.schedule["sunday"])
lessonNumAm = int(generation.schedule["lessonNumAm"])
lessonNumPm = int(generation.schedule["lessonNumPm"])
if week_on[0] != 0:
week_mask += "Mon"
if week_on[1] != 0:
week_mask += " Tue"
if week_on[2] != 0:
week_mask += " Wed"
if week_on[3] != 0:
week_mask += " Thu"
if week_on[4] != 0:
week_mask += " Fri"
if week_on[5] != 0:
week_mask += " Sat"
if week_on[6] != 0:
week_mask += " Sun"
holiday_list = CustomBusinessDay(holidays=generation.schedule["holiday"], weekmask=week_mask)
s_day = self.schedule["startTermBegin"]
e_day = self.schedule["startTermEnd"]
bus_day = pd.date_range(start=s_day, end=e_day, freq=holiday_list)
day_period = list()
times_sum = 0
for day in bus_day:
weekday = day.weekday() + 1
if week_on[weekday] == 1:
times = int(generation.schedule["lessonNumAm"]) + int(generation.schedule["lessonNumPm"])
if week_on[weekday] == 2:
times = int(generation.schedule["lessonNumAm"])
if week_on[weekday] == 3:
times = int(generation.schedule["lessonNumPm"])
times_sum += times
day_period.append(times_sum)
generation.bus_day = bus_day
generation.day_period = day_period # 每天对应哪几节课
generation.times_sum = times_sum
generation.week_on = week_on
generation.lessonNumAm = lessonNumAm
generation.lessonNumPm = lessonNumPm
return bus_day
# 科目信息读取,在教师信息之前读入
def subject_info(self):
subject = dict()
for i in generation.subject:
subject_id = i["subjectId"]
subject[subject_id] = dict()
subject[subject_id]["subjectNumber"] = i["subjectNumber"]
subject[subject_id]["subjectName"] = i["subjectName"]
subject[subject_id]["teacher"] = list()
subject[subject_id]["course"] = list()
# if i["subjectNumber"] % self.bus_week == 0:
# times = i["subjectNumber"] // self.bus_week
# else:
# times = i["subjectNumber"] // self.bus_week + 1
# subject_arrange.append(int(times))
generation.subject = subject
self.teacher_info()
self.course_info()
return
# 老师信息读取和安排
def teacher_info(self):
generation.teacher_num = len(generation.teachers)
# teacher_work = [0] * generation.teacher_num
count = 0
for i in generation.teachers:
sub = i["subjectId"]
if type(sub) == list:
for j in sub:
if j in generation.subject:
generation.subject[j]["teacher"].append(count)
else:
print("第",count,"老师无课")
else:
if sub in generation.subject:
generation.subject[sub]["teacher"].append(count)
else:
print("第",count,"老师无课")
# 在teacher字典中加入给每个老师安排课程和工作量安排
i["workload"] = 0
i["subject"] = list()
count += 1
# self.teacher_work = teacher_work
return
# 课程信息读取和安排
def course_info(self):
# subject = dict()
# subject["course"] = dict()
count = 0
for course in generation.courses:
sub_id = course["subjectId"]
if sub_id in generation.subject:
generation.subject[sub_id]["course"].append(count)
else:
print("course", count, "is not in subjects")
count += 1
self.id2course()
return
# 教室信息读取
def room_info(self):
room_type = dict()
count = 0
for i in generation.classroom:
if i["typeId"] in room_type:
room_type[i["typeId"]].append(count)
else:
room_type[i["typeId"]] = list()
room_type[i["typeId"]].append(count)
count += 1
generation.room_type = room_type
return
#教具信息读取
def tool_info(self):
toolcode2num = dict()
for i in generation.tools:
toolcode2num[i["code"]] = i["count"]
generation.toolcode2num = toolcode2num
return
def id2course(self):
p = {}
courses = self.courses
for i in range(len(courses)):
if type(courses[i]['goalId']) == list:
for j in courses[i]['goalId']:
if j in p:
p[j].append(i)
else:
p[j] = list()
p[j].append(i)
else:
j = courses[i]['goalId']
if j in p:
p[j].append(i)
else:
p[j] = list()
p[j].append(i)
self.goalid2course = p
return p
def arrange(self):
cluster_num = 4
clustering = cluster(self, cluster_num)
# clustering.display(self)
subject_arrange = list()
cluster_dict = self.cluster_dict
for clusterId in cluster_dict:
for subjectId in cluster_dict[clusterId]["sub2cou"]:
length = len(cluster_dict[clusterId]["sub_times"][subjectId])
subtime_sum = cluster_dict[clusterId]["sub_times"][subjectId][length - 1]
a = list(range(self.times_sum))
if subtime_sum > self.times_sum:
error_control.error_info(301)
a = random.sample(a, subtime_sum)
a.sort()
time_num = 0
subject = dict()
subject["cluster"] = clusterId
subject["subjectId"] = subjectId
subject["subtime_sum"] = subtime_sum
workload_min = 65535
teacher_length = len(generation.teachers)
for count in range(teacher_length):
if generation.teachers[count]["workload"] < workload_min :
teacherId = count
workload_min = generation.teachers[count]["workload"]
subject["teacher"] = teacherId
subject["course"] = list()
course_list = cluster_