[chore:] improved, but not good craft()-method
This commit is contained in:
141
crafter.cs
141
crafter.cs
@@ -17,6 +17,7 @@ public class CourseCrafter
|
|||||||
public static void Craft()
|
public static void Craft()
|
||||||
{
|
{
|
||||||
GeneratedCourses = new();
|
GeneratedCourses = new();
|
||||||
|
|
||||||
var settings = Settings.Instance;
|
var settings = Settings.Instance;
|
||||||
var students = settings.Students;
|
var students = settings.Students;
|
||||||
var sports = settings.Sports;
|
var sports = settings.Sports;
|
||||||
@@ -26,11 +27,9 @@ public class CourseCrafter
|
|||||||
|
|
||||||
int semesterCount = sports.Max(s => s.Semester.Length);
|
int semesterCount = sports.Max(s => s.Semester.Length);
|
||||||
|
|
||||||
// Result initialisieren
|
|
||||||
foreach (var st in students)
|
foreach (var st in students)
|
||||||
st.Result = Enumerable.Repeat("Fehler", semesterCount).ToList();
|
st.Result = Enumerable.Repeat("Fehler", semesterCount).ToList();
|
||||||
|
|
||||||
// Mapping Name + Alternativen → Sport
|
|
||||||
var nameMap = new Dictionary<string, Sport>(StringComparer.OrdinalIgnoreCase);
|
var nameMap = new Dictionary<string, Sport>(StringComparer.OrdinalIgnoreCase);
|
||||||
foreach (var sp in sports)
|
foreach (var sp in sports)
|
||||||
{
|
{
|
||||||
@@ -39,128 +38,86 @@ public class CourseCrafter
|
|||||||
nameMap[alt] = sp;
|
nameMap[alt] = sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== Semesterweise Planung =====
|
// Nachfrage je Sport
|
||||||
for (int sem = 0; sem < semesterCount; sem++)
|
var demand = new Dictionary<Sport, List<Student>>();
|
||||||
{
|
|
||||||
int remainingCourseSlots = settings.NumCoursesPerSemester;
|
|
||||||
|
|
||||||
// Interessenten dieses Semesters
|
|
||||||
var interested = new Dictionary<Sport, List<Student>>();
|
|
||||||
foreach (var sp in sports)
|
foreach (var sp in sports)
|
||||||
interested[sp] = new List<Student>();
|
demand[sp] = new List<Student>();
|
||||||
|
|
||||||
foreach (var st in students)
|
foreach (var st in students)
|
||||||
{
|
{
|
||||||
if (st.SelectedCourseNames.Count <= sem)
|
foreach (var sel in st.SelectedCourseNames.Distinct())
|
||||||
continue;
|
{
|
||||||
|
|
||||||
var sel = st.SelectedCourseNames[sem];
|
|
||||||
if (nameMap.TryGetValue(sel, out var sp))
|
if (nameMap.TryGetValue(sel, out var sp))
|
||||||
interested[sp].Add(st);
|
demand[sp].Add(st);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var instances = new List<CourseInstance>();
|
// ===== Semesterweise echte Verteilung =====
|
||||||
|
for (int sem = 0; sem < semesterCount; sem++)
|
||||||
|
{
|
||||||
|
int remainingSemesterSlots = settings.NumCoursesPerSemester;
|
||||||
|
|
||||||
// ===== Kurse erzeugen =====
|
|
||||||
foreach (var sp in sports)
|
foreach (var sp in sports)
|
||||||
{
|
{
|
||||||
if (sem >= sp.Semester.Length)
|
if (sem >= sp.Semester.Length)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int demand = interested[sp].Count;
|
if (remainingSemesterSlots <= 0)
|
||||||
if (demand < sp.MinStudents)
|
break;
|
||||||
|
|
||||||
|
var interested = demand[sp]
|
||||||
|
.Where(st =>
|
||||||
|
st.Result![sem] == "Fehler" &&
|
||||||
|
!st.Result.Contains(sp.Name))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (interested.Count < sp.MinStudents)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int needed = (int)Math.Ceiling(demand / (double)sp.MaxStudents);
|
int maxInstances = Math.Min(sp.Semester[sem], remainingSemesterSlots);
|
||||||
int allowed = Math.Min(needed, sp.Semester[sem]);
|
int instanceCount = 0;
|
||||||
allowed = Math.Min(allowed, remainingCourseSlots);
|
int index = 0;
|
||||||
|
|
||||||
if (allowed <= 0)
|
while (interested.Count - index >= sp.MinStudents &&
|
||||||
continue;
|
instanceCount < maxInstances)
|
||||||
|
|
||||||
remainingCourseSlots -= allowed;
|
|
||||||
|
|
||||||
for (int i = 0; i < allowed; i++)
|
|
||||||
{
|
{
|
||||||
var instance = new CourseInstance
|
var inst = new CourseInstance
|
||||||
{
|
{
|
||||||
Sport = sp,
|
Sport = sp,
|
||||||
Remaining = sp.MaxStudents
|
Remaining = sp.MaxStudents
|
||||||
};
|
};
|
||||||
instances.Add(instance);
|
|
||||||
GeneratedCourses.Add((sem, instance));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== Schüler verteilen =====
|
GeneratedCourses.Add((sem, inst));
|
||||||
foreach (var sp in sports)
|
remainingSemesterSlots--;
|
||||||
|
instanceCount++;
|
||||||
|
|
||||||
|
int filled = 0;
|
||||||
|
|
||||||
|
while (filled < sp.MaxStudents &&
|
||||||
|
index < interested.Count)
|
||||||
{
|
{
|
||||||
var pool = interested[sp];
|
var st = interested[index++];
|
||||||
if (pool.Count == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var targets = instances.Where(i => i.Sport == sp).ToList();
|
|
||||||
if (targets.Count == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
int idx = 0;
|
|
||||||
foreach (var st in pool)
|
|
||||||
{
|
|
||||||
if (st.SelectedCourseNames.Count <= sem)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (st.Result![sem] != "Fehler")
|
if (st.Result![sem] != "Fehler")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
bool assigned = false;
|
|
||||||
|
|
||||||
for (int t = 0; t < targets.Count; t++)
|
|
||||||
{
|
|
||||||
var inst = targets[(idx + t) % targets.Count];
|
|
||||||
if (inst.Remaining > 0)
|
|
||||||
{
|
|
||||||
inst.Remaining--;
|
|
||||||
inst.Students.Add(st);
|
inst.Students.Add(st);
|
||||||
|
inst.Remaining--;
|
||||||
st.Result[sem] = sp.Name;
|
st.Result[sem] = sp.Name;
|
||||||
idx++;
|
filled++;
|
||||||
assigned = true;
|
}
|
||||||
|
|
||||||
|
// Falls Mindestanzahl nicht erreicht → Instanz verwerfen
|
||||||
|
if (inst.Students.Count < sp.MinStudents)
|
||||||
|
{
|
||||||
|
foreach (var st in inst.Students)
|
||||||
|
st.Result[sem] = "Fehler";
|
||||||
|
|
||||||
|
GeneratedCourses.Remove((sem, inst));
|
||||||
|
remainingSemesterSlots++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!assigned)
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== Alternativen =====
|
|
||||||
foreach (var st in students)
|
|
||||||
{
|
|
||||||
if (st.SelectedCourseNames.Count <= sem)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (st.Result![sem] != "Fehler")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var sel = st.SelectedCourseNames[sem];
|
|
||||||
if (!nameMap.TryGetValue(sel, out var wanted))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
foreach (var altId in wanted.AlternativeCourses)
|
|
||||||
{
|
|
||||||
var altSport = sports.FirstOrDefault(s => s.ID == altId);
|
|
||||||
if (altSport == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var inst = instances.FirstOrDefault(i => i.Sport == altSport && i.Remaining > 0);
|
|
||||||
if (inst == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
inst.Remaining--;
|
|
||||||
inst.Students.Add(st);
|
|
||||||
st.Result[sem] = altSport.Name;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user