Logic Code required to be run in finite time
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I have 2 sets of data. Set 1 contains 57 complex numbers. Set 2 is the same set of complex no. but with opposite sign.
nth term in sets is defined as
Set 1 = (An+iBn)
Set 2 = (-An-iBn)
n=57
Eg:
Set 1 (2+2i , 3-2i, -4+6i......57 terms total)
Set 2 (-2-2i, -3+2i, 4-6i....57 terms toal).
Finally what I want is to create a SET 3 which consists of 57 terms. Each of the nth term in SET 3 can be either from SET1 or SET2.
Condition of SET3 is that: Finally when I take the complex SUM of 57 terms of SET3, and take the Magnitude part of the SUM (RMS) it should be the least possible minimum value out of the all possible combinations of 57 terms from SET1 or SET2.
Total no. of combinations (loop in program) that are required to be run if we want to check sum of each combination, is 2^57 which is not possible in finite time.
Is there any logical way to address this problem and get the answer.
Any help would be appreciated.
Thank you.!!
0 comentarios
Respuestas (1)
John D'Errico
el 18 de Abr. de 2025
Editada: John D'Errico
el 19 de Abr. de 2025
I think you misunderstand an important idea of computing. You NEVER want to use brute force on big problems. Those problems always get far too intensive to solve. And that is what you wnat to do, checking all 2^57 combinations to minimize a result. I say you misunderstand things, because you are clearly thinking about this in terms of a loop, and the need to check every possibility. And that is the wrong way to go, since you will never be able to solve your problem. It means you need to learn to think differently on this class of problem.
Instead, this is what optimization tools are designed to solve! If I understand your question, you have a vector, for example, here I have a complex vector of length 10:
n = 10;
X = randn(1,n) + i*randn(1,n)
Now you want to chose a boolean vector B, thus a vector of length the same as X, but composed of zeros and ones. The objective is to choose B, such that
abs(dot(B,X) + dot(1-B,-X))
is minimized? So as an example, I'll choose three random vectors B, and show that we get different results for each.
B = rand(n,3) > 0.5
abs(X*B + (-X)*(1-B))
There will be some binary vector B, out of the 1024 possible such vectors in this small example that minimizes the result. I'll use an optimization tool to find the solution. Best would be a tool like intlinprog, but this is not a linear problem, so intlinprog is not an option. Our object function would be something like this:
obj = @(B) abs(X*B - X*(1-B));
But first, I can actually make the objective simpler, because we can rewrite it as:
X*B - X*(1- B) == -X + X*2*B = X*(2*B - 1)
abs(X*(2*B-1))
As you can see, the two results are identical. This works because X is a row vector, and B a binary column vector.
obj = @(B,X) abs(X*(2*B(:)-1));
When B == 0, 2*B-1 is -1, and when B == 1, 2*B-1 reduces to 1. So it still does what you want. Regardless, now I'll use an optimization tool. GA comes to mind.
intcon = 1:n;
lb = zeros(n,1);
ub = ones(n,1);
opts = optimoptions('ga')
opts.Display = 'iter';
[Bvec,Fval,exitflag] = ga(@(B) obj(B,X),n,[],[],[],[],lb,ub,[],intcon,opts)
Is that the optimal solution? We can verify it easily enough, using brute force for this small problem, as that gives us a way to check the small problem, before I go on to a larger problem where I cannot make a brute force validation.
B = dec2bin(0:1023)' - '0';
[objmin,ind] = min(abs(X*(2*B - 1)))
B(:,ind)'
And indeed, it works. Note that I used only a vector of length 10 to make the brute force solution a viable one. I'll redo it now with a vector of length 57.
n = 57;
X = randn(1,n) + i*randn(1,n);
intcon = 1:n;
lb = zeros(n,1);
ub = ones(n,1);
tic,[Bvec,Fval,exitflag] = ga(@(B) obj(B,X),n,[],[],[],[],lb,ub,[],intcon,opts),toc
If you were worried that GA might not have found the truly optimal solution, you could force it to run a little longer. But it seems to have converged to a happy place. And it only used less than a second of CPU time, which in my opinion is a finite amount of time.
0 comentarios
Ver también
Categorías
Más información sobre Problem-Based Optimization Setup en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!