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 个评论

"... or what is the right answer."
Particle swarm optimization, like all global optimizers, is not guaranteed to find the global solution.
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.
Thank you Stephen I know that. 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. That is awar from particleswarm.
@ahmad eldeeb: please upload your function by clicking the paperclip button.
PS: and please also upload the input variables that you are using, in a .mat file.
@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?
not particle swarm. i substitute in the function with a specific value on a computer and do the same on other computer then i don't get the same function value.
@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.
@Stephen Cobeldick, I read your answer, I tried all possible commands (from my point of view) to solve the system of linear equations without having 'warning: matrix is close to singular or.....'. However, results I get are right as I compare with available results.
@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.
I tried or the commented lines. Some give the same results but with the same warning.
@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 个)

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 个评论

thank you for respond. My function file doesn't have random numbers. forget about particleswarm.
Again, forget about particle swarm. I have a value and substitute in the function file I had which had no random numbers. on different computers I got different function values.
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.
my code doesn't have a built-in function. It's attached above.
What does "my code doesn't have a built-in function" mean?
i don'y use rand or particleswarm in the function fuile.
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.

请先登录,再进行评论。

类别

产品

版本

R2017b

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by