我用 c# 写了一个简单的算法,可以解决在同一给定时间内教师、房间和讲座之间的 0 冲突的时间表问题。它的想法很简单: 1- 为每个老师生成可能的讲座(老师+时间段)。2 - 为每个房间(房间+时间段)生成可能的讲座。3- 从给定的teacher_periods 列表中随机抽取一个周期并检查两件事。a - 这个随机周期是否存在于房间周期中?b - 这个随机时间不是由同一班学生参加的吗??如果这两个条件都成立,那么你必须做 3 件事: 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;
}
}
}