Algorithm for searching the combination with lowest price [duplicate] - algorithm

I have items with ID (1001, 1002, 1003, 1004, 1005, 1006). There respective quantities are (2, 5, 1, 1, 5, 2): Now I have data like following.There is an offerId for each row.
offerId :{[Item_Id, Item_quantity_on_which_offer_Applied, Discount per quantity]}
1 :{[1001, 2, 21]}
4 :{[1002, 5, 5]}
6 :{[1003, 1, 25] [1004, 1, 25]}
5 :{[1004, 1, 20]}
3 :{[1005, 5, 17.5] [1002, 5, 17.5]}
2 :{[1005, 2, 18.33] [1001, 2, 26] [1006, 2, 21.67]}
Explaination When offer Id 1 is applied, I get Item 2 quantities of Item Id 1002 at 21 rs. discount per quantity i.e. I am getting 21 rs. discount on 1 quantity of 1002.
Objective I want to get the best offer combination. For example in above case best offer combination will be:
OfferId : 2 (discount = 132 (i.e. (18.33+26+21.67)*2))
OfferId : 3 (note: for 3 quantities of item 1005 and 3 quantities of item 1002 since 2 quantities of item 1005 is already in offer Id 2). (discount = 105(i.e. (17.5+17.5)*3))
Now item 1002 has 2 quantity remaining , so:
offerId : 4 (applied on 2 quantities of item 1002)(discount = 10(i.e 5*2))
offerId : 6 (discount = (25+25)*1 = 50)
So in a nutshell, offerids 2, 3 , 4 , 6 will give me best combination of offers where offer 4 is applied on 2 quantities of item 1002,
offer 3 for 3 quantities of item 1005 and 3 quantities of item 1002.
Above is the result I desire to compute best offer combination depending on quantity.
So far, I had been able to find best offer combination without considering quantity. But now my requirement is to consider quantities of Items and then find best offer combination.
It would be really helpful if anyone can provide me with a pseudocode. Any suggestions are greatly appreciated.
P.S. I am writing my code in Golang but solutions in any language are welcomed.
I hope I framed my question correctly. Comment below if any more information regarding question is required.
Thanks in advance.

Even if there is only a single item of each type and every offer gives the same total discount (say, $1), this is NP-hard, since the NP-hard problem Set Packing can be reduced to it: for each set, make an offer for the same elements with total discount $1. Since all offers provide the same benefit, the optimal solution to this constructed instance of your problem is the one that uses the largest number of offers, and this solution corresponds directly to the optimal solution to the original Set Packing problem.
Thus there's no hope for a polynomial-time solution to your problem.

Related

Adding numbers with different priorities into a total value that does not exceed the allowable

Suppose we have an array of numbers, each number has its own priority and price, the price is the value of the number, how to calculate the sum of a set of these numbers in decreasing order of priority so that the sum does not exceed the allowable one, please tell me at least the name of the algorithm with which it is can be done. Example: there are numbers 2, 3, 9 with priorities 3, 1, 2, respectively. The constraint is 4, therefore the number 9 is cut off immediately, since 9> 4, 2 and 3 we cannot add together, since 5> 3, therefore the choice of 2 numbers is 2 and 3, but since the number 2 has a higher priority, we add only his, this algorithm should work with any number of numbers.
It seems that you are looking for a greedy algorithm:
Order By priority
Scan this ordered collection from the beginning while
Adding item if total meets the constrait(s), skipping if constraint is broken.
In your case:
2, 3, 9 with priorities 3, 1, 2 and a constraint total <= 4
After ordering we have
2, 9, 3
then we scan:
2 take (total == 2 meets the constraint)
9 skip (total == 2 + 9 == 11 > 4 doesn't meet the constraint)
3 skip (total == 2 + 3 == 5 > 4 doesn't meet the constraint)
So far we should take the only 2 item.
Edit: you've dropped 9 since 9 > 4 and that's why 9 can't be in the solution. This process (when we drop items or, on the contrary, take items which are guaranteed to be in the solution) is called Kernelization
In general case when you can skip high priority item in order to take, say, ten low priority items you have Knapsack problem

Algorithms for optimal student seating arrangements

Say I need to place n=30 students into groups of between 2 and 6, and I collect the following preference data from each student:
Student Name: Tom
Likes to sit with: Jimi, Eric
Doesn't like to sit with: John, Paul, Ringo, George
It's implied that they're neutral about any other student in the overall class that they haven't mentioned.
How might I best run a large number of simulations of many different/random grouping arrangements, to be able to determine a score for each arrangement, through which I could then pick the "most optimal" score/arrangement?
Alternatively, are there any other methods by which I might be able to calculate a solution that satisfies all of the supplied constraints?
I'd like a generic method that can be reused on different class sizes each year, but within each simulation run, the following constants and variables apply:
Constants: Total number of students, Student preferences
Variables: Group sizes, Student Groupings, Number of different group arrangements/iterations to test
Thanks in advance for any help/advice/pointers provided.
I believe you can state this as an explicit mathematical optimization problem.
Define the binary decision variables:
x(p,g) = 1 if person p is assigned to group g
0 otherwise
I used:
I used your data set with 28 persons, and your preference matrix (with -1,+1,0 elements). For groups, I used 4 groups of 6 and 1 group of 4. A solution can look like:
---- 80 PARAMETER solution using MIQP model
group1 group2 group3 group4 group5
aimee 1
amber-la 1
amber-le 1
andrina 1
catelyn-t 1
charlie 1
charlotte 1
cory 1
daniel 1
ellie 1
ellis 1
eve 1
grace-c 1
grace-g 1
holly 1
jack 1
jade 1
james 1
kadie 1
kieran 1
kristiana 1
lily 1
luke 1
naz 1
nibah 1
niko 1
wiki 1
zeina 1
COUNT 6 6 6 6 4
Notes:
This model can be linearized, so it can be fed into a standard MIP solver
I solved this directly as a MIQP model (actually the solver reformulated the model into a MIP). The model solved in a few seconds.
Probably we need to add extra logic to make sure one person is not getting a really bad assignment. We optimize here only the total sum. This overall sum may allow an individual to get a bad deal. It is an interesting exercise to take this into account in the model. There are some interesting trade-offs.
1st approach should be, create matrix n x n where n is total number of students, indexes for row and columns are ordinals for every student, and each column representing preferences for sitting with the others students. Fills the cells with values 1=Like to sit, -1 = the Opposite, 0 = neutral. Zeroes to be filled too on main diagonal (i,i)
------Mark Maria John Peter
Mark 0 1 -1 1
Maria 0 0 -1 1
John -1 1 0 1
Peter 0
Score calculations are based on sums of these values. So ie: John likes to sit with Maria, = 1, but Maria doesn't like to sit with John -1, result is 0. Best result is when both score (sum) 2.
So on, based on Group Sizes, calculate Score of each posible combination. Bigger the score, better the arrangement. Combinations discriminate values on main diagonal. ie: John grouped with the same John is not a valid combination/group.
In a group size of 2, best score is 2
In a group size of 3, best score is 6,
In a group size of 4, best score is 12
In a group size of n, best score would be (n-1)*n
Now in ordered list of combinations / groups, you should take first the best tuples with highest scores, but avoiding duplicates of students between tuples.
In a recent research, a PSO was implemented to classify students under unknown number of groups of 4 to 6. PSO showed improved capabilities compared to GA. I think that all you need is the specific research.
The paper is: Forming automatic groups of learners using particle swarm optimization for applications of differentiated instruction
You can find the paper here: https://doi.org/10.1002/cae.22191
Perhaps the researchers could guide you through researchgate: https://www.researchgate.net/publication/338078753
Regarding the optimal sitting you need to specify an objective function with the specific data

Algorithm: Fill different baskets

Let's assume I have 3 different baskets with a fixed capacity
And n-products which provide different value for each basket -- you can only pick whole products
Each product should be limited to a max amount (i.e. you can maximal pick product A 5 times)
Every product adds at least 0 or more value to all baskets and come in all kinds of variations
Now I want a list with all possible combinations of products fitting in the baskets ordered by accuracy (like basket 1 is 5% more full would be 5% less accurate)
Edit: Example
Basket A capacity 100
Basket B capacity 80
Basket C capacity 30
fake products
Product 1 (A: 5, B: 10, C: 1)
Product 2 (A: 20 B: 0, C: 0)
There might be hundreds more products
Best fit with max 5 each would be
5 times Product 1
4 times Product 2
Result
A: 105
B: 50
C: 5
Accuracy: (qty_used / max_qty) * 100 = (160 / 210) * 100 = 76.190%
Next would be another combination with less accuracy
Any pointing in the right direction is highly appreciated Thanks
Edit:
instead of above method, accuracy should be as error and the list should be in ascending order of error.
Error(Basket x) = (|max_qty(x) - qty_used(x)| / max_qty(x)) * 100
and the overall error should be the weighted average of the errors of all baskets.
Total Error = [Σ (Error(x) * max_qty(x))] / [Σ (max_qty(x))]

Algorithm to find best offer combination that gives maximum discount on a given set Of Items

I have items with ID (1001, 1002, 1003, 1004, 1005, 1006). There respective quantities are (2, 5, 1, 1, 5, 2): Now I have data like following.There is an offerId for each row.
offerId :{[Item_Id, Item_quantity_on_which_offer_Applied, Discount per quantity]}
1 :{[1001, 2, 21]}
4 :{[1002, 5, 5]}
6 :{[1003, 1, 25] [1004, 1, 25]}
5 :{[1004, 1, 20]}
3 :{[1005, 5, 17.5] [1002, 5, 17.5]}
2 :{[1005, 2, 18.33] [1001, 2, 26] [1006, 2, 21.67]}
Explaination When offer Id 1 is applied, I get Item 2 quantities of Item Id 1002 at 21 rs. discount per quantity i.e. I am getting 21 rs. discount on 1 quantity of 1002.
Objective I want to get the best offer combination. For example in above case best offer combination will be:
OfferId : 2 (discount = 132 (i.e. (18.33+26+21.67)*2))
OfferId : 3 (note: for 3 quantities of item 1005 and 3 quantities of item 1002 since 2 quantities of item 1005 is already in offer Id 2). (discount = 105(i.e. (17.5+17.5)*3))
Now item 1002 has 2 quantity remaining , so:
offerId : 4 (applied on 2 quantities of item 1002)(discount = 10(i.e 5*2))
offerId : 6 (discount = (25+25)*1 = 50)
So in a nutshell, offerids 2, 3 , 4 , 6 will give me best combination of offers where offer 4 is applied on 2 quantities of item 1002,
offer 3 for 3 quantities of item 1005 and 3 quantities of item 1002.
Above is the result I desire to compute best offer combination depending on quantity.
So far, I had been able to find best offer combination without considering quantity. But now my requirement is to consider quantities of Items and then find best offer combination.
It would be really helpful if anyone can provide me with a pseudocode. Any suggestions are greatly appreciated.
P.S. I am writing my code in Golang but solutions in any language are welcomed.
I hope I framed my question correctly. Comment below if any more information regarding question is required.
Thanks in advance.
Even if there is only a single item of each type and every offer gives the same total discount (say, $1), this is NP-hard, since the NP-hard problem Set Packing can be reduced to it: for each set, make an offer for the same elements with total discount $1. Since all offers provide the same benefit, the optimal solution to this constructed instance of your problem is the one that uses the largest number of offers, and this solution corresponds directly to the optimal solution to the original Set Packing problem.
Thus there's no hope for a polynomial-time solution to your problem.

Ad distribution problem: an optimal solution?

I'm asked to find a 2 approximate solution to this problem:
You’re consulting for an e-commerce site that receives a large number
of visitors each day. For each visitor i, where i € {1, 2 ..... n}, the site
has assigned a value v[i], representing the expected revenue that can be
obtained from this customer.
Each visitor i is shown one of m possible ads A1, A2 ..... Am as they
enter the site. The site wants a selection of one ad for each customer so
that each ad is seen, overall, by a set of customers of reasonably large
total weight.
Thus, given a selection of one ad for each customer, we will
define the spread of this selection to be the minimum, over j = 1, 2 ..... m,
of the total weight of all customers who were shown ad Aj.
Example: Suppose there are six customers with values 3, 4, 12, 2, 4, 6, and
there are m = 3 ads. Then, in this instance, one could achieve a spread of
9 by showing ad A1 to customers 1, 2, 4, ad A2 to customer 3, and ad A3 to
customers 5 and 6.
The ultimate goal is to find a selection of an ad for each customer
that maximizes the spread.
Unfortunately, this optimization problem
is NP-hard (you don’t have to prove this).
So instead give a polynomial-time algorithm that approximates the maximum spread within a factor of 2.
The solution I found is the following:
Order visitors values in descending order
Add the next visitor value (i.e. assign the visitor) to
the Ad with the current lowest total value
Repeat
This solution actually seems to always find the optimal solution, or I simply can't find a counterexample.
Can you find it? Is this a non-polinomial solution and I just can't see it?
With:
v = [7, 6, 5, 3, 3]
m = 2
The optimal solution is:
A1: 6 + 3 + 3 = 12
A2: 5 + 7 = 12
Your solution gives:
A1: 7 + 3 + 3 = 13
A2: 6 + 5 = 11

Resources