Skip to content

Commit

Permalink
path => requiredEvents, string[] => Item[]
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-sth committed Oct 26, 2023
1 parent e0bb4b5 commit 1f9748f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Class/Groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -28,7 +28,7 @@ class Groups {

get(path: string[]): Group {
return this.groups.find((group) =>
arraysHaveSameValues(group.path, path)
arraysHaveSameValues(group.requiredEvents, path)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/alg/TimeDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function buildGroupsByPaths(
function createPQ(groups: Group[]): PriorityQueue<Group> {
const pq = new PriorityQueue<Group>();
groups.forEach((group) => {
pq.enqueue(group, group.path.length);
pq.enqueue(group, group.requiredEvents.length);
});
return pq;
}
Expand Down
4 changes: 2 additions & 2 deletions src/alg/TimeDistribution/AllocateGroupsToItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/alg/TimeDistribution/DistributeGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function createRecordOfCurrentUsedCapacity(
const record: Record<string, number> = {};
paths.forEach((path) => {
path.path.forEach((pathItem) => {
record[pathItem] =
(record[pathItem] || 0) +
record[pathItem._id] =
(record[pathItem._id] || 0) +
path.valueForTestingStudentDistribution;
});
});
Expand All @@ -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
);
Expand Down Expand Up @@ -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
Expand All @@ -77,7 +77,7 @@ function checkForExceedingGroupCapacities(
});
}
function redistribute(
failedId: string,
failedId: Item,
excessStudents: number,
items: Item[],
paths: Path_config[]
Expand Down
24 changes: 14 additions & 10 deletions src/alg/TimeDistribution/FindPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ function findPathsForTheGroups(
const requiredIds = new Set<string>(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<Item>) => {
dfs(entry, ids, [], group.path, group._id, items, path_configs);
dfs(
entry,
ids,
[],
group.requiredEvents,
group._id,
items,
path_configs
);
});
});

Expand All @@ -26,7 +34,7 @@ function findPathsForTheGroups(
function dfs(
node: GraphNode<Item>,
remainingIds: Set<string>,
path: string[],
path: Item[],
extraIds: string[],
groupId: number,
items: Item[],
Expand All @@ -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) {
Expand All @@ -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 };
4 changes: 3 additions & 1 deletion src/types/Group.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Item from './Item';

export interface Group {
_id: number;
path: string[];
requiredEvents: string[];
studentIds: string[];
}
4 changes: 3 additions & 1 deletion src/types/Path_config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Item from './Item';

export interface Path_config {
groupId: number;
path: string[];
path: Item[];
valueForTestingStudentDistribution: number;
}
16 changes: 12 additions & 4 deletions test/Groups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});

Expand All @@ -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);
});
Expand All @@ -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);
});

Expand Down

0 comments on commit 1f9748f

Please sign in to comment.