Algorithm to group items according to simple rules









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 :



  1. A1 : 10

  2. B1 : 11

  3. A2 : 9

  4. B2 : 6

  5. 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 :



  1. A1 + B2 + B3

  2. 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.










share|improve this question





















  • 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














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 :



  1. A1 : 10

  2. B1 : 11

  3. A2 : 9

  4. B2 : 6

  5. 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 :



  1. A1 + B2 + B3

  2. 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.










share|improve this question





















  • 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












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 :



  1. A1 : 10

  2. B1 : 11

  3. A2 : 9

  4. B2 : 6

  5. 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 :



  1. A1 + B2 + B3

  2. 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.










share|improve this question













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 :



  1. A1 : 10

  2. B1 : 11

  3. A2 : 9

  4. B2 : 6

  5. 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 :



  1. A1 + B2 + B3

  2. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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.






share|improve this answer




















  • Hi, I'm sorry but I don't understand this.
    – xxxo
    18 hours ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer




















  • Hi, I'm sorry but I don't understand this.
    – xxxo
    18 hours ago














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.






share|improve this answer




















  • Hi, I'm sorry but I don't understand this.
    – xxxo
    18 hours ago












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Joris Schellekens

5,69811141




5,69811141











  • 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




Hi, I'm sorry but I don't understand this.
– xxxo
18 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo