我必须在大学里为6个班级制定一个时间表。以下是我提出的课程。
class ClassRoom
{
private:
char id[5];
char type;
int cap;
public:
void setRoom();
void disp();
}c[15];
class StudentGroup
{
int sem;
char group[3];
int strength;
public:
void setGroup();
}stud[8];
class Course
{
char courseID[7];
int semNo;
char courseCode[5];
char courseName[50];
char type;
int lec;
int tut;
int prac;
public:
void setCourse();
}cour[75];
class CourseDetails
{
public:
char courseID[7];
char preference[3];
char resourceType;
char classPref[3];
};
class Faculty
{
char facID[6];
char name[50];
int noCourses;
public:
CourseDetails cD[6];
void setFaculty();
void disp();
}fac[50];
class ElecPair
{
int noPaired;
char courseId[10][7];
public:
void setElecPair();
}ePair[10];
class Period
{
int semNo;
char className[3];
char courseID;
char facID;
bool availability;
};
class TimeTable
{
Period prd[5][7];
}tt[6];
我必须用通常的约束来创建时间表:
没有老师可以同时上两节课
没有学生可以同时上2门课程,等等
我不想要代码,但我希望得到关于在不使用遗传算法的情况下生成此时间表的最佳方法的建议。
PS:有没有办法可以使用堆栈或递归来生成这个时间表?
答案 0 :(得分:0)
我在c#中编写了一个简单的算法,可以解决在同一给定时间内教师,房间和讲座之间0冲突的时间表问题。 它的想法很简单: 1-为每位教师(教师+一段时间)生成可能的讲座。 2 - 为每个房间(房间+时间段)生成可能的讲座。 3-从给定的teacher_periods列表中随机抽取一段时间并检查两件事。 a - 这个随机期是否存在于房间期间? b - 这个随机时期是不是同一类学生? 如果这两个条件都是真的那么你必须做三件事: 1-选择具有相同周期的房间并将其添加到解决方案中(在解决方案列表中)。 2-从教师期间和房间期间列表中删除这段时间。 否则,重复这个过程。 公共课程 {
static List<Room> rooms ;
private static List<Room> GetListeOfRoomsFomSource()
{
throw new NotImplementedException();
}
public static void Main(string[] args)
{
var listTeachers = GetListeOfTeachersFomSource();
rooms= GetListeOfRoomsFomSource();
foreach (var techer in listTeachers)
{
var temp = techer.periods.ToList();
var rnd = new Random();
var rnd2 = new Random();
var period = 0;
var exist = false;
do
{
if (temp.Count == 0) break;
int index = rnd.Next(0, temp.Count - 1);
period = temp[index];
exist = CheckIfLectureExist(period, section,techer);
temp.Remove(temp[index]);
} while (!exist);
if (exist)
{
AddSolution( techer,period);
}
else
{
AddNoSolution();
// list.Remove(tc1);
}
}
}
private static void AddSolution( Teacher item, int period)
{
// get a random room with the this given period;
var lisOfrooms = new List<Room>();
foreach (var room in rooms)
{
if (room.periods.Any(x=>x.Equals(period))) lisOfrooms.Add(room);
}
var rnd2 = new Random();
var r2 = rnd2.Next(0, lisOfrooms.Count());
var availableRoom = lisOfrooms[r2];
// create solution which is a combination of the techer + availableRoom + period + class.
//remove the period from the periods of the given techer;
//remove the period from the periods of the given room;
//r;
}
private static void AddNoSolution()
{
throw new NotImplementedException();
}
private static bool CheckIfLectureExist(int period, object classRoom ,Teacher teacher)
{
// chech if this period has an available (free) room in the list of rooms.
// chech if this period + classroom + techer does not exist in the solutions to avoid the conflict between classes.
throw new NotImplementedException();
}
private static List<Teacher> GetListeOfTeachersFomSource()
{
throw new NotImplementedException();
}
}
public class Teacher{
public Teacher (int periodsWeek)
{
_periodWeek = periodsWeek;
GeneratePerids();
}
int _periodWeek;
public int[] periods {get;set;}
public void GeneratePerids(){
for (int i = 0; i < _periodWeek; i++)
{
//populate periods;
}
}
}
public class Room
{
public Room (int periodsWeek)
{
_periodWeek = periodsWeek;
GeneratePerids();
}
int _periodWeek;
public int[] periods {get;set;}
public void GeneratePerids(){
for (int i = 0; i < _periodWeek; i++)
{
//populate periods;
}
}
}