diff --git a/src/Class/Groups.ts b/src/Class/Groups.ts index 36dde07..19f652f 100644 --- a/src/Class/Groups.ts +++ b/src/Class/Groups.ts @@ -8,14 +8,14 @@ class Groups { add(path: string[], studentId: string): void { const existingGroup = this.groups.find((group) => - arraysHaveSameValues(group.path, path) + arraysHaveSameValues(group.requiredEvents, path) ); if (existingGroup) { existingGroup.studentIds.push(studentId); } else { this.groups.push({ - path, + requiredEvents: path, studentIds: [studentId], _id: this.groups.length + 1, }); @@ -28,7 +28,7 @@ class Groups { get(path: string[]): Group { return this.groups.find((group) => - arraysHaveSameValues(group.path, path) + arraysHaveSameValues(group.requiredEvents, path) ); } } diff --git a/src/alg/TimeDistribution.ts b/src/alg/TimeDistribution.ts index 0e1d92b..1374b45 100644 --- a/src/alg/TimeDistribution.ts +++ b/src/alg/TimeDistribution.ts @@ -25,7 +25,7 @@ function buildGroupsByPaths( function createPQ(groups: Group[]): PriorityQueue { const pq = new PriorityQueue(); groups.forEach((group) => { - pq.enqueue(group, group.path.length); + pq.enqueue(group, group.requiredEvents.length); }); return pq; } diff --git a/src/alg/TimeDistribution/AllocateGroupsToItems.ts b/src/alg/TimeDistribution/AllocateGroupsToItems.ts index fd223cc..f7df41f 100644 --- a/src/alg/TimeDistribution/AllocateGroupsToItems.ts +++ b/src/alg/TimeDistribution/AllocateGroupsToItems.ts @@ -22,8 +22,8 @@ function allocateGroupsToItems( ); path_config.path.forEach((eventId) => { - if (items.some((item) => item._id === eventId)) { - const item = items.find((item) => item._id === eventId); + if (items.some((item) => item === eventId)) { + const item = items.find((item) => item === eventId); if (item) { item.studentIds.push(...ids); } diff --git a/src/alg/TimeDistribution/DistributeGroups.ts b/src/alg/TimeDistribution/DistributeGroups.ts index 7d0d2ab..1101a13 100644 --- a/src/alg/TimeDistribution/DistributeGroups.ts +++ b/src/alg/TimeDistribution/DistributeGroups.ts @@ -14,8 +14,8 @@ function createRecordOfCurrentUsedCapacity( const record: Record = {}; paths.forEach((path) => { path.path.forEach((pathItem) => { - record[pathItem] = - (record[pathItem] || 0) + + record[pathItem._id] = + (record[pathItem._id] || 0) + path.valueForTestingStudentDistribution; }); }); @@ -34,7 +34,7 @@ function distributeStudentsToPaths( paths.forEach((path) => { if (path.groupId === group._id && amountStudentsRemaining > 0) { const min = Math.min( - getMaxAvailableCapacity(path.path, items) - + getMaxAvailableCapacity(path.path) - path.valueForTestingStudentDistribution, amountStudentsRemaining ); @@ -68,7 +68,7 @@ function checkForExceedingGroupCapacities( items.forEach((item) => { if (record[item._id] > item.groupCapazity) { redistribute( - item._id, + item, record[item._id] - item.groupCapazity, items, paths @@ -77,7 +77,7 @@ function checkForExceedingGroupCapacities( }); } function redistribute( - failedId: string, + failedId: Item, excessStudents: number, items: Item[], paths: Path_config[] diff --git a/src/alg/TimeDistribution/FindPaths.ts b/src/alg/TimeDistribution/FindPaths.ts index 3c7c5c3..70331f5 100644 --- a/src/alg/TimeDistribution/FindPaths.ts +++ b/src/alg/TimeDistribution/FindPaths.ts @@ -15,9 +15,17 @@ function findPathsForTheGroups( const requiredIds = new Set(getDefaultIds(project)); const entries = g.getNodesWithoutIngoingEdges(); groups.forEach((group) => { - const ids = new Set([...requiredIds, ...group.path]); + const ids = new Set([...requiredIds, ...group.requiredEvents]); entries.forEach((entry: GraphNode) => { - dfs(entry, ids, [], group.path, group._id, items, path_configs); + dfs( + entry, + ids, + [], + group.requiredEvents, + group._id, + items, + path_configs + ); }); }); @@ -26,7 +34,7 @@ function findPathsForTheGroups( function dfs( node: GraphNode, remainingIds: Set, - path: string[], + path: Item[], extraIds: string[], groupId: number, items: Item[], @@ -38,7 +46,7 @@ function dfs( return; } - const newPath = [...path, node.value._id]; + const newPath = [...path, node.value]; remainingIds.delete(node.value.eventId); if (remainingIds.size === 0) { @@ -63,11 +71,7 @@ function dfs( remainingIds.add(node.value.eventId); } -function getMaxAvailableCapacity(path: string[], items: Item[]): number { - return Math.min( - ...items - .filter((item) => path.includes(item._id)) - .map((item) => item.groupCapazity) - ); +function getMaxAvailableCapacity(path: Item[]): number { + return Math.min(...path.map((item) => item.groupCapazity)); } export { findPathsForTheGroups, getMaxAvailableCapacity }; diff --git a/src/types/Group.ts b/src/types/Group.ts index a01c07b..5b5f65a 100644 --- a/src/types/Group.ts +++ b/src/types/Group.ts @@ -1,5 +1,7 @@ +import Item from './Item'; + export interface Group { _id: number; - path: string[]; + requiredEvents: string[]; studentIds: string[]; } diff --git a/src/types/Path_config.ts b/src/types/Path_config.ts index 77848ad..d039aa5 100644 --- a/src/types/Path_config.ts +++ b/src/types/Path_config.ts @@ -1,5 +1,7 @@ +import Item from './Item'; + export interface Path_config { groupId: number; - path: string[]; + path: Item[]; valueForTestingStudentDistribution: number; } diff --git a/test/Groups.test.ts b/test/Groups.test.ts index 4e6660e..544c6ac 100644 --- a/test/Groups.test.ts +++ b/test/Groups.test.ts @@ -13,7 +13,11 @@ describe('Groups class', () => { groups.add(path, studentId); - const expectedGroup: Group = { path, studentIds: [studentId], _id: 1 }; + const expectedGroup: Group = { + requiredEvents: path, + studentIds: [studentId], + _id: 1, + }; expect(groups.getAll()).toEqual([expectedGroup]); }); @@ -28,8 +32,8 @@ describe('Groups class', () => { groups.add(path2, studentId2); const expectedGroups: Group[] = [ - { path: path1, studentIds: [studentId1], _id: 1 }, - { path: path2, studentIds: [studentId2], _id: 2 }, + { requiredEvents: path1, studentIds: [studentId1], _id: 1 }, + { requiredEvents: path2, studentIds: [studentId2], _id: 2 }, ]; expect(groups.getAll()).toEqual(expectedGroups); }); @@ -42,7 +46,11 @@ describe('Groups class', () => { const retrievedGroup = groups.get(path); - const expectedGroup: Group = { path, studentIds: [studentId], _id: 1 }; + const expectedGroup: Group = { + requiredEvents: path, + studentIds: [studentId], + _id: 1, + }; expect(retrievedGroup).toEqual(expectedGroup); });