I want to export my products to another marketing platform.
My products have different prices and types.
I can only export a maximum of 100 and I want to make sure I export 100 exactly if possible.
I have a set of criteria rules against which I select products, and I have a rough target for the amount of each I want to select (like a percentage of the 1,000) because I know at any one time that one type of product is more popular than others - there's no point marketing more winter clothing in summer. The targets don't add up to 100 because the admin would be horrendous when I make changes to a rule.
- RULE 1 - I want 30 yellow widgets between 10 and 20 dollars
- RULE 2 - I want 50 red doodahs between 30 and 40 dollars
- RULE 3 - I want 40 blue thingies between 100 and 200 dollars
I asked another question here recently about how to use ratios to calculate how to adjust these quantities so that I could select more or less products to reach my maximum. It works.
However, the problem I have is that perhaps there are only 20 yellow widgets between 10 and 20 dollars and I've told the system I want 30. This means I need to:
- work out the ratios
- the calc says I need to select 30
- if there are only 20 yellow widgets then I select all of them
- then I loop through the remaining rules calculate the ratios again and do the same check, ad nauseum
I have struggled with this for several days trying to make a system work, and to understand what's happening in my code when looping through. I wondered if there was a mathematical principle and method for this kind of problem.
At the moment this is what I do:
- allocation limit: 100 products
- I separate out the RULES where my target is greater than its available products into a list of VALID_RULES
- I sum the number of products matching these rules and subtract this from the max 100 to find the remaining allocation limit (say, there's 70 left of the 100)
- loop through the remaining rules where number of products is more than my chosen target
- sum all the targets, then work out the ratio for each one to find the number of products I should allocate (say, the first one says instead of 50 red doodahs I should select 55)
- check if the new allocation is greater than the products I have (say there are 52 red doodahs in my products database)
- if the new allocation is greater than the available products, I move the rule to VALID_RULES with with the max products as the target number otherwise I set it to the new_allocation
- I delete the rule from the RULES, move to the next rule and do all this again
But I am consistently getting more than my max of 100.