Answer the question
In order to leave comments, you need to log in
How can I display the number of combinations in this algorithm?
I understand that the question may be stupid, I ran into the annealing algorithm, I understood the essence a little, but I could not fully understand the algorithm, please help me to deduce the number of combinations...
Program Otzig;
const N = 20; (*размер доски и число ферзей*)
Tn = 30.0; (*начальная температура*)
Tk = 0.5; (*конечная температура*)
Alfa = 0.98; (*скорость охлаждения*)
ST = 100; (*число итераций при смене T*)
type TMember = record (*решение*)
Plan :array[1..N] of word; (*кодировка*)
Energy :word; (*энергия*)
end;
var Current :TMember; (*текущее решение*)
Working :TMember; (*рабочее решение*)
Best :TMember; (*лучшее решение*)
T :double; (*температура*)
Delta :double; (*разница энергий*)
P :double; (*вероятность допуска*)
fNew :boolean; (*флаг нового решения*)
fBest :boolean; (*флаг лучшего решения*)
Time :longint; (*этап поиска*)
Step :longint; (*шаг на этапе поиска*)
Accepted :longint; (*число новых решений*)
procedure Swap(var M:TMember); (*модификация решения*)
var x,y,v :word;
begin
x:=Random(N)+1;
Repeat
y:=Random(N)+1;
Until x<>y;
v:=M.Plan[x]; M.Plan[x]:=M.Plan[y]; M.Plan[y]:=v;
end;
12
procedure New(var M:TMember); (*инициализация решения*)
var i:word;
begin
for i:=1 to N do M.Plan[i]:=i;
for i:=1 to N do Swap(M);
end;
procedure CalcEnergy(var M:TMember); (*расчет энергии*)
const dx:array[1..4] of integer = (-1,1,-1,1);
dy:array[1..4] of integer = (-1,1,1,-1);
var j,x,tx,ty :word;
error :word;
begin
error:=0;
for x:=1 to N do begin
for j:=1 to 4 do begin
tx:=x+dx[j]; ty:=M.Plan[x]+dy[j];
While (tx>0)and(tx<=N)and
(ty>0)and(ty<=N) do begin
if M.Plan[tx]=ty then inc(error);
tx:=tx+dx[j]; ty:=ty+dy[j];
end;
end;
end;
M.Energy:=error;
end;
procedure Copy(var MD,MS:TMember); (*копирование решения*)
var i:word;
begin
for i:=1 to N do MD.Plan[i]:=MS.Plan[i];
MD.Energy:=MS.Energy;
end;
procedure Show(M:TMember); (*отображение на экране*)
var x,y :word;
begin
writeln('Решение:');
for y:=1 to N do begin
for x:=1 to N do
if M.Plan[x]=y then write('Q')
else write('.');
writeln;
end;
writeln;
end;
13
begin
Randomize; T:=Tn; fBest:=false;
Time:=0; Best.Energy:=100;
New(Current);
CalcEnergy(Current);
Copy(Working,Current);
While T>Tk do begin
Accepted:=0;
for Step:=0 to ST do begin
fNew:=false;
Swap(Working);
CalcEnergy(Working);
if Working.Energy<=Current.Energy then
fNew:=true
else begin
Delta:=Working.Energy-Current.Energy;
P:=exp(-Delta/T);
if P>random then begin
Accepted:=Accepted+1;
fNew:=true;
end;
end;
if fNew then begin
fNew:=false; Copy(Current,Working);
if Current.Energy<Best.Energy then begin
Copy(Best,Current); fBest:=true;
end;
end else begin
Copy(Working,Current);
end;
end;
writeln('Temp=',T:6:2,' Energy=',best.Energy:4);
T:=T*Alfa;
Time:=Time+1;
end;
if fBest then Show(Best);
end.
Answer the question
In order to leave comments, you need to log in
ST = 100;
It can be seen that there is a cycle for Step:=0 to ST do, i.e. 101 times, which is nested in the
While T>Tk do loop
But your indentation is so bad that you can't understand where the end's are from different if's and from these loops.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question