How to calculate all permutations of a vector that satisfy a given condition
Show older comments
I'd like to calculate all permutations of the vector N taken k at a time. I also want to allow any element of N to permute with itself not only with other elements. I only want to get permutations that sum up to a given number n.
Here is an example in MATLAB:
N = [1 2 3 4];
k = 2;
n = 6;
For numbers above, I could reach my target by doing:
Perm = combvec(N, N)';
Perm = Perm(sum(Perm,2)==n,:);
Perm =
4 2
3 3
2 4
However, N is expected to be up to 90 elements long and value of k up to 10. This makes the above way is infeasible as it involves calculation of many permutations that are not needed.
Is there any way of doing this efficiently given the expected N vector length and k?
8 Comments
Walter Roberson
on 6 Sep 2019
This is what is referred to as a Knapsack Problem. You might want to search for algorithms for those.
Bruno Luong
on 6 Sep 2019
No it is not a Knapsack Problem
Walter Roberson
on 6 Sep 2019
Yes it is. The contents of N are the weights, and the selection of weights must add up to a fixed value. That is a Knapsack problem. Once all of knapsack solutions are known, then you can permute each of them.
Bruno Luong
on 6 Sep 2019
Edited: Bruno Luong
on 6 Sep 2019
NOPE There are subtility that you don't see the difference Walter
- knapsack try to maximize an objective that here there is none
- knsack problem try to find a single combination that maximizes the score (no more), here is ALL combinations that reach a sum.
Jos (10584)
on 6 Sep 2019
There are 90^10 = 34867844009999998976 possible combinations that you need to check or exclude somehow ... I am not sure what your real problem is, but are you sure this is the right approach?
Walter Roberson
on 9 Sep 2019
Bruno:
Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
Here the weights are the contents of N, and the value of each is 0; furthermore it is a binary knapsack problem as each item is to be either present or not.
It does not not require the full power of more general knapsack problems, but it is a knapsack problem.
Bruno Luong
on 9 Sep 2019
Edited: Bruno Luong
on 9 Sep 2019
No I have already explained why it is not quite the same.
The problem here is falls into enumerative combinatorics problem, the knapsack is a discrete optimization problem.
If you convince otherwise please show us the MATLAB code using knapsack.
Here is solving knacksack using INTLINPROG
N = [1 2 3 4];
k = 2;
n = 6; % 16
% knapsack solver using INTLINPROG
f = -N(:);
intcon = 1:4;
A = N(:).';
b = 6;
Aeq = ones(1,4);
beq = k;
lb = 0+zeros(4,1);
ub = 1+zeros(4,1);
x = intlinprog(f, intcon, A, b, Aeq, beq, lb, ub);
keep = logical(x);
c = N(keep)
It returns one solution for n=6, and for n=16 it's still return one solution (where one can discard by checking the sum), but in anycase it won't solve the problem posted above.
Walter Roberson
on 18 May 2020
The problem here is falls into enumerative combinatorics problem, the knapsack is a discrete optimization problem.
There can be multiple approaches to the same problem.
This is a classic constrained knapsack problem:
"Given a set of objects of different sizes, with a pre-set number of copies of each different size, find a combination of objects that comes closest to filling up a knapsack of fixed carrying capacity."
In this case, the number of copies of each is restricted to 1 (which is called a "binary knapsack".
Given any particular solution that exactly fills up the knapsack, you can then proceed to permute it mechanically to get the permutations needed by the user.
Accepted Answer
More Answers (0)
Categories
Find more on Particle Swarm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!