In that case, you're asking the code to do quite a complex job and it
seems like you'd have to do quite a few sums to determine there wasn't a
fit; there's lots of algorithms and evaluation here. I'd suggest you
hire a professional programmer; this type of 'best fit' algorithm can
take a pro days to get working.
Now I've said that, perhaps someone on the list will prove me wrong.