Making a Class Schedule Using a Genetic Algorithm 中的fitness函数的解析

2024-04-19 14:32

本文主要是介绍Making a Class Schedule Using a Genetic Algorithm 中的fitness函数的解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

genetic algotithm 排课

排课过程中的一些硬条件:

  • A class can be placed only in a spare classroom
  • No professor or student group can have more then one class at a time.
  • A classroom must have enough seats to accommodate all students.
  • To place a class in a classroom, the classroom must have laboratory equipment (computers, in our case) if the class requires it。

Fitness函数积分规则:

  • Each class can have 0 to 5 points
  • If a class uses a spare classroom, we increment its score.
  • If a class requires computers and it is located in the classroom with them, or it doesn't require them, we increment the score of the class.
  • If a class is located in a classroom with enough available seats, guess what, we increment its score.
  • If a professor has no other classes at the time, we increment the class's score once again.
  • The last thing that we check is if any of the student groups that attend the class has any other class at the same time, and if they don't, we increment the score of the class.
  • If a class breaks a rule at any time-space slot that it occupies, its score is not incremented for that rule.
  • The total score of a class schedule is the sum of points of all classes.
  • The fitness value is calculated as schedule_score/maximum_score, and maximum_score is number_of_classes*5.

这之间的联系可以这样:


这些限制条件关联了不同的对象,有课程对教室的要求,有学生或教授对课程的要求,有课程对课程表的要求(时间唯一性)。现在有个问题,有了这些限制条件,问题的解空间该怎么定义?怎么初始化一个初代的课程表?这分为两个步骤,一个是课程已经安排好了(一个课程指的是,确定好了上什么课,哪个老师上,哪些班级的学生去上,教室选在哪里),这时候上课的时间是不定的,也就是只有时间安排这一个变量,这时候把安排好的课程输入排课系统里进行优化排序。怎么安排课程呢?也就是统筹学生,老师,地点这些信息。上面这种思考方式有点偏离问题,教室和一周的时间都是可配置资源,应该是一起安排的,并不是先安排教室,然后在安排时间。

按照上面的资源分配来看,教师也可以看做一种分配资源,不过因为教师一般只教授一门课或者两门课,变化比较小,所以很多课表设计时,把这个部分是定死了。问题有点远了,后续在思考这部分问题,先解析下C++中的fitness源码。

Fitness源码部分c++

// Calculates fitness value of chromosome
void Schedule::CalculateFitness()
{// chromosome's scoreint score = 0;int numberOfRooms = Configuration::GetInstance().GetNumberOfRooms();int daySize = DAY_HOURS * numberOfRooms;int ci = 0;// check criterias and calculate scores for each class in schedulefor( hash_map<CourseClass*, int>::const_iterator it = _classes.begin(); it != _classes.end(); ++it, ci += 5 ){// coordinate of time-space slotint p = ( *it ).second;int day = p / daySize;int time = p % daySize;int room = time / DAY_HOURS;time = time % DAY_HOURS;int dur = ( *it ).first->GetDuration();// check for room overlapping of classesbool ro = false;for( int i = dur - 1; i >= 0; i-- ){if( _slots[ p + i ].size() > 1 ){ro = true;break;}}// on room overlapingif( !ro )score++;_criteria[ ci + 0 ] = !ro;CourseClass* cc = ( *it ).first;Room* r = Configuration::GetInstance().GetRoomById( room );// does current room have enough seats_criteria[ ci + 1 ] = r->GetNumberOfSeats() >= cc->GetNumberOfSeats();if( _criteria[ ci + 1 ] )score++;// does current room have computers if they are required_criteria[ ci + 2 ] = !cc->IsLabRequired() || ( cc->IsLabRequired() && r->IsLab() );if( _criteria[ ci + 2 ] )score++;bool po = false, go = false;// check overlapping of classes for professors and student groupsfor( int i = numberOfRooms, t = day * daySize + time; i > 0; i--, t += DAY_HOURS ){// for each hour of classfor( int i = dur - 1; i >= 0; i-- ){// check for overlapping with other classes at same timeconst list<CourseClass*>& cl = _slots[ t + i ];for( list<CourseClass*>::const_iterator it = cl.begin(); it != cl.end(); it++ ){if( cc != *it ){// professor overlaps?if( !po && cc->ProfessorOverlaps( **it ) )po = true;// student group overlaps?if( !go && cc->GroupsOverlap( **it ) )go = true;// both type of overlapping? no need to check moreif( po && go )goto total_overlap;}}}}total_overlap:// professors have no overlaping classes?if( !po )score++;_criteria[ ci + 3 ] = !po;// student groups has no overlaping classes?if( !go )score++;_criteria[ ci + 4 ] = !go;}// calculate fitess value based on score_fitness = (float)score / ( Configuration::GetInstance().GetNumberOfCourseClasses() * DAYS_NUM );
}


这篇关于Making a Class Schedule Using a Genetic Algorithm 中的fitness函数的解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/917804

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客