Skip to content

Eliminate Schedule Generator Duplicates #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 86 additions & 21 deletions src/components/ScheduleGenerate/ScheduleGenerateModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,24 @@
</button>
<button
@click="() => paginate(1)"
:disabled="currentPage === 5"
:class="'footer-button' + (currentPage === 5 ? ' footer-button-disabled' : ' ')"
:disabled="currentPage === generatedScheduleOutputs.length"
:class="
'footer-button' +
(currentPage === generatedScheduleOutputs.length ? ' footer-button-disabled' : ' ')
"
>
<span :class="currentPage === 5 ? 'footer-text-disabled' : 'footer-text'">Next</span>
<span
:class="
currentPage === generatedScheduleOutputs.length
? 'footer-text-disabled'
: 'footer-text'
"
>Next</span
>
</button>
<span class="pagination-text ml-25">Page {{ currentPage }}/5</span>
<span class="pagination-text ml-25"
>Page {{ currentPage }}/{{ generatedScheduleOutputs.length }}</span
>
</div>
<div class="download-button" @click="downloadSchedule">
<svg
Expand Down Expand Up @@ -112,11 +124,7 @@ import { generateSchedulePDF } from '@/tools/export-plan';
import GeneratorRequest from '@/schedule-generator/generator-request';
import ScheduleGenerator from '@/schedule-generator/algorithm';
import type { GeneratedScheduleOutput } from '@/schedule-generator/algorithm';
import Course, {
CourseForFrontend,
DayOfTheWeek,
Timeslot,
} from '@/schedule-generator/course-unit';
import Course, { CourseForFrontend, DayOfTheWeek } from '@/schedule-generator/course-unit';
import Requirement from '@/schedule-generator/requirement';

export default defineComponent({
Expand Down Expand Up @@ -168,12 +176,7 @@ export default defineComponent({
);
},
generateSchedules() {
const output: {
semester: string;
schedule: Map<Course, Timeslot[]>;
fulfilledRequirements: Map<string, Requirement[]>;
totalCredits: number;
}[] = [];
const outputs: GeneratedScheduleOutput[] = [];

function getRandomDaySet(): DayOfTheWeek[] {
const daySets = [
Expand All @@ -190,8 +193,63 @@ export default defineComponent({
Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
);
}
function deepEqual(a: GeneratedScheduleOutput, b: GeneratedScheduleOutput): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}

/**
* Checks if two `GeneratedScheduleOutput` objects `first` and `second` are equal.
*/
function isEqual(first: GeneratedScheduleOutput, second: GeneratedScheduleOutput) {
if (first.totalCredits !== second.totalCredits || first.semester !== second.semester) {
return false;
}

const firstCourses = Array.from(first.schedule.keys());
const secondCourses = Array.from(second.schedule.keys());

if (firstCourses.length !== secondCourses.length) {
return false;
}

const sortedFirstCourses = firstCourses.sort((a, b) => a.code.localeCompare(b.code));
const sortedSecondCourses = secondCourses.sort((a, b) => a.code.localeCompare(b.code));

for (let i = 0; i < sortedFirstCourses.length; i += 1) {
const firstCourse = sortedFirstCourses[i];
const secondCourse = sortedSecondCourses[i];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!deepEqual(firstCourse, secondCourse)) {
return false;
}

const firstTimeslots = first.schedule.get(firstCourse) || [];
const secondTimeslots = second.schedule.get(secondCourse) || [];

for (let i = 0; i < 5; i += 1) {
if (firstTimeslots.length !== secondTimeslots.length) {
return false;
}

const firstTimeslotKeys = firstTimeslots
.map(ts => `${ts.start}-${ts.end}-${ts.daysOfTheWeek.sort().join(',')}`)
.sort();

const secondTimeslotKeys = secondTimeslots
.map(ts => `${ts.start}-${ts.end}-${ts.daysOfTheWeek.sort().join(',')}`)
.sort();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!deepEqual(firstTimeslotKeys, secondTimeslotKeys)) {
return false;
}
}

return true;
}

const startTime = Date.now();
while (outputs.length < 5 && Date.now() - startTime < 1500) {
const courses = this.courses.map(
course =>
new Course(
Expand Down Expand Up @@ -228,7 +286,9 @@ export default defineComponent({
// NOTE: ideally we want to move this to the algorithm itself.
// But also NOTE that this might not want to be hardcoded — consider e.g. liberal studies.

const momentary = ScheduleGenerator.generateSchedule(generatorRequest);
const momentary = ScheduleGenerator.generateSchedule(
generatorRequest
) as GeneratedScheduleOutput;

// Basic algorithm: now we want to clean up output.
// Make it so that for every element of output, we keep track of
Expand Down Expand Up @@ -311,17 +371,21 @@ export default defineComponent({
momentary.fulfilledRequirements = newFullfilledReqs;
momentary.totalCredits += deltaCredits;

output.push(momentary);
if (outputs.every(output => !isEqual(output, momentary))) {
outputs.push(momentary);
}
}

this.generatedScheduleOutputs = output;
this.generatedScheduleOutputs = outputs;
},
regenerateSchedule() {
this.generateSchedules();
this.currentPage = 1;
},
paginate(direction: number) {
if ((this.currentPage < 5 && direction === 1) || (this.currentPage > 1 && direction === -1)) {
if (
(this.currentPage < this.generatedScheduleOutputs.length && direction === 1) ||
(this.currentPage > 1 && direction === -1)
) {
this.currentPage += direction;
}
},
Expand Down Expand Up @@ -546,6 +610,7 @@ input {
margin-top: -0.5rem;
background-color: $white;
padding: 0rem 0.5rem 0rem 0.5rem;

&--font {
color: $black;
flex-direction: row;
Expand Down
Loading