Algorithm to group items according to simple rules

Multi tool use
up vote
0
down vote
favorite
I'd like some help to find the good algorithm for a problem but I can't even figure out how to abstract this problem so I'll write this question as en example of what I want.
Say I have a list of items of 2 types (A and B) which all have a value. The list looks like this :
- A1 : 10
- B1 : 11
- A2 : 9
- B2 : 6
- B3 : 4
My problem is that I want to make as many groups of items as possible for the following rules :
- there must be at least 1 type A item and 1 type B item
- the sum of the values of each group must be at least 20
The output of the algorithm should be :
- A1 + B2 + B3
- A2 + B1
Parameters of the problem may vary :
- this list itself of course (number of items, values, ...)
- the number of item types (from 1 to a lot)
- the number of items required per type (I may want at least 2 items from A and 3 items from B)
- the minimum required value sum
I'm almost sure such algorithm exist, it makes me think of the Knapsack problem but I actually don't even know what to search.
algorithm
|
show 2 more comments
up vote
0
down vote
favorite
I'd like some help to find the good algorithm for a problem but I can't even figure out how to abstract this problem so I'll write this question as en example of what I want.
Say I have a list of items of 2 types (A and B) which all have a value. The list looks like this :
- A1 : 10
- B1 : 11
- A2 : 9
- B2 : 6
- B3 : 4
My problem is that I want to make as many groups of items as possible for the following rules :
- there must be at least 1 type A item and 1 type B item
- the sum of the values of each group must be at least 20
The output of the algorithm should be :
- A1 + B2 + B3
- A2 + B1
Parameters of the problem may vary :
- this list itself of course (number of items, values, ...)
- the number of item types (from 1 to a lot)
- the number of items required per type (I may want at least 2 items from A and 3 items from B)
- the minimum required value sum
I'm almost sure such algorithm exist, it makes me think of the Knapsack problem but I actually don't even know what to search.
algorithm
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'd like some help to find the good algorithm for a problem but I can't even figure out how to abstract this problem so I'll write this question as en example of what I want.
Say I have a list of items of 2 types (A and B) which all have a value. The list looks like this :
- A1 : 10
- B1 : 11
- A2 : 9
- B2 : 6
- B3 : 4
My problem is that I want to make as many groups of items as possible for the following rules :
- there must be at least 1 type A item and 1 type B item
- the sum of the values of each group must be at least 20
The output of the algorithm should be :
- A1 + B2 + B3
- A2 + B1
Parameters of the problem may vary :
- this list itself of course (number of items, values, ...)
- the number of item types (from 1 to a lot)
- the number of items required per type (I may want at least 2 items from A and 3 items from B)
- the minimum required value sum
I'm almost sure such algorithm exist, it makes me think of the Knapsack problem but I actually don't even know what to search.
algorithm
I'd like some help to find the good algorithm for a problem but I can't even figure out how to abstract this problem so I'll write this question as en example of what I want.
Say I have a list of items of 2 types (A and B) which all have a value. The list looks like this :
- A1 : 10
- B1 : 11
- A2 : 9
- B2 : 6
- B3 : 4
My problem is that I want to make as many groups of items as possible for the following rules :
- there must be at least 1 type A item and 1 type B item
- the sum of the values of each group must be at least 20
The output of the algorithm should be :
- A1 + B2 + B3
- A2 + B1
Parameters of the problem may vary :
- this list itself of course (number of items, values, ...)
- the number of item types (from 1 to a lot)
- the number of items required per type (I may want at least 2 items from A and 3 items from B)
- the minimum required value sum
I'm almost sure such algorithm exist, it makes me think of the Knapsack problem but I actually don't even know what to search.
algorithm
algorithm
asked yesterday
xxxo
12315
12315
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday
|
show 2 more comments
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
It can easily be solved by a mathematical constraint solver:
Let's use following terms:
- @A = number of items with category A
- @B = number of items with category B
- n = min(@A, @B)
- G_j = sum for i = 0 to @A of ( α_j_i * A_i) + sum for i=0 to @B of ( β_j_i * B_i )
Then the following equations must hold in order to have a valid solution:
- G_j is either 0 OR G_j is larger than 20
- min(α_j_0 ... α_j_@A ... β_j_0 ... β_j_@B) is either 0 or 1
- sum(α_0_0 ... α_0_n) is 1 (each A is used once)
- sum(β_0_0 ... β_0_n) is 1 (each B is used once)
And then we simply maximize
T = sum for i = 0 to n of 2 ^ G_i - 1
Which will add a 0 for each empty group, and a larger value for each non-empty group.
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It can easily be solved by a mathematical constraint solver:
Let's use following terms:
- @A = number of items with category A
- @B = number of items with category B
- n = min(@A, @B)
- G_j = sum for i = 0 to @A of ( α_j_i * A_i) + sum for i=0 to @B of ( β_j_i * B_i )
Then the following equations must hold in order to have a valid solution:
- G_j is either 0 OR G_j is larger than 20
- min(α_j_0 ... α_j_@A ... β_j_0 ... β_j_@B) is either 0 or 1
- sum(α_0_0 ... α_0_n) is 1 (each A is used once)
- sum(β_0_0 ... β_0_n) is 1 (each B is used once)
And then we simply maximize
T = sum for i = 0 to n of 2 ^ G_i - 1
Which will add a 0 for each empty group, and a larger value for each non-empty group.
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
add a comment |
up vote
0
down vote
It can easily be solved by a mathematical constraint solver:
Let's use following terms:
- @A = number of items with category A
- @B = number of items with category B
- n = min(@A, @B)
- G_j = sum for i = 0 to @A of ( α_j_i * A_i) + sum for i=0 to @B of ( β_j_i * B_i )
Then the following equations must hold in order to have a valid solution:
- G_j is either 0 OR G_j is larger than 20
- min(α_j_0 ... α_j_@A ... β_j_0 ... β_j_@B) is either 0 or 1
- sum(α_0_0 ... α_0_n) is 1 (each A is used once)
- sum(β_0_0 ... β_0_n) is 1 (each B is used once)
And then we simply maximize
T = sum for i = 0 to n of 2 ^ G_i - 1
Which will add a 0 for each empty group, and a larger value for each non-empty group.
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
It can easily be solved by a mathematical constraint solver:
Let's use following terms:
- @A = number of items with category A
- @B = number of items with category B
- n = min(@A, @B)
- G_j = sum for i = 0 to @A of ( α_j_i * A_i) + sum for i=0 to @B of ( β_j_i * B_i )
Then the following equations must hold in order to have a valid solution:
- G_j is either 0 OR G_j is larger than 20
- min(α_j_0 ... α_j_@A ... β_j_0 ... β_j_@B) is either 0 or 1
- sum(α_0_0 ... α_0_n) is 1 (each A is used once)
- sum(β_0_0 ... β_0_n) is 1 (each B is used once)
And then we simply maximize
T = sum for i = 0 to n of 2 ^ G_i - 1
Which will add a 0 for each empty group, and a larger value for each non-empty group.
It can easily be solved by a mathematical constraint solver:
Let's use following terms:
- @A = number of items with category A
- @B = number of items with category B
- n = min(@A, @B)
- G_j = sum for i = 0 to @A of ( α_j_i * A_i) + sum for i=0 to @B of ( β_j_i * B_i )
Then the following equations must hold in order to have a valid solution:
- G_j is either 0 OR G_j is larger than 20
- min(α_j_0 ... α_j_@A ... β_j_0 ... β_j_@B) is either 0 or 1
- sum(α_0_0 ... α_0_n) is 1 (each A is used once)
- sum(β_0_0 ... β_0_n) is 1 (each B is used once)
And then we simply maximize
T = sum for i = 0 to n of 2 ^ G_i - 1
Which will add a 0 for each empty group, and a larger value for each non-empty group.
answered yesterday


Joris Schellekens
5,69811141
5,69811141
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
add a comment |
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224418%2falgorithm-to-group-items-according-to-simple-rules%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
QJUOrRxzZCJ7,T7vCKBP,SI8O2t p
what is the requirement which rejects solution where all items are in one group?
– Marek R
yesterday
The goal is to have as many groups as possible.
– xxxo
yesterday
how many items can we expect? How many groups could be made? How big can the values be?
– juvian
yesterday
@juvian We generally expect between 1 and 50 items, there is no limit to the number of groups that can be made and values may vary between 1 and 500.
– xxxo
yesterday
@JimMischel The expected output is the groups of items (in my example 2 groups: A1+B2+B3 and A2+B1). My example is a sample input and output :)
– xxxo
yesterday