I'm in need of expert advice on a tricky matter.
The scenario is:
A product is identified by a unique ProductID and has a sales price. Very classic scenario. The product can also be in one or more discounts.
A discount can be of different types. One example of a discount is:
A line item can only get one discount thus once a line item has been discounted it is not available for other discounts.
Discount-A: Buy two or more and get 20 % off the any of the following products
Discount-B: Buy product and get 50 % off the following product
Basket: containing line items with:
Calculation #1:
Calculation #2:
Which means that a combination of Discount-A and Discount-B will give the best possible discount for the customer.
Basket: containing line items with:
Calculation #1:
Calculation #2:
Which means that applying Discount-A will give the best possible discount for the customer.
In order to calculate the best discount for a given basket, literally all combinations of products and available discounts on these products must be evaluated.
Normally there is 30-40 line items in a basket each with 0-3 discounts each.
Basically I'm stuck with finding an efficient way to do this calculation.
Right now the algorithm I have for applying the discounts is something like:
But this is not at all sufficient as it does not try out different combinations of line items / discounts.
I've been searching around for standardized algorithms that can solve problems like this, but without any luck so far.
Hope to hear from you :)
The formula used to calculate the rate of discount is (discount ÷ list price) × 100. In the formula, the discount is the difference between the marked price and the selling price. Another formula that can be used for calculating discount percentage is [(List price - Selling price)/List price] × 100.
First subtract each discount from 100 percent to find its complement. Then multiply the complements to find the percent you actually pay, which is the net-price rate. The complement method can also be used to find the dollar amount of the discount.
Assuming that:
Then the problem becomes one that is called an assignment problem and can be optimally solved in O(n^3) using the Hungarian algorithm.
You will need to compute a matrix M[a,b] containing the money saved if using discount a on product b. (If a discount does not apply, then set the money saved to 0.)
The Hungarian algorithm will compute the way of assigning discounts to products that saves the most money.
If you don't have the same number of discounts and products, then add dummy discounts (with zero savings) or dummy products (again with zero savings) until the number of discounts matches the number of products.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With