Repository navigation
Feat/28 mini algo #48
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bb89f99
test
Wesley-Fos bc4b919
Merge branch 'main' of github.com:RIT-Software-Engineering/Open-Sourc…
Wesley-Fos 6f685fb
feat: starter code for the mini algo
ff47add
Merge branch 'main' of github.com:RIT-Software-Engineering/Open-Sourc…
Wesley-Fos 570dad2
Merge branch 'main' into feat/28-Mini-Algo
Wesley-Fos 305cff7
feat: moving things to appropriate folders
Wesley-Fos 38a2d5c
feat: adding X stickers to remove functionality
Wesley-Fos dd9e25f
feat: adding comments and fixing file type
91e5129
bug: removing test file
31b9053
feat: moving stuff
e0246da
feat: adding docs
ca6311f
docs(mini-algo): add comments explaining the fitTest algorithm
or8655 6055a73
feat: fixing sticker properties and updating tests
Wesley-Fos 8e251bb
docs: updating inline comments
Wesley-Fos a5e3dc8
docs: updating inline comments
Wesley-Fos 0abe80e
bug: fixing test that was missing implementation
Wesley-Fos 211b369
Merge pull request #51 from RIT-Software-Engineering/feat/28-mini-algo
Wesley-Fos 3f8df68
Feat/28 mini algo (#56)
Wesley-Fos 65edbd4
Feat/28 mini algo (#57)
Wesley-Fos c298bd8
Merge branch 'main' into feat/28-Mini-Algo
Wesley-Fos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /// <reference types="node" /> | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import { | ||
| fitTest, | ||
| STAGING_AREA_CAPACITY, | ||
| type Sticker, | ||
| type StickerSelection, | ||
| } from './mini-algo.ts'; | ||
|
|
||
| const baseSticker: Sticker = { | ||
| name: '', | ||
| height: 1, | ||
| width: 1, | ||
| description: '', | ||
| tags: [], | ||
| id: '', | ||
| categorySection: '', | ||
| hpglFilePath: '', | ||
| svgLocation: '', | ||
| }; | ||
|
|
||
| function selection(id: string, height: number, width = 1, quantity = 1): StickerSelection { | ||
| return { | ||
| id, | ||
| sticker: { ...baseSticker, id, height, width }, | ||
| quantity, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| // tests moved into their own file using github copilot (and turned into actual tests) | ||
| test('accepts a sticker that fits without mutating the staging area', () => { | ||
| const existing = selection('existing', 6, 7); | ||
| const stagingArea = { stickers: [existing] }; | ||
|
|
||
| assert.deepEqual(fitTest(stagingArea, selection('new', 1, 3)), { | ||
| fits: true, | ||
| stickersToRemove: [], | ||
| }); | ||
| assert.deepEqual(stagingArea.stickers, [existing]); | ||
| }); | ||
|
|
||
| test('suggests removing existing stickers when the area is full', () => { | ||
| const existing = selection('existing', 6, 7); | ||
| const stagingArea = { stickers: [existing] }; | ||
|
|
||
| assert.deepEqual(fitTest(stagingArea, selection('new', 2, 2)), { | ||
| fits: false, | ||
| stickersToRemove: [[existing]], | ||
| }); | ||
| assert.deepEqual(stagingArea.stickers, [existing]); | ||
| }); | ||
|
|
||
| test('rejects a missing sticker', () => { | ||
| assert.deepEqual(fitTest({ stickers: [] }, null), { | ||
| fits: false, | ||
| stickersToRemove: [], | ||
| }); | ||
| assert.deepEqual(fitTest({ stickers: [] }, undefined), { | ||
| fits: false, | ||
| stickersToRemove: [], | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects a sticker quantity that exceeds capacity', () => { | ||
| assert.deepEqual(fitTest({ stickers: [] }, selection('too-large', 5, 5, 2)), { | ||
| fits: false, | ||
| stickersToRemove: [], | ||
| }); | ||
| }); | ||
|
|
||
| test('suggests combinations of existing stickers when needed', () => { | ||
| const first = selection('first', 2, 5); | ||
| const second = selection('second', 4, 5); | ||
| const stagingArea = { stickers: [first, second] }; | ||
|
|
||
| assert.deepEqual(fitTest(stagingArea, selection('new', 6, 6)), { | ||
| fits: false, | ||
| stickersToRemove: [[first, second]], | ||
| }); | ||
| }); | ||
|
|
||
| test('prefers the smallest removal option and preserves staging order', () => { | ||
| const small = selection('small', 1, 2); | ||
| const large = selection('large', 4, 5); | ||
|
|
||
| assert.deepEqual(fitTest({ stickers: [small, large] }, selection('new', 5, 6)), { | ||
| fits: false, | ||
| stickersToRemove: [[large]], | ||
| }); | ||
| }); | ||
|
|
||
| test('returns alternate removal combinations in staging order', () => { | ||
| const first = selection('first', 4, 5); | ||
| const second = selection('second', 5, 4); | ||
| const third = selection('third', 2, 5); | ||
|
|
||
| assert.deepEqual( | ||
| fitTest({ stickers: [first, second, third] }, selection('new', 4, 5)), | ||
| { | ||
| fits: false, | ||
| stickersToRemove: [ | ||
| [first, second], | ||
| [first, third], | ||
| [second, third], | ||
| ], | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('only suggests removing up to MAX_STICKERS_PER_REMOVAL (3) stickers', () => { | ||
| const stickers = Array.from({ length: 5 }, (_, index) => | ||
| selection(`sticker-${index}`, 4, 5), | ||
| ); | ||
|
|
||
| const result = fitTest({ stickers }, selection('new', 1, 5)); | ||
|
|
||
| assert.equal(result.fits, false); | ||
| assert(result.stickersToRemove.length > 0); | ||
| assert(result.stickersToRemove.some((option) => option.length === 3)); | ||
| assert(result.stickersToRemove.every((option) => option.length <= 3)); | ||
| }); | ||
|
|
||
| test('caps removal suggestions at five options', () => { | ||
| const stickers = Array.from({ length: 5 }, (_, index) => | ||
| selection(`option-${index}`, 4, 5), | ||
| ); | ||
| const result = fitTest({ stickers }, selection('new', 1, 5)); | ||
|
|
||
| assert.equal(result.stickersToRemove.length, 5); | ||
| assert(result.stickersToRemove.every((option) => option.length <= 3)); | ||
| }); | ||
|
|
||
| test('rejects a sticker larger than the staging area capacity', () => { | ||
| assert.deepEqual( | ||
| fitTest( | ||
| { stickers: [selection('existing', 1, 3)] }, | ||
| selection('too-large', STAGING_AREA_CAPACITY + 1, 1), | ||
| ), | ||
| { fits: false, stickersToRemove: [] }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| export const STAGING_AREA_CAPACITY = 45; | ||
| const MAX_REMOVAL_OPTIONS = 5; | ||
| const MAX_STICKERS_PER_REMOVAL_OPTION = 3; | ||
|
|
||
| export interface Sticker { | ||
| name: string; | ||
| height: number; | ||
| width: number; | ||
| description: string; | ||
| tags: string[]; | ||
| id: string; | ||
| categorySection: string; | ||
| hpglFilePath: string; | ||
| svgLocation: string; | ||
| } | ||
|
|
||
| export interface StickerSelection { | ||
| id: string; | ||
| sticker: Sticker; | ||
| quantity: number; | ||
| } | ||
|
|
||
| export interface StagingArea { | ||
| accountId: string; | ||
| stickers: StickerSelection[]; | ||
| } | ||
|
|
||
| export interface FitTestResult { | ||
| fits: boolean; | ||
| stickersToRemove: StickerSelection[][]; | ||
| } | ||
|
|
||
| export const sticker: Sticker = { | ||
| name: '', | ||
| height: 3, | ||
| width: 3, | ||
| description: '', | ||
| tags: [], | ||
| id: '', | ||
| categorySection: '', | ||
| hpglFilePath: '', | ||
| svgLocation: '', | ||
| }; | ||
|
|
||
| export const sticker_selection: StickerSelection = { | ||
| id: '', | ||
| sticker, | ||
| quantity: 1, | ||
| }; | ||
|
|
||
| type RemovalCandidate = { | ||
| selection: StickerSelection; | ||
| index: number; | ||
| area: number; | ||
| }; | ||
|
|
||
|
|
||
| function collectOptions( | ||
| removalCandidates: RemovalCandidate[], | ||
| stickersToRemove: number, | ||
| overflowAmount: number, | ||
| ): StickerSelection[][] { | ||
| // Array for selected stickers to remove and the list of options to remove from the staging area | ||
| const options: StickerSelection[][] = []; | ||
| const selected: RemovalCandidate[] = []; | ||
|
|
||
| // Recursive function to find a list of options to remove from the stickers in the staging area. | ||
| function search(startIndex: number, freedArea: number): void { | ||
| // Stop the recursion once we have gathered enough removal options. | ||
| if (options.length >= MAX_REMOVAL_OPTIONS) { | ||
| return; | ||
| } | ||
|
|
||
| // If the current selected combination of stickers is equal to stickersToRemove, | ||
| // we examine if removing those stickers frees enough space | ||
| // to accommodate the overflowAmount. If so, we push that combination on to | ||
| // the list of options, sorted by their original staging area index. | ||
| if (selected.length === stickersToRemove) { | ||
| if (freedArea >= overflowAmount) { | ||
| options.push( | ||
| [...selected] | ||
| .sort((left, right) => left.index - right.index) | ||
| .map(({ selection }) => selection), | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Are there enough candidates left to finish this combination? | ||
| const remainingCount = stickersToRemove - selected.length; | ||
| if (removalCandidates.length - startIndex < remainingCount) { | ||
| return; | ||
| } | ||
|
|
||
| // Make sure that the amount of area that could be freed by the remaining stickers is enough to accommodate the overflowAmount | ||
| let maximumRemainingArea = 0; | ||
| for (let index = startIndex; index < startIndex + remainingCount; index++) { | ||
| maximumRemainingArea += removalCandidates[index].area; | ||
| } | ||
| if (freedArea + maximumRemainingArea < overflowAmount) { // if not end | ||
| return; | ||
| } | ||
|
|
||
| // Recursively call this function with a new starting index. | ||
| for ( | ||
| let index = startIndex; | ||
| index <= removalCandidates.length - remainingCount && | ||
| options.length < MAX_REMOVAL_OPTIONS; | ||
| index++ | ||
| ) { | ||
| selected.push(removalCandidates[index]); | ||
| search(index + 1, freedArea + removalCandidates[index].area); | ||
| selected.pop(); | ||
| } | ||
| } | ||
|
|
||
| // Start the recursive function to find options of stickers to remove and return results. | ||
| search(0, 0); | ||
| return options; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether a sticker selection can be added to the staging area. | ||
| * | ||
| * Calculates the total area used by the stickers in the staging area. | ||
| * Determines if adding the new sticker selection would exceed the staging area's capacity. | ||
| * If the new selection would exceed the capacity, it identifies combinations of existing selections that could be removed to make room. | ||
| * Returns a `FitTestResult` object containing whether the new selection fits and the suggested combinations to remove if it doesn't. | ||
| * STAGING_AREA_CAPACITY, MAX_REMOVAL_OPTIONS, MAX_STICKERS_PER_REMOVAL_OPTION define limits for the function's behavior. | ||
| * | ||
| * A result with `fits: false` and non-empty `stickersToRemove` means the new | ||
| * selection could fit if one of the suggested combinations were removed. | ||
| */ | ||
| export function fitTest( | ||
| stagingArea: Pick<StagingArea, 'stickers'>, | ||
| stickerToAdd: StickerSelection | null | undefined, | ||
| ): FitTestResult { | ||
| if (!stickerToAdd) { | ||
| return { fits: false, stickersToRemove: [] }; | ||
| } | ||
|
|
||
| // calculate the area of stickers in the staging area | ||
| const existingArea = stagingArea.stickers.reduce((totalArea, currentSticker) => { | ||
| return totalArea + currentSticker.sticker.height * currentSticker.sticker.width * currentSticker.quantity; | ||
| }, 0); | ||
|
|
||
| // calculate area of sticker(s) to add | ||
| const stickerArea = stickerToAdd.sticker.height * stickerToAdd.sticker.width * stickerToAdd.quantity; | ||
|
|
||
| // sticker cannot fit in area capacity? | ||
| if (stickerArea > STAGING_AREA_CAPACITY) { | ||
| return { fits: false, stickersToRemove: [] }; | ||
| } | ||
|
|
||
| const overflowAmount = existingArea + stickerArea - STAGING_AREA_CAPACITY; | ||
|
|
||
| if (overflowAmount > 0) { | ||
| // Create a list of stickers, their indexes, and areas in the staging area with their sizes | ||
| // sorted from greatest area to least area. | ||
| const candidates = stagingArea.stickers | ||
| .map((selection, index) => ({ | ||
| selection, | ||
| index, | ||
| area: selection.sticker.height * selection.sticker.width * selection.quantity, | ||
| })) | ||
| .sort((left, right) => right.area - left.area); | ||
|
|
||
| // For each stickersToRemove up to either the number of canidates or the max stickers per removal option | ||
| for ( | ||
| let stickersToRemove = 1; | ||
| stickersToRemove <= Math.min(candidates.length, MAX_STICKERS_PER_REMOVAL_OPTION); | ||
| stickersToRemove++ | ||
| ) { | ||
| const options = collectOptions(candidates, stickersToRemove, overflowAmount); | ||
| if (options.length > 0) { | ||
| return { fits: false, stickersToRemove: options }; | ||
| } | ||
| } | ||
|
|
||
| // No combination of stickers found. | ||
| return { fits: false, stickersToRemove: [] }; | ||
| } | ||
|
|
||
| return { fits: true, stickersToRemove: [] }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not an issue for this ticket, but this is a good reminder that we need to think about how to handle sellers wanting to post stickers that can't fit alone on a half sheet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point