[fix]: inconsistent course creation when changing NumCoursePerSemester
This commit is contained in:
177
crafter.cs
177
crafter.cs
@@ -80,8 +80,8 @@ public class CourseCrafter
|
||||
}
|
||||
GeneratedCourses.Add((semester, inst));
|
||||
|
||||
MainWindow.Instance.TbResultLog.Text += ($"{semester} -> {inst.Students.Count}\n");
|
||||
MainWindow.Instance.TbResultLog.Text += ($"{students_in_semester[0].Count} - {students_in_semester[1].Count} - {students_in_semester[2].Count} - {students_in_semester[3].Count}\n\n");
|
||||
//MainWindow.Instance.TbResultLog.Text += ($"{semester} -> {inst.Students.Count}\n");
|
||||
//MainWindow.Instance.TbResultLog.Text += ($"{students_in_semester[0].Count} - {students_in_semester[1].Count} - {students_in_semester[2].Count} - {students_in_semester[3].Count}\n\n");
|
||||
}
|
||||
|
||||
semeq0: ;
|
||||
@@ -120,8 +120,8 @@ public class CourseCrafter
|
||||
item.Item2.Remove(s);
|
||||
}
|
||||
}
|
||||
MainWindow.Instance.TbResultLog.Text += ($"{ci.Semester} -> {ci.Instance.Students.Count}\n");
|
||||
MainWindow.Instance.TbResultLog.Text += ($"{students_in_semester[0].Count} - {students_in_semester[1].Count} - {students_in_semester[2].Count} - {students_in_semester[3].Count}\n\n");
|
||||
//MainWindow.Instance.TbResultLog.Text += ($"{ci.Semester} -> {ci.Instance.Students.Count}\n");
|
||||
//MainWindow.Instance.TbResultLog.Text += ($"{students_in_semester[0].Count} - {students_in_semester[1].Count} - {students_in_semester[2].Count} - {students_in_semester[3].Count}\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,7 +234,8 @@ public class CourseCrafter
|
||||
MainWindow.Instance.TbResultTextout.Text += $"{tuple.Item1}: {tuple.Item2.Count} remaining\n";
|
||||
}
|
||||
|
||||
int getSemesterForSport(Sport sp)
|
||||
// ...existing code...
|
||||
int getSemesterForSport2(Sport sp)
|
||||
{
|
||||
int[] semcount = new int[4];
|
||||
|
||||
@@ -248,6 +249,13 @@ public class CourseCrafter
|
||||
{
|
||||
if (sp.Semester[i] == 0) continue;
|
||||
|
||||
// prüfen, ob für diesen Sport im Semester i schon die maximale Anzahl erreicht ist
|
||||
int sportCoursesInSemester = GeneratedCourses
|
||||
.Count(g => g.Semester == i + 1 && g.Instance.Sport.ID == sp.ID);
|
||||
|
||||
if (sportCoursesInSemester >= sp.Semester[i])
|
||||
continue;
|
||||
|
||||
if (semcount[i] < Settings.Instance.NumCoursesPerSemester &&
|
||||
semcount[i] < minCourses)
|
||||
{
|
||||
@@ -258,12 +266,68 @@ public class CourseCrafter
|
||||
|
||||
return bestSem;
|
||||
}
|
||||
|
||||
int getSemesterForSport(Sport sp)
|
||||
{
|
||||
// 1. Zähle alle Kurse pro Semester (egal welche Sportart)
|
||||
int[] totalCoursesPerSemester = new int[4];
|
||||
foreach (var inst in GeneratedCourses)
|
||||
totalCoursesPerSemester[inst.Semester - 1]++;
|
||||
|
||||
int bestSem = 0;
|
||||
int minCourses = int.MaxValue;
|
||||
|
||||
// 2. Kandidaten-Semester durchgehen
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
// a) Sport darf in diesem Semester gar nicht stattfinden?
|
||||
if (sp.Semester[i] == 0)
|
||||
continue;
|
||||
|
||||
int semesterNumber = i + 1;
|
||||
|
||||
// b) Wie viele Kurse DIESES Sports gibt es schon in diesem Semester?
|
||||
int sportCoursesInThisSemester = GeneratedCourses
|
||||
.Count(g => g.Semester == semesterNumber &&
|
||||
g.Instance.Sport.Name == sp.Name);
|
||||
|
||||
// c) Pro-Sport-Limit erreicht?
|
||||
if (sportCoursesInThisSemester >= sp.Semester[i])
|
||||
continue;
|
||||
|
||||
// d) Globales Limit pro Semester erreicht?
|
||||
if (totalCoursesPerSemester[i] >= Settings.Instance.NumCoursesPerSemester)
|
||||
continue;
|
||||
|
||||
// e) Wähle das Semester mit bisher insgesamt den wenigsten Kursen
|
||||
if (totalCoursesPerSemester[i] < minCourses)
|
||||
{
|
||||
minCourses = totalCoursesPerSemester[i];
|
||||
bestSem = semesterNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return bestSem; // 0, falls kein zulässiges Semester gefunden
|
||||
}
|
||||
|
||||
var errors = ValidateCourses(GeneratedCourses);
|
||||
|
||||
if (errors.Count == 0)
|
||||
{
|
||||
MainWindow.Instance.TbResultLog.Text = "Alles generierten Kursen erfüllen die gegebenen Voraussetzungen.";
|
||||
}
|
||||
else
|
||||
{
|
||||
MainWindow.Instance.TbResultLog.Text = "--- Bei der Generierung sind folgende Fehler aufgetreten: ---\n\n";
|
||||
foreach (var e in errors)
|
||||
MainWindow.Instance.TbResultLog.Text += e + "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static string GenerateStatistics()
|
||||
{
|
||||
GeneratedCourses.Sort((x,y) => x.Semester.CompareTo(y.Semester) );
|
||||
string sb = $"Generierte Kurse: {GeneratedCourses.Count}\n\n";
|
||||
foreach (var genc in GeneratedCourses)
|
||||
{
|
||||
@@ -335,4 +399,107 @@ public class CourseCrafter
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static List<string> ValidateCourses(List<(int Semester, CourseInstance Instance)> courses)
|
||||
{
|
||||
List<string> errors = new();
|
||||
|
||||
// --- 1. Min/Max + Semester erlaubt ---
|
||||
foreach (var tuple in courses)
|
||||
{
|
||||
int semester = tuple.Semester;
|
||||
var inst = tuple.Instance;
|
||||
var sport = inst.Sport;
|
||||
|
||||
if (inst.Students.Count < sport.MinStudents)
|
||||
{
|
||||
errors.Add($"[Min] {sport.Name} (Sem {semester}): {inst.Students.Count} < {sport.MinStudents}");
|
||||
}
|
||||
|
||||
if (inst.Students.Count > sport.MaxStudents)
|
||||
{
|
||||
errors.Add($"[Max] {sport.Name} (Sem {semester}): {inst.Students.Count} > {sport.MaxStudents}");
|
||||
}
|
||||
|
||||
if (sport.Semester[semester - 1] == 0)
|
||||
{
|
||||
errors.Add($"[Semester] {sport.Name} darf nicht in Semester {semester} stattfinden");
|
||||
}
|
||||
|
||||
// --- doppelte Schüler im selben Kurs ---
|
||||
for (int i = 0; i < inst.Students.Count; i++)
|
||||
{
|
||||
for (int j = i + 1; j < inst.Students.Count; j++)
|
||||
{
|
||||
if (inst.Students[i] == inst.Students[j])
|
||||
{
|
||||
errors.Add($"[Kurs-Duplikat] {inst.Students[i]} mehrfach in {sport.Name} (Sem {semester})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- 2. Schüler doppelt im Semester ---
|
||||
for (int sem = 1; sem <= 4; sem++)
|
||||
{
|
||||
List<string> students = new();
|
||||
|
||||
foreach (var tuple in courses)
|
||||
{
|
||||
if (tuple.Semester != sem) continue;
|
||||
|
||||
foreach (var stud in tuple.Instance.Students)
|
||||
{
|
||||
// prüfen ob schon drin
|
||||
bool exists = false;
|
||||
foreach (var s in students)
|
||||
{
|
||||
if (s == stud)
|
||||
{
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (exists)
|
||||
{
|
||||
errors.Add($"[Doppelt] Schüler {stud} doppelt in Semester {sem}");
|
||||
}
|
||||
else
|
||||
{
|
||||
students.Add(stud);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. Sport-Angebote pro Semester zählen ---
|
||||
// (ohne Dictionary: wir iterieren über alle Kurse und zählen jeweils erneut)
|
||||
|
||||
foreach (var tuple in courses)
|
||||
{
|
||||
int semester = tuple.Semester;
|
||||
var sport = tuple.Instance.Sport;
|
||||
|
||||
int count = 0;
|
||||
|
||||
foreach (var other in courses)
|
||||
{
|
||||
if (other.Semester == semester &&
|
||||
other.Instance.Sport.Name == sport.Name)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
int allowed = sport.Semester[semester - 1];
|
||||
|
||||
if (count > allowed)
|
||||
{
|
||||
errors.Add($"[Sport-Semester] {sport.Name} in Sem {semester}: {count} Kurse > erlaubt {allowed}");
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user