Different values obtained from same function file on different computers
显示 更早的评论
Hello, I use particle swarm built-in function on matlab 2017b. I have my function file that to optimized. I got results. I took this results to another computer with the same version of matlab and resubtitute in the same function file with the optimum position but I don't get the same value of function. The two computers are 64 bit. Any solution, or what is the right answer.
11 个评论
Stephen23
2018-8-23
"... or what is the right answer."
Particle swarm optimization, like all global optimizers, is not guaranteed to find the global solution.
Adam Danz
2018-8-23
Are both versions 2017b?
If you were to run the function file on the same computer 4-5 times, do you get identical results within the same computer for both computers?
Several parts of the algorithm for Particle Swarm involve random numbers which could result in non-identical outcomes.
ahmad eldeeb
2018-8-23
Jan
2018-8-23
@admad eldeeb: I do not understand this sentence:
if you have a number and subtitute in a function file you got a
value for the function and when you took this function file with
same subtitution you don't get the same function value.
What exactly are you doing? Does your question concern the particle swarm optimization or not?
ahmad eldeeb
2018-8-23
@ahmad eldeeb: it is possible that the algorithm is not very numerically stable. In particular your use of inv explicitly brings up this warning:
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 3.683468e-025.
Interestingly you tried several alternatives to inv but commented them out:
% u=double(A\B);
u=double(inv(A)*B);
% u=(double((A\eye(size(A)))*B))*10^15;
% u=lscov(A,B);
% u=(double(pinv(A)*B));
% u=(double(inv(A)*B));
% u=double(mldivide(A,B));
% u=double(bicgstab(A,B));
% u=double(gmres(A,B));
So it seems that you are already aware that your algorithm has some numeric problems. If that matrix is close to singular then it is quite possible that different computer HW can return different results, because at that point it is just amplifying floating point error.
The lines which calculate E2, h2, ro2, al2, etc should also be checked carefully, because you take differences of very similar numbers and then perform arithmetic operations using values of very different magnitudes (around the order of precision of double numbers): both of these can easily produce meaningless floating point noise in the outputs, or at least produce outputs which are nothing like "real" algebraic maths.
If you want detailed analysis of your code's numeric stability I suggest that you politely ask John D'Errico to take a look at this thread.
You should also pay attention to MATLAB's static code analysis tools: the Editor shows 74 places where you can improve the efficiency and simplicity of the code, with advice on what to change. Do not ignore this advice.
ahmad eldeeb
2018-8-24
@ahmad eldeeb: then you will have to do some debugging. Work backwards from the final values, and figure out where they differ: at each line compare the values and see which ones are different. It won't be quick, but it will tell you where these differences occur. Let us know what you find.
ahmad eldeeb
2018-8-24
@ahmad eldeeb: I was not talking about inv, I was talking about your original question: i.e. why the same function on two different machines produces two different values with the same input data. You have made it clear that there are no random numbers, so your best option is to do some debugging, exactly as I described in my last comment. This has nothing (in particular) to do with inv or any warnings, it is solely a method to find out where the data in the two functions diverge from each other. At this point debugging and comparing the intermediate calculations is what I would do, and what other experienced users would do. You can too!
Probably the first and most important step is to confirm that the two functions are actually the same: the MATLAB IDE has a file comparison tool to help with that:
回答(1 个)
Steven Lord
2018-8-23
There are many reasons why calling the same function file twice with the same inputs may return different answers. The most common is, as Adam Danz said, calling the random number generators.
function y = myfun415924(x)
y = x + randi([0 10], 1);
Now call this function repeatedly:
z = zeros(1, 10);
for k = 1:10
z(k) = myfun415924(5);
end
disp(z)
You may have some duplicate values in z, but you will also have some values that are different. If you want reproducible results in this type of scenario, control the random number generator to generate random numbers that are repeatable. [Note: you may be tempted later on to adapt my example to change the random number generator state at each iteration of a loop to generate "more random" numbers. DON'T. As that documentation page states, reseeding too frequently may be hazardous to the statistical properties of your random numbers.]
z = zeros(1, 10);
for k = 1:10
rng default
z(k) = myfun415924(5);
end
disp(z)
7 个评论
ahmad eldeeb
2018-8-23
ahmad eldeeb
2018-8-23
You may not be calling any random variables within your function but it's possible that the built-in functions you're using rely on random variables.
One way to test that is by setting the random number generator seed prior to running your function. Assuming you're using the same version of matlab on both machines, set the rng to the same value and look at your outputs.
If they match using the same seed, you'll know that a random process is the cause of the discrepancy.
ahmad eldeeb
2018-8-23
Jan
2018-8-23
What does "my code doesn't have a built-in function" mean?
ahmad eldeeb
2018-8-23
Adam Danz
2018-8-23
Hi Ahmad, I just glanced at your code briefly and saw 3 built-in functions in the first few lines:
size(); round(); sum()
A built-in function is a function that comes along with Matlab. Many of Matlab's built-in function use randomization. I suggest you take the advice of Jan and Stephen and it also wouldn't hurt to try the random number seed that I suggested above to rule out any random processes that you may be unaware of.
类别
在 帮助中心 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!