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

相关文章

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

golang panic 函数用法示例详解

《golangpanic函数用法示例详解》在Go语言中,panic用于触发不可恢复的错误,终止函数执行并逐层向上触发defer,最终若未被recover捕获,程序会崩溃,recover用于在def... 目录1. panic 的作用2. 基本用法3. recover 的使用规则4. 错误处理建议5. 常见错

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler