-
Notifications
You must be signed in to change notification settings - Fork 0
/
anneal.m
131 lines (115 loc) · 3.04 KB
/
anneal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function solglobal = anneal(solinit, L, a, v, conf, onoff)
% function solglobal = anneal(solinit, L, a, v, conf, onoff)
% Search for the best transmission power configurations and application probabilities
% L: Loss matrix
% a: minimal traffic per reader
% v: readers for joint traffic maximization
% conf: configuration of the anti-collision protocol in each reader
% onoff: use independent powers (0) or same power (1)
global solglobal;
% External parameters
rand('seed',1);
N = size(L,1);
a = a(:); % Column
v = v(:); % Column
% Internal parameters
timeslot = 0.3; % s
Ptx = 0:0.0025:1; % W
REPMAX = 100;
LAGRANGEMENOR = 100000;
% Annealing initialization
fmax = -1;
rep = 0;
cambios = 0;
KT = 5.0;
delta = 1.0;
prob = exp(-KT)
% Search for start solution, round robin correspond to identity matrix
if ~isempty(solinit)
P = solinit.P;
else
P = eye(size(L));
end
sol = solveLP(L,P,timeslot,a,v,conf);
sol.fobj = sol.tasaobj;
sol.Ptx = Ptx;
sol.KT = KT;
sol.prob= prob;
sol.potencia = sum(sol.P'*sol.alfa);
% Add secondary goal of reducing joint transmission power
sol.fobj = sol.tasaobj-sol.potencia/LAGRANGEMENOR;
if isnan(sol.fobj)
fprintf('Unfeasible problem\n');
solglobal = NaN;
return
end
solglobal = sol;
fprintf('Initial solution: %0.6f, %d\nStarting annealing...\n', sol.fobj, size(sol.P,1));
% Annealing running
cambios = 0;
while 1
countloop = 0;
while 1
P = updateP(sol);
if onoff
potencia = Ptx(randi(length(sol.Ptx)));
P(P>0) = potencia;
end
% Solve associated LP
solaux = solveLP(L, P, timeslot, a, v, conf);
solaux.fobj = solaux.tasaobj;
solaux.potencia = sum(solaux.P'*solaux.alfa);
solaux.fobj = solaux.tasaobj-solaux.potencia/LAGRANGEMENOR;
% If feasible solution break
countloop = countloop + 1;
if ~isnan(solaux.fobj) break; end
end
fprintf('Sol updated: %0.6f %0.6f %d %d %d \r', solaux.tasaobj, solaux.potencia, size(solaux.P,1), rep, countloop);
% Is solution accepted?
cambio = 0;
forzado = 0;
if solaux.fobj>sol.fobj
cambio = 1;
elseif rand()<prob
cambio = 1;
forzado = 1;
end
if cambio
cambios = cambios + 1;
cambio = 0;
sol = solaux;
sol.Ptx = Ptx;
sol.KT = KT;
sol.prob = prob;
fprintf('\t\t\t\t\t +Sol accepted, prob: %0.3f, changes: %d, forzed: %d ', prob, cambios, forzado)
if solglobal.fobj < sol.fobj
solglobal = sol;
fprintf('[Sol global updated: %0.6f %d %d]\n', solglobal.fobj, size(sol.P,1), sum(sol.alfa>0));
else
fprintf('[No global updated]\n');
end
end
% Have we finish?
rep = rep + 1;
if rep==REPMAX || forzado
if cambios
rep = 0;
KT = KT+delta;
delta = 2*delta;
prob = exp(-KT)
cambios = 0;
if ~forzado
fprintf('\nReset sol to solglobal\n');
sol = solglobal; % Reseting after a cycle without improvements in global solution
end
else
fprintf('\nFinished with fobj: %0.3f %d\n', solglobal.fobj, size(solglobal.P,1));
mask = solglobal.alfa>0;
solglobal.P = solglobal.P(mask,:);
solglobal.B = solglobal.B(mask,:);
solglobal.alfa = solglobal.alfa(mask);
break % End of annealing
end
end
end
end