以下是一个使用遗传算法来生成每天每节课课程表并输出到Excel的Python代码示例,代码考虑了不同年级、科目、老师的课时安排,以及语数英、体育等科目的排课限制:

import random
import xlwt

# 定义班级和老师的信息
grades = ['6年级']
classes = ['班级1', '班级2', '班级3', '班级4', '班级5', '班级6']
teachers = ['老师1', '老师2', '老师3', '老师4', '老师5', '老师6', '老师7', '老师8', '老师9', '老师10', '老师11']

# 定义科目和指定科目的课时量
subjects = ['语文', '数学', '英语', '体育', '科学', '音乐', '美术', '地理', '历史', '政治', '品德']
subject_hours = {
    '语文': [4, 3, 3, 2, 2, 0],  # 每个年级的课时量
    '数学': [4, 3, 3, 2, 2, 0],
    '英语': [4, 3, 3, 2, 2, 0],
    '体育': [2, 2, 2, 2, 2, 0],
    '科学': [2, 2, 2, 2, 2, 0],
    '音乐': [0, 0, 0, 0, 0, 0],
    '美术': [0, 0, 0, 0, 0, 0],
    '地理': [0, 0, 0, 0, 0, 0],
    '历史': [0, 0, 0, 0, 0, 0],
    '政治': [0, 0, 0, 0, 0, 0],
    '品德': [0, 0, 0, 0, 0, 0]
}

# 定义每天每节课的课程表
class Timetable:
    def __init__(self):
        self.timetable = [[None for _ in range(7)] for _ in range(5)]  # 初始化课程表为空

    def set_subject(self, day, period, subject):
        self.timetable[day][period] = subject

    def get_subject(self, day, period):
        return self.timetable[day][period]

    def is_empty(self, day, period):
        return self.timetable[day][period] is None

    def __str__(self):
        timetable_str = ''
        for day in range(5):
            for period in range(7):
                if self.is_empty(day, period):
                    timetable_str += '空  '
                else:
                    timetable_str += self.get_subject(day, period) + '  '
            timetable_str += '
'
        return timetable_str

# 定义遗传算法参数
POPULATION_SIZE = 100  # 种群大小
GENERATIONS = 100  # 迭代次数
MUTATION_RATE = 0.1  # 变异概率

# 初始化种群
def init_population():
    population = []
    for _ in range(POPULATION_SIZE):
        timetable = Timetable()
        for day in range(5):
            for period in range(7):
                if period < 3:  # 前3节课排语数英
                    subject = random.choice(['语文', '数学', '英语'])
                else:  # 后4节课排其他科目
                    subject = random.choice(['体育', '科学', '音乐', '美术', '地理', '历史', '政治', '品德'])
                timetable.set_subject(day, period, subject)
        population.append(timetable)
    return population

# 计算适应度函数
def calculate_fitness(timetable):
    fitness = 0
    for grade, subject_hour in subject_hours.items():
        for i in range(len(subject_hour)):
            count = 0
            for day in range(5):
                for period in range(7):
                    if timetable.get_subject(day, period) == grade:
                        count += 1
            if count != subject_hour[i]:
                fitness += abs(count - subject_hour[i])
    return fitness

# 选择操作
def selection(population):
    fitnesses = [calculate_fitness(timetable) for timetable in population]
    total_fitness = sum(fitnesses)
    probabilities = [fitness / total_fitness for fitness in fitnesses]
    selected = random.choices(population, probabilities, k=2)
    return selected[0], selected[1]

# 交叉操作
def crossover(timetable1, timetable2):
    new_timetable = Timetable()
    for day in range(5):
        for period in range(7):
            if random.random() < 0.5:
                new_timetable.set_subject(day, period, timetable1.get_subject(day, period))
            else:
                new_timetable.set_subject(day, period, timetable2.get_subject(day, period))
    return new_timetable

# 变异操作
def mutation(timetable):
    new_timetable = Timetable()
    for day in range(5):
        for period in range(7):
            if random.random() < MUTATION_RATE:
                if period < 3:
                    new_subject = random.choice(['语文', '数学', '英语'])
                else:
                    new_subject = random.choice(['体育', '科学', '音乐', '美术', '地理', '历史', '政治', '品德'])
                new_timetable.set_subject(day, period, new_subject)
            else:
                new_timetable.set_subject(day, period, timetable.get_subject(day, period))
    return new_timetable

# 运行遗传算法
def run_genetic_algorithm():
    population = init_population()

    for generation in range(GENERATIONS):
        new_population = []

        for _ in range(POPULATION_SIZE // 2):
            timetable1, timetable2 = selection(population)
            new_timetable = crossover(timetable1, timetable2)
            new_timetable = mutation(new_timetable)
            new_population.append(new_timetable)

        population = new_population

    best_timetable = min(population, key=calculate_fitness)
    return best_timetable

# 输出课程表到Excel
def output_to_excel(timetable):
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet('课程表')
    for day in range(5):
        for period in range(7):
            subject = timetable.get_subject(day, period)
            sheet.write(day, period, subject)
    workbook.save('timetable.xls')

# 运行遗传算法并输出课程表到Excel
best_timetable = run_genetic_algorithm()
output_to_excel(best_timetable)

以上代码假设每天每节课的上课科目是固定的,具体的排课规则可以根据实际需求进行调整。生成的课程表会保存为名为'timetable.xls'的Excel文件。

遗传算法自动排课:Python代码生成小学课程表并输出到Excel

原文地址: https://www.cveoy.top/t/topic/qor6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录