Merge pull request #1617 from siiky/fix/lam-990/refactor-coin-change

LAM-990 refactor: coin change
This commit is contained in:
Rafael Taranto 2023-11-22 14:46:56 +00:00 committed by GitHub
commit 1b30f43747
2 changed files with 146 additions and 13 deletions

View file

@ -1,6 +1,9 @@
const _ = require('lodash/fp')
const sumService = require('@haensl/subset-sum')
const logger = require('./logger')
const cc = require('./coin-change')
const BILL_LIST_MODES = {
LAST_UNIT_FIRST: 0,
FIRST_UNIT_FIRST: 1,
@ -103,7 +106,7 @@ const buildBillList = (units, mode) => {
}
}
const getSolution = (units, amount, mode) => {
const getSolution_old = (units, amount, mode) => {
const billList = buildBillList(units, mode)
if (_.sum(billList) < amount.toNumber()) {
@ -122,24 +125,46 @@ const getSolution = (units, amount, mode) => {
)
}
const getSolution = (units, amount) => {
amount = amount.toNumber()
units = units.map(({ denomination, count }) => [denomination, count])
const model = cc.model(units)
return cc.solve(model, amount)
}
const solutionToOriginalUnits = (solution, units) => {
const billsLeft = _.clone(_.fromPairs(_.map(it => [it.denomination, it.provisioned])(solution)))
return _.reduce(
(acc, value) => {
const unit = units[value]
const billsToAssign = _.clamp(0, unit.count)(_.isNaN(billsLeft[unit.denomination]) || _.isNil(billsLeft[unit.denomination]) ? 0 : billsLeft[unit.denomination])
acc.push({ name: unit.name, denomination: unit.denomination, provisioned: billsToAssign })
billsLeft[unit.denomination] -= billsToAssign
return acc
const billsToAssign = (count, left) => _.clamp(0, count)(_.isNaN(left) || _.isNil(left) ? 0 : left)
const billsLeft = _.flow(
_.map(([denomination, provisioned]) => [BN(denomination), provisioned]),
_.fromPairs,
)(solution)
return _.map(
({ count, name, denomination }) => {
const provisioned = billsToAssign(count, billsLeft[denomination])
billsLeft[denomination] -= provisioned
return { name, denomination, provisioned }
},
[],
_.range(0, _.size(units))
units
)
}
function makeChange(outCassettes, amount) {
const solution = getSolution(outCassettes, amount, BILL_LIST_MODES.VALUE_ROUND_ROBIN)
return solutionToOriginalUnits(solution, outCassettes)
const ss_solution = getSolution_old(outCassettes, amount, BILL_LIST_MODES.VALUE_ROUND_ROBIN)
const cc_solution = cc.solve(cc.model(outCassettes), amount.toNumber())
if (!!ss_solution === !!cc_solution) {
logger.error(new Error(`subset-sum and coin-change don't agree on solvability -- subset-sum:${!!ss_solution} coin-change:${!!cc_solution}`))
return solutionToOriginalUnits(ss_solution, outCassettes)
}
if (!cc.check(cc_solution, amount.toNumber())) {
logger.error(new Error("coin-change provided a bad solution"))
return solutionToOriginalUnits(ss_solution, outCassettes)
}
return solutionToOriginalUnits(cc_solution, outCassettes)
}
module.exports = { makeChange }

108
lib/coin-change.js Normal file
View file

@ -0,0 +1,108 @@
/*
* Greedy solver of the coin change problem, based on the following CHICKEN
* implementation: https://git.sr.ht/~siiky/coin-change
*/
/*
* prepare_denominations([[d0, count], [d1, count], ...])
* => [{ denom, count, csum }, ...]
*/
const prepare_denominations = denominations =>
JSON.parse(JSON.stringify(denominations))
.sort(([d1, c1], [d2, c2]) => d1 < d2)
.reduce(
([csum, denoms], [denom, count]) => {
csum += denom*count
return [
csum,
[{ denom, count, csum }].concat(denoms)
]
},
[0, []]
)[1] /* ([csum, denoms]) => denoms */
const max_denomination_multiplicity = (denom, count, target) =>
Math.min(count, Math.floor(target / denom))
const has_divisor = (didx, denominations, target) =>
denominations
.slice(didx)
.some(({ denom }) => (target % denom) === 0)
/*
* @returns null if there's no solution set;
* false if there's no solution;
* solution if there's a solution
*/
const memo_get = (memo, target, denom) => {
const denom_solutions = memo[target]
if (denom_solutions === undefined) return null
const solution = denom_solutions[denom]
return solution === undefined ? null : solution
}
const memo_set = (memo, target, denom, solution) => {
let denom_solutions = memo[target]
if (denom_solutions === undefined)
memo[target] = denom_solutions = {}
return denom_solutions[denom] = solution
}
const check = (solution, target) =>
!solution
|| target === solution.reduce((sum, [denom, provisioned]) => sum + denom*provisioned, 0)
const model = denominations => ({
denominations: prepare_denominations(denominations),
memo: {}
})
/*
* target :: Int
* denominations :: [[d0, count], [d1, count], ...]
*
* @returns [[d0, provisioned], [d1, provisioned], ... ];
* false if there's no solution.
*/
const solve = (model, target) => {
const { denominations, memo } = model
const coin_change = (didx, target) => {
if (target === 0) return []
for (; didx < denominations.length; didx++) {
const { denom, count, csum } = denominations[didx]
/*
* There's no solution if the target is greater than the cumulative sum
* of the denominations, or if the target is not divisible by any of the
* denominations
*/
if (target > csum || !has_divisor(didx, denominations, target))
return memo_set(memo, target, denom, false)
let solution = memo_get(memo, target, denom)
if (solution === false) continue /* not here, keep looking */
if (solution) return solution /* we've previously computed a solution */
/* solution === null */
for (let nd = max_denomination_multiplicity(denom, count, target); nd >= 0; nd--) {
solution = coin_change(didx+1, target - denom*nd)
if (solution)
return memo_set(memo, target, denom, [[denom, nd]].concat(solution))
}
memo_set(memo, target, denom, false)
}
return false
}
return coin_change(0, target)
}
module.exports = {
check,
model,
solve,
}