# How to plot a graph in simulated annealing code? How can we know the solution is near the true value?

3 ビュー (過去 30 日間)
Siti Nur Izzaty Nor Izuddin 2019 年 11 月 9 日
コメント済み: Walter Roberson 2019 年 11 月 9 日
I have 26 resistors but I have not yet assigned the position of all the resistance values in my circuit. There are in total 26! = 4*10^26 possible configurations of the circuit. My main concern is I want to find the minimum possible power dissipation (or a near approximation) and a coressponding network configuration. The minimum possible power dissipation that I got is 0.021965 (after a few times running the code) with the order of R=[2 8 9 14 2 3 8 13 2 3 2 7 7 8 9 1 3 7 2 6 9 13 4 7 12 15]. Here comes the problems:
1. How can I be confident that the solution is near the true value as the minimum value keeps changing when I run the code?
2. I'm trying to plot a graph showing the intermediate values of power against iteration but it seems like it didnt work. The iteration value exceeds the limit.
As you can see below, I've included 2 set of codes; the first one is the function for P=calc_power_B(R,V); the second one is the simulated annealing code that calls the function P=calc_power_B(R,V).
Thank you.
function P = calc_power_B(R,V)
A = [ (R(1)+R(5)+R(6)+R(9)) -R(6) 0 -R(9) 0 0 0 0 0 0;
-R(6) (R(2)+R(6)+R(7)+R(10)) -R(7) 0 -R(10) 0 0 0 0 0;
0 -R(7) (R(3)+R(7)+R(8)+R(11)) 0 0 -R(11) 0 0 0 -R(8);
-R(9) 0 0 (R(9)+R(12)+R(13)+R(16)) -R(13) 0 -R(16) 0 0 0;
0 -R(10) 0 -R(13) (R(10)+R(13)+R(14)+R(17)) -R(14) 0 -R(17) 0 0;
0 0 -R(11) 0 -R(14) (R(11)+R(14)+R(15)+R(18)) 0 0 -R(18) -R(15);
0 0 0 -R(16) 0 0 (R(16)+R(19)+R(20)+R(23)) -R(20) 0 0;
0 0 0 0 -R(17) 0 -R(20) (R(17)+R(20)+R(21)+R(24)) -R(21) 0;
0 0 0 0 0 -R(18) 0 -R(21) (R(18)+R(21)+R(22)+R(25)) -R(22);
0 0 R(8) 0 0 R(15) 0 0 R(22) -(R(4)+R(8)+R(15)+R(22)+R(26)) ];
b = [0;0;0;0;0;0;0;0;0;V];
Iloop = A \ b; %%%% Use backslash \ to solve A Iloop = b
I = zeros(26,1);
I(1) = Iloop(1);
I(2) = Iloop(2);
I(3) = Iloop(3);
I(4) = Iloop(10);
I(5) = Iloop(1);
I(6) = Iloop(1)-Iloop(2);
I(7) = Iloop(2)-Iloop(3);
I(8) = Iloop(3)-Iloop(10);
I(9) = Iloop(1)-Iloop(4); %it doesn't matter if 1-4 or 4-1
I(10)= Iloop(2)-Iloop(5); %2-5
I(11)= Iloop(3)-Iloop(6); %3-6
I(12)= Iloop(4);
I(13)= Iloop(4)-Iloop(5);
I(14)= Iloop(5)-Iloop(6);
I(15)= Iloop(6)-Iloop(10);
I(16)= Iloop(4)-Iloop(7); %4-7
I(17)= Iloop(5)-Iloop(8); %5-8
I(18)= Iloop(6)-Iloop(9); %6-9
I(19)= Iloop(7);
I(20)= Iloop(7)-Iloop(8);
I(21)= Iloop(8)-Iloop(9);
I(22)= Iloop(9)-Iloop(10);
I(23)= Iloop(7);
I(24)= Iloop(8);
I(25)= Iloop(9);
I(26)= Iloop(10);
P = (I.*I)'*R;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = 100;
R = [2 1 2 7 6 3 2 2 2 8 12 8 4 3 7 7 9 7 3 8 13 14 9 9 13 15]';
V = 1;
P = calc_power_B(R,V);
while T > 1e-7,
mov = randi(26,10,1);
if mov(1) ~= mov(10),
if mov(1) > mov(10),
mov = mov(10:-1:1);
end
ind = [[0:mov(1)-1],[mov(10):-1:mov(1)],[mov(10)+1:27]];
Rnew = R(ind(2:27));
Pnew = calc_power_B(Rnew,V);
if Pnew < P
R = Rnew;
P = Pnew;
else
Rand_variable = rand;
if Rand_variable < exp(-(Pnew-P)/T),
R = Rnew;
P = Pnew;
end
end
T = T * 0.999;
end
end
clc
% plot([1:4*10^26],PP,[1:4*10^26],0*PP+Pmin,[1:4*10^26])
disp(['Minimum power is ' num2str(P) ' W'])
disp([' with permutation R = [' num2str(R') ']'])

#### 1 件のコメント

Walter Roberson 2019 年 11 月 9 日
You talk about 26! but your R that gives the value you indicate has at least 3 copies of 7 which implies that you are not taking care to only get unique values, so the number of possibilities is not 26! but rather 26^26.

サインインしてコメントする。

### 回答 (1 件)

darova 2019 年 11 月 9 日
• How can I be confident that the solution is near the true value as the minimum value keeps changing when I run the code?
Well, stop the code when the minimum doesn't change anymore. Simple?
• I'm trying to plot a graph showing the intermediate values of power against iteration but it seems like it didnt work. The iteration value exceeds the limit.
Try:
iter = 0;
hold on
while T > 1e-7
iter = iter + 1;
plot(iter,P,'.b')
end
hold off

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by