814 lines
28 KiB
C#
814 lines
28 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace spplus;
|
|
|
|
public class CourseCrafter
|
|
{
|
|
public class CourseInstance
|
|
{
|
|
public Sport Sport = null!;
|
|
public int Remaining;
|
|
public List<string> Students = new();
|
|
}
|
|
public static List<(int Semester, CourseInstance Instance)> GeneratedCourses
|
|
= new();
|
|
|
|
public static void Craft()
|
|
{
|
|
GeneratedCourses = new();
|
|
int globalCount = 0;
|
|
List<(Sport, List<string>)> initial_sportlist = new();
|
|
List<string>[] students_in_semester = new List<string>[4] { new(), new(), new(), new() };
|
|
foreach (var sp in Settings.Instance.Sports)
|
|
{
|
|
initial_sportlist.Add((sp, new()));
|
|
}
|
|
|
|
foreach (Student s in Settings.Instance.Students)
|
|
{
|
|
foreach (var sp in s.SelectedCourseNames)
|
|
{
|
|
foreach (var item in initial_sportlist)
|
|
{
|
|
if (item.Item1.AlternativeNames.Contains(sp))
|
|
{
|
|
item.Item2.Add(s.ID);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
while (!requestExit())
|
|
{
|
|
Console.WriteLine($"Calculating... ({globalCount})");
|
|
foreach (var item in initial_sportlist)
|
|
{
|
|
if (item.Item2.Count >= item.Item1.MinStudents)
|
|
{
|
|
int semester = getSemesterForSport(item.Item1, item.Item2);
|
|
if (semester <= 0) goto semeq0;
|
|
var inst = new CourseInstance();
|
|
inst.Sport = item.Item1;
|
|
inst.Students = new List<string>();
|
|
// int dist = 1;
|
|
for (int i = item.Item2.Count - 1; i >= 0; i--)
|
|
{
|
|
if (inst.Students.Count >= inst.Sport.MaxStudents)
|
|
break;
|
|
|
|
string stud = item.Item2[i];
|
|
|
|
if (!students_in_semester[semester - 1].Contains(stud))
|
|
{
|
|
inst.Students.Add(stud);
|
|
students_in_semester[semester - 1].Add(stud);
|
|
item.Item2.RemoveAt(i);
|
|
}
|
|
}
|
|
if (inst.Students.Count < inst.Sport.MinStudents)
|
|
{
|
|
// Rückgängig machen
|
|
foreach (var s in inst.Students)
|
|
{
|
|
students_in_semester[semester-1].Remove(s);
|
|
item.Item2.Add(s);
|
|
}
|
|
continue; // Kurs nicht erstellen
|
|
}
|
|
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");
|
|
}
|
|
|
|
semeq0: ;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
FillExistingCourses();
|
|
|
|
// Kurs umdisponieren (besser verteilen)
|
|
// Kurs umdisponieren (besser verteilen)
|
|
bool changed;
|
|
int maxIterations = 20;
|
|
int iteration = 0;
|
|
|
|
do
|
|
{
|
|
changed = false;
|
|
iteration++;
|
|
|
|
// nach Sport gruppieren
|
|
var sports = GeneratedCourses
|
|
.GroupBy(c => c.Instance.Sport.Name);
|
|
|
|
foreach (var sportGroup in sports)
|
|
{
|
|
var courses = sportGroup.ToList();
|
|
|
|
// paarweise vergleichen
|
|
for (int i = 0; i < courses.Count; i++)
|
|
{
|
|
for (int j = 0; j < courses.Count; j++)
|
|
{
|
|
if (i == j) continue;
|
|
|
|
var cA = courses[i];
|
|
var cB = courses[j];
|
|
|
|
// nur sinnvoll, wenn Unterschied
|
|
if (cA.Instance.Students.Count <= cB.Instance.Students.Count + 1)
|
|
continue;
|
|
|
|
// Kandidaten aus A nach B verschieben
|
|
for (int k = cA.Instance.Students.Count - 1; k >= 0; k--)
|
|
{
|
|
string stud = cA.Instance.Students[k];
|
|
|
|
// 1. Zielsemester frei?
|
|
if (!isStudentFree(cB.Semester, stud))
|
|
continue;
|
|
|
|
// 2. Zielkurs hat noch Platz?
|
|
if (cB.Instance.Students.Count >= cB.Instance.Sport.MaxStudents)
|
|
continue;
|
|
|
|
// 3. Quellkurs darf nicht unter Min fallen
|
|
if (cA.Instance.Students.Count - 1 < cA.Instance.Sport.MinStudents)
|
|
continue;
|
|
|
|
// --- MOVE durchführen ---
|
|
cA.Instance.Students.RemoveAt(k);
|
|
students_in_semester[cA.Semester - 1].Remove(stud);
|
|
|
|
cB.Instance.Students.Add(stud);
|
|
students_in_semester[cB.Semester - 1].Add(stud);
|
|
|
|
changed = true;
|
|
break; // nach jedem Move neu bewerten
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} while (changed && iteration < maxIterations);
|
|
|
|
// --- Kurse nachträglich aufteilen, um NumCoursesPerSemester exakt zu erreichen ---
|
|
for (int semester = 1; semester <= 4; semester++)
|
|
{
|
|
int cancel = 0;
|
|
while (GeneratedCourses.Count(c => c.Semester == semester) < Settings.Instance.NumCoursesPerSemester)
|
|
{
|
|
cancel++;
|
|
if (cancel >= 20) break;
|
|
// Kandidaten suchen: splittbare Kurse, deren Sport noch Kapazität hat
|
|
var candidate = GeneratedCourses
|
|
.Where(c => c.Semester == semester)
|
|
.Where(c => c.Instance.Students.Count >= c.Instance.Sport.MinStudents * 2)
|
|
.Where(c =>
|
|
{
|
|
int sportCount = GeneratedCourses.Count(g =>
|
|
g.Semester == semester &&
|
|
g.Instance.Sport.Name == c.Instance.Sport.Name);
|
|
|
|
int allowed = c.Instance.Sport.Semester[semester - 1];
|
|
return sportCount < allowed;
|
|
})
|
|
.OrderByDescending(c => c.Instance.Students.Count)
|
|
.FirstOrDefault();
|
|
|
|
if (candidate.Instance == null)
|
|
break;
|
|
|
|
var students = candidate.Instance.Students;
|
|
int totalStudents = students.Count;
|
|
int movedCount = totalStudents / 2;
|
|
int remainingCount = totalStudents - movedCount;
|
|
|
|
if (movedCount < candidate.Instance.Sport.MinStudents ||
|
|
remainingCount < candidate.Instance.Sport.MinStudents)
|
|
{
|
|
break;
|
|
}
|
|
|
|
var newCourse = new CourseInstance
|
|
{
|
|
Sport = candidate.Instance.Sport,
|
|
Students = new List<string>()
|
|
};
|
|
|
|
var moved = students
|
|
.Skip(remainingCount)
|
|
.Take(movedCount)
|
|
.ToList();
|
|
|
|
foreach (var s in moved)
|
|
{
|
|
candidate.Instance.Students.Remove(s);
|
|
newCourse.Students.Add(s);
|
|
}
|
|
|
|
GeneratedCourses.Add((semester, newCourse));
|
|
}
|
|
}
|
|
|
|
bool rescueChanged;
|
|
do
|
|
{
|
|
rescueChanged = FillExistingCourses();
|
|
|
|
foreach (var item in initial_sportlist)
|
|
{
|
|
if (TryCreateRescueCourse(item.Item1, item.Item2))
|
|
{
|
|
rescueChanged = true;
|
|
}
|
|
}
|
|
} while (rescueChanged);
|
|
|
|
OptimizeStudentWishCoverage();
|
|
OptimizeStudentWishCoverage();
|
|
OptimizeStudentWishCoverage();
|
|
|
|
bool isStudentFree(int semester, string studentID)
|
|
{
|
|
foreach (var inst in GeneratedCourses)
|
|
{
|
|
if (semester != inst.Semester) continue;
|
|
foreach (string stud in inst.Instance.Students)
|
|
{
|
|
if (stud == studentID) return false; // Schüler in genanntem Semester bereits gefunden
|
|
}
|
|
}
|
|
|
|
// Schüler nicht gefunden:
|
|
return true;
|
|
}
|
|
|
|
|
|
bool requestExit()
|
|
{
|
|
globalCount++;
|
|
// max Kursanzahl
|
|
if (GeneratedCourses.Count >= Settings.Instance.NumCoursesPerSemester * 4) return true;
|
|
|
|
|
|
int low = 0;
|
|
foreach (var item in initial_sportlist)
|
|
{
|
|
if (item.Item2.Count < item.Item1.MinStudents) low++;
|
|
}
|
|
|
|
if (low >= initial_sportlist.Count) return true;
|
|
|
|
if (globalCount >= 20) return true;
|
|
return false;
|
|
}
|
|
|
|
int total_missing = 0;
|
|
foreach (var tuple in initial_sportlist)
|
|
{
|
|
int[] missingPerSemester = new int[4];
|
|
foreach (var studentId in tuple.Item2.Distinct())
|
|
{
|
|
for (int semesterIndex = 0; semesterIndex < 4; semesterIndex++)
|
|
{
|
|
if (!students_in_semester[semesterIndex].Contains(studentId))
|
|
{
|
|
missingPerSemester[semesterIndex]++;
|
|
total_missing++;
|
|
}
|
|
}
|
|
}
|
|
|
|
MainWindow.Instance.TbResultTextout.Text +=
|
|
$"{tuple.Item1}: {tuple.Item2.Count} remaining ({missingPerSemester[0]},{missingPerSemester[1]},{missingPerSemester[2]},{missingPerSemester[3]})\n";
|
|
|
|
}
|
|
MainWindow.Instance.TbResultTextout.Text += $"\n total remaining: {total_missing}";
|
|
|
|
bool FillExistingCourses()
|
|
{
|
|
bool changed = false;
|
|
|
|
foreach (var item in initial_sportlist)
|
|
{
|
|
if (item.Item2.Count == 0)
|
|
continue;
|
|
|
|
foreach (var ci in GeneratedCourses
|
|
.Where(ci => ci.Instance.Sport.Name == item.Item1.Name)
|
|
.OrderBy(ci => ci.Instance.Students.Count))
|
|
{
|
|
int semester = ci.Semester;
|
|
List<string> added = new();
|
|
|
|
foreach (string stud in item.Item2)
|
|
{
|
|
if (ci.Instance.Students.Count >= ci.Instance.Sport.MaxStudents)
|
|
break;
|
|
|
|
if (students_in_semester[semester - 1].Contains(stud))
|
|
continue;
|
|
|
|
ci.Instance.Students.Add(stud);
|
|
students_in_semester[semester - 1].Add(stud);
|
|
added.Add(stud);
|
|
}
|
|
|
|
foreach (string s in added)
|
|
{
|
|
item.Item2.Remove(s);
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
bool TryCreateRescueCourse(Sport sport, List<string> remainingStudents)
|
|
{
|
|
if (remainingStudents.Count == 0)
|
|
return false;
|
|
|
|
for (int semester = 1; semester <= 4; semester++)
|
|
{
|
|
int semesterIndex = semester - 1;
|
|
|
|
if (sport.Semester[semesterIndex] == 0)
|
|
continue;
|
|
|
|
int sportCoursesInSemester = GeneratedCourses.Count(c =>
|
|
c.Semester == semester && c.Instance.Sport.Name == sport.Name);
|
|
if (sportCoursesInSemester >= sport.Semester[semesterIndex])
|
|
continue;
|
|
|
|
if (GeneratedCourses.Count(c => c.Semester == semester) >= Settings.Instance.NumCoursesPerSemester)
|
|
continue;
|
|
|
|
var directlyPlaceable = remainingStudents
|
|
.Distinct()
|
|
.Where(studentId => !students_in_semester[semesterIndex].Contains(studentId))
|
|
.ToList();
|
|
|
|
var donorMoves = new List<((int Semester, CourseInstance Instance) Course, string StudentId)>();
|
|
foreach (var course in GeneratedCourses
|
|
.Where(c => c.Instance.Sport.Name == sport.Name && c.Semester != semester)
|
|
.OrderByDescending(c => c.Instance.Students.Count))
|
|
{
|
|
int movableCount = course.Instance.Students.Count - sport.MinStudents;
|
|
if (movableCount <= 0)
|
|
continue;
|
|
|
|
foreach (var studentId in course.Instance.Students.ToList())
|
|
{
|
|
if (movableCount <= 0)
|
|
break;
|
|
|
|
if (students_in_semester[semesterIndex].Contains(studentId))
|
|
continue;
|
|
|
|
donorMoves.Add((course, studentId));
|
|
movableCount--;
|
|
}
|
|
}
|
|
|
|
if (directlyPlaceable.Count + donorMoves.Count < sport.MinStudents)
|
|
continue;
|
|
|
|
var newCourse = new CourseInstance
|
|
{
|
|
Sport = sport,
|
|
Students = new List<string>()
|
|
};
|
|
|
|
foreach (var studentId in directlyPlaceable)
|
|
{
|
|
if (newCourse.Students.Count >= sport.MaxStudents)
|
|
break;
|
|
|
|
newCourse.Students.Add(studentId);
|
|
}
|
|
|
|
foreach (var move in donorMoves)
|
|
{
|
|
if (newCourse.Students.Count >= sport.MaxStudents)
|
|
break;
|
|
|
|
if (newCourse.Students.Contains(move.StudentId))
|
|
continue;
|
|
|
|
move.Course.Instance.Students.Remove(move.StudentId);
|
|
students_in_semester[move.Course.Semester - 1].Remove(move.StudentId);
|
|
newCourse.Students.Add(move.StudentId);
|
|
}
|
|
|
|
if (newCourse.Students.Count < sport.MinStudents)
|
|
continue;
|
|
|
|
foreach (var studentId in newCourse.Students)
|
|
{
|
|
remainingStudents.Remove(studentId);
|
|
students_in_semester[semesterIndex].Add(studentId);
|
|
}
|
|
|
|
GeneratedCourses.Add((semester, newCourse));
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void OptimizeStudentWishCoverage()
|
|
{
|
|
bool changed;
|
|
int iterations = 0;
|
|
|
|
do
|
|
{
|
|
changed = false;
|
|
iterations++;
|
|
|
|
foreach (var student in Settings.Instance.Students)
|
|
{
|
|
if (TryImproveStudentCoverage(student))
|
|
{
|
|
changed = true;
|
|
}
|
|
}
|
|
} while (changed && iterations < 20);
|
|
}
|
|
|
|
bool TryImproveStudentCoverage(Student student)
|
|
{
|
|
var assignments = GetAssignmentsBySemester(student.ID);
|
|
if (assignments.All(a => a != null))
|
|
return false;
|
|
|
|
var openSemesters = Enumerable.Range(0, 4)
|
|
.Where(i => assignments[i] == null)
|
|
.Select(i => i + 1)
|
|
.ToList();
|
|
if (openSemesters.Count == 0)
|
|
return false;
|
|
|
|
var assignedSports = assignments
|
|
.Where(a => a != null)
|
|
.Select(a => a!.Value.Instance.Sport.Name)
|
|
.ToHashSet();
|
|
|
|
var preferredSports = student.SelectedCourseNames
|
|
.Select(ResolveSportFromSelection)
|
|
.Where(sport => sport != null)
|
|
.DistinctBy(sport => sport!.Name)
|
|
.Select(sport => sport!)
|
|
.Where(sport => !assignedSports.Contains(sport.Name))
|
|
.ToList();
|
|
|
|
foreach (var desiredSport in preferredSports)
|
|
{
|
|
for (int sourceSemester = 1; sourceSemester <= 4; sourceSemester++)
|
|
{
|
|
var currentCourse = assignments[sourceSemester - 1];
|
|
if (currentCourse == null)
|
|
continue;
|
|
|
|
var currentCourseValue = currentCourse.Value;
|
|
|
|
var desiredCourse = GeneratedCourses
|
|
.FirstOrDefault(course =>
|
|
course.Semester == sourceSemester &&
|
|
course.Instance.Sport.Name == desiredSport.Name &&
|
|
course.Instance.Students.Count < course.Instance.Sport.MaxStudents);
|
|
|
|
if (desiredCourse.Instance == null)
|
|
continue;
|
|
|
|
foreach (var targetSemester in openSemesters)
|
|
{
|
|
var relocationTarget = GeneratedCourses
|
|
.FirstOrDefault(course =>
|
|
course.Semester == targetSemester &&
|
|
course.Instance.Sport.Name == currentCourseValue.Instance.Sport.Name &&
|
|
course.Instance.Students.Count < course.Instance.Sport.MaxStudents);
|
|
|
|
if (relocationTarget.Instance == null)
|
|
continue;
|
|
|
|
if (!CanRelocateStudent(student.ID, currentCourseValue, relocationTarget))
|
|
continue;
|
|
|
|
currentCourseValue.Instance.Students.Remove(student.ID);
|
|
students_in_semester[sourceSemester - 1].Remove(student.ID);
|
|
|
|
relocationTarget.Instance.Students.Add(student.ID);
|
|
students_in_semester[targetSemester - 1].Add(student.ID);
|
|
|
|
desiredCourse.Instance.Students.Add(student.ID);
|
|
students_in_semester[sourceSemester - 1].Add(student.ID);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool CanRelocateStudent(
|
|
string studentId,
|
|
(int Semester, CourseInstance Instance) sourceCourse,
|
|
(int Semester, CourseInstance Instance) targetCourse)
|
|
{
|
|
if (sourceCourse.Semester == targetCourse.Semester)
|
|
return false;
|
|
|
|
if (sourceCourse.Instance.Students.Count - 1 < sourceCourse.Instance.Sport.MinStudents)
|
|
return false;
|
|
|
|
if (targetCourse.Instance.Students.Count >= targetCourse.Instance.Sport.MaxStudents)
|
|
return false;
|
|
|
|
if (!isStudentFree(targetCourse.Semester, studentId))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
(int Semester, CourseInstance Instance)?[] GetAssignmentsBySemester(string studentId)
|
|
{
|
|
var assignments = new (int Semester, CourseInstance Instance)?[4];
|
|
|
|
foreach (var course in GeneratedCourses)
|
|
{
|
|
if (course.Instance.Students.Contains(studentId))
|
|
{
|
|
assignments[course.Semester - 1] = course;
|
|
}
|
|
}
|
|
|
|
return assignments;
|
|
}
|
|
|
|
Sport? ResolveSportFromSelection(string selectedCourseName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(selectedCourseName) ||
|
|
string.Equals(selectedCourseName, "null", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return Settings.Instance.Sports
|
|
.FirstOrDefault(sport => sport.AlternativeNames.Contains(selectedCourseName));
|
|
}
|
|
|
|
int getSemesterForSport2(Sport sp, List<string> interestedStudents)
|
|
{
|
|
int[] semcount = new int[4];
|
|
|
|
foreach (var inst in GeneratedCourses)
|
|
semcount[inst.Semester - 1]++;
|
|
|
|
int bestSem = 0;
|
|
int minCourses = int.MaxValue;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
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.Name == sp.Name);
|
|
|
|
if (sportCoursesInSemester >= sp.Semester[i])
|
|
continue;
|
|
|
|
int freeInterestedStudents = interestedStudents
|
|
.Distinct()
|
|
.Count(studentId => !students_in_semester[i].Contains(studentId));
|
|
|
|
if (freeInterestedStudents < sp.MinStudents)
|
|
continue;
|
|
|
|
if (semcount[i] < Settings.Instance.NumCoursesPerSemester &&
|
|
semcount[i] < minCourses)
|
|
{
|
|
minCourses = semcount[i];
|
|
bestSem = i + 1;
|
|
}
|
|
}
|
|
|
|
return bestSem;
|
|
}
|
|
|
|
int getSemesterForSport(Sport sp, List<string> interestedStudents)
|
|
{
|
|
// 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;
|
|
int maxFreeInterestedStudents = -1;
|
|
|
|
// 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;
|
|
|
|
int freeInterestedStudents = interestedStudents
|
|
.Distinct()
|
|
.Count(studentId => !students_in_semester[i].Contains(studentId));
|
|
|
|
// e) Ohne genügend freie Interessenten kann in diesem Semester kein Kurs entstehen
|
|
if (freeInterestedStudents < sp.MinStudents)
|
|
continue;
|
|
|
|
// f) Primär das Semester mit den meisten tatsächlich freien Interessenten wählen,
|
|
// sekundär das mit den wenigsten Kursen insgesamt.
|
|
if (freeInterestedStudents > maxFreeInterestedStudents ||
|
|
(freeInterestedStudents == maxFreeInterestedStudents &&
|
|
totalCoursesPerSemester[i] < minCourses))
|
|
{
|
|
maxFreeInterestedStudents = freeInterestedStudents;
|
|
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 = "--- Alle 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";
|
|
}
|
|
|
|
foreach (var student in Settings.Instance.Students)
|
|
{
|
|
student.Result = new string[4];
|
|
}
|
|
|
|
foreach (var course in GeneratedCourses)
|
|
{
|
|
foreach (var student in Settings.Instance.Students)
|
|
{
|
|
if (course.Instance.Students.Contains(student.ID))
|
|
{
|
|
student.Result[course.Semester-1] = course.Instance.Sport.Name;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
{
|
|
sb += $"Sem. {genc.Semester}: {genc.Instance.Sport.Name} ({genc.Instance.Students.Count} SuS)\n";
|
|
}
|
|
|
|
|
|
|
|
return sb;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|