Updating multiple variables randomly with a constraint in a while cycle
Afficher commentaires plus anciens
I have a pretty complex function that calculates a 1 by 6 vector (phi) from another 1 by 6 vector (x) like (phi)=function(x) I want now to fix phi and look for all the possible x that give me that phi with the costraint that sum(x)==1 and that for every element of x(i), 0<= x <= 1
I'm trying with a WHILE loop but I don't know how to randomly update the x variable preventing it to try the same x multiple times; my desired solution have 3 digits of accuracy like x=[0.0112 0.0997 0.157 0.227 ecc.] I know there could be a maximum of 2 solutions and I need to find them both eventually.
I don't necessarily need to use a WHILE loop If there is any MATLAB functionality that can solve this it would be great.
11 commentaires
gabriele provenza
le 26 Mai 2020
darova
le 26 Mai 2020
Please attach the code
gabriele provenza
le 28 Mai 2020
darova
le 28 Mai 2020
- I want now to fix phi and look for all the possible x that give me that phi with the costraint that sum(x)==1;
Maybe you need fsolve or fmincon. Looks like optimization problem
gabriele provenza
le 28 Mai 2020
John D'Errico
le 28 Mai 2020
Modifié(e) : John D'Errico
le 28 Mai 2020
Incredibly confusing question. However you cannot use fmincon to choose anything randomly. You cannot use fmincon to solve a discrete problem, which this somehow seems like it might be. And finally, you cannot get fmincon to return two solutions. And of course we don't see your code, but only how you call it.
You can use fmincon with a constraint that the sum of the unknowns is a contstant, thus 1 here. But the rest of what you are asking for is really confused.
It is not clear how it is that you are sure there are exactly two solutions either.
Are you trying somehow to perform some sort of ad hoc optimization without the use of fmincon? Is that what the idea of choosing values randomly is intended to do?
gabriele provenza
le 30 Mai 2020
Modifié(e) : gabriele provenza
le 30 Mai 2020
Sindar
le 31 Mai 2020
While I don't know a simple way to iterate through the possibilities, counting them is straightforward. This is a classic "stars and bars" problem: you have 1000 units of 0.001 to split between the 6 elements. The number of possibilities is then (1000-1) choose (6-1), or about 8e12. This is very probably more than you can reasonably calculate.
Note: it's not clear what exactly you mean by "3 digits of accuracy," since your examples x elements have 4 digits after the decimal (3 significant figures?) and your last comment shows x with 5 digits. Still, it only gets (much much) worse if you have finer-than-0.001 precision
gabriele provenza
le 31 Mai 2020
Sindar
le 1 Juin 2020
In that case, the number of possible solutions is dramatically more, and significantly harder to either count or iterate over
Sindar
le 1 Juin 2020
Therefore, my suggestion is to drop the discretization, and treat it as a continuous optimization problem. Once you have an answer, truncate it to your desired accuracy and check how poor the agreement is.
Réponses (1)
Jeff Miller
le 1 Juin 2020
You might be able to do what you want with nested while loops, something like this. Pretty tedious though.
x = zeros(6,1);
step = 0.001; % minimum step between adjacent x values
x1max = 1 - 5*step; % x1 cannot be bigger than this because at least step is needed for all others
x(1) = 0;
nFound = 0; % number of solutions found so far
while x(1)<=x1max && nFound<2
x(1) = x(1) + step;
x2max = 1 - x(1) - 4*step;
x(2) = 0;
while x(2)<=x2max && nFound<2
x(2) = x(2) + step;
x3max = 1 - x(1) - x(2) - 3*step;
x(3) = 0;
% add further nested while loops like the above for x(3), x(4) and x(5)
% finally, deep in the midst of all these while loops, do this
x(6) = 1 - sum(x(1:5));
phi = func(x);
% check phi here.
% If phi is a solution you want, save it and increment nFound
end
end
1 commentaire
Sindar
le 1 Juin 2020
While this technically would work, there are several issues for this particular problem:
- the number of iterations is ridiculous (I estimated 8e12 above)
- Gabriele eventually clarified that they mean 3 significant figures, not 3 places after the decimal
- while possible, I think it unlikely that there is a way to check phi such that only the two desired solutions pass. Instead, I expect that this is a normal convergence problem where the closer x gets to x_solution, the closer phi gets to phi_solution. So, depending on what tolerance you set, you could either miss x_solution ("nah, this phi is 1e-16 off from the real one") or find solutions that are not distinct (one step apart)
- since you are iterating through in a set order, it doesn't make much sense to use "while" over "for". It makes it harder to track progress, restart from where you left off, parallelize, etc..
Still, this is close to the optimal solution to the particular request given. But, it won't work. Conclusion: use a different algorithm
Catégories
En savoir plus sur Surrogate Optimization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!