|
1 |
| -function V = sim(net,V,U) |
| 1 | +function V = sim(net,V,U,isSaddle) |
| 2 | + |
| 3 | + if nargin == 1 % Start in the center of the Hypercube. Replace for saddle? |
| 4 | + U = rand(net.TrainParam.N,1)-.5; |
| 5 | + V = 0.5 + 1e-7*U; |
| 6 | + elseif nargin == 2 |
| 7 | + U = net.Setting.InvTransferFcn(V); |
| 8 | + end |
| 9 | + |
| 10 | + % Send data to GPU |
| 11 | + if strcmp(net.Setting.ExecutionEnvironment,'gpu') |
| 12 | + U = gpuArray(U); |
| 13 | + V = gpuArray(V); |
| 14 | + end |
| 15 | + |
| 16 | + if nargin < 4 |
| 17 | + isSaddle = false; |
| 18 | + end |
| 19 | + |
| 20 | + if ~isSaddle % Logging time and Checkpoint |
| 21 | + timeID = tic; |
2 | 22 |
|
3 |
| - timeID = tic; |
| 23 | + if ~isempty(net.Setting.CheckpointPath) |
| 24 | + net.Results.CheckpointFilename = ... |
| 25 | + utils.checkpoint.createFilename(net.Setting.CheckpointPath,net.SimFcn); |
| 26 | + else |
| 27 | + net.Results.CheckpointFilename = ''; |
| 28 | + end |
| 29 | + end |
| 30 | + |
4 | 31 | net = reinit(net);
|
| 32 | + |
| 33 | + if ~isfield(net.TrainParam,'T') |
| 34 | + error('HopfieldNetworkGQKP:NotTrained', 'Training has not taken place yet. Use train(net).'); |
| 35 | + end |
| 36 | + |
| 37 | + % Classic Scheme support only |
5 | 38 | if strcmp(net.SimFcn,'euler')
|
6 |
| - if nargin < 2 |
7 |
| - [net,V,~,iter] = simEuler(net); |
8 |
| - else |
9 |
| - [net,V,~,iter] = simEuler(net,V,U); |
10 |
| - end |
| 39 | + [net,V,~,iter] = simEuler(net,V,U,isSaddle); |
| 40 | + |
11 | 41 | elseif strcmp(net.SimFcn,'talavan-yanez')
|
12 |
| - if nargin < 2 |
13 |
| - [net,V,~,iter] = simTalavanYanez(net); |
14 |
| - else |
15 |
| - [net,V,~,iter] = simTalavanYanez(net,V,U); |
16 |
| - end |
| 42 | + [net,V,~,iter] = simTalavanYanez(net,V,U,isSaddle); |
17 | 43 | end
|
18 | 44 | net = computeSolution(net,V,iter);
|
19 | 45 |
|
|
22 | 48 |
|
23 | 49 |
|
24 | 50 | % Euler Algorithm
|
25 |
| - |
26 |
| -function [net,V,U,iter] = simEuler(net,V) |
| 51 | +function [net,V,U,iter] = simEuler(net,V,U,isSaddle) |
27 | 52 |
|
28 | 53 | % Stopping criteria
|
29 | 54 | stopC1 = power(10, -1 * net.Setting.E);
|
|
38 | 63 | % Memory allocation
|
39 | 64 | dU = zeros(N,1);
|
40 | 65 |
|
41 |
| - if nargin == 1 |
42 |
| - U = rand(N,1)-.5; % TODO Different from Pedro's algorithm |
43 |
| - V = 0.5 + 1e-7*U; % TODO Different from Pedro's algorithm |
44 |
| - end |
45 |
| - |
46 | 66 | iter = 1;
|
47 | 67 | % Showing Network parameters in the command window
|
48 | 68 | % if net.Setting.Verbose
|
49 | 69 | % hopfield.tsp.display.printWhenStarting(net);
|
50 | 70 | % end
|
51 | 71 |
|
| 72 | + if ~isSaddle % Logging Checkpoint and plotting Simulation process |
| 73 | + if ~isempty(net.Setting.CheckpointPath) || net.Setting.SimulationPlot |
| 74 | + if ~isempty(net.Setting.CheckpointPath) |
| 75 | + utils.checkpoint.loggingData(fullfile(net.Setting.CheckpointPath, ... |
| 76 | + net.Results.CheckpointFilename),... |
| 77 | + net.Setting.MaxIter,iter,V,zeros(size(V))); |
| 78 | + end |
| 79 | + if net.Setting.SimulationPlot |
| 80 | + fV = viewConvergence(iter,V,net); |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
52 | 85 | dt = net.Setting.Dt;
|
53 | 86 | net.Results.Time(iter) = dt;
|
54 | 87 | maxDiffV = 1;
|
|
66 | 99 | V = net.Setting.TransferFcn(U);
|
67 | 100 |
|
68 | 101 | maxDiffV = max(abs(Vprev-V));
|
69 |
| - iter = iter + 1; |
| 102 | + iter = iter + 1; |
| 103 | + |
| 104 | + if ~isSaddle % Logging Checkpoint and plotting Simulation process |
| 105 | + if ~isempty(net.Setting.CheckpointPath) || net.Setting.SimulationPlot |
| 106 | + if ~isempty(net.Setting.CheckpointPath) |
| 107 | + utils.checkpoint.loggingData(fullfile(net.Setting.CheckpointPath,... |
| 108 | + net.Results.CheckpointFilename),... |
| 109 | + net.Setting.MaxIter,iter,V,dU); |
| 110 | + end |
| 111 | + if net.Setting.SimulationPlot |
| 112 | + fV = viewConvergence(iter,V,net,fV); |
| 113 | + end |
| 114 | + end |
| 115 | + end |
70 | 116 | end
|
| 117 | + |
| 118 | + if strcmp(net.Setting.ExecutionEnvironment,'gpu') && nargout > 1 |
| 119 | + V = gather(V); |
| 120 | + U = gather(U); |
| 121 | + end |
| 122 | + |
| 123 | + if ~isSaddle % Logging Checkpoint |
| 124 | + if ~isempty(net.Setting.CheckpointPath) |
| 125 | + utils.checkpoint.trimToSimulatedData(net.Setting.CheckpointPath, ... |
| 126 | + net.Results.CheckpointFilename, iter) |
| 127 | + end |
| 128 | + end |
| 129 | + |
71 | 130 | net.Results.x = V;
|
72 | 131 | end
|
73 | 132 |
|
74 | 133 | % Talaván-Yáñez Algorithm
|
75 | 134 |
|
76 |
| -function [net,V,U,iter] = simTalavanYanez(net,V,U) |
77 |
| - |
| 135 | +function [net,V,U,iter] = simTalavanYanez(net,V,U,isSaddle) |
78 | 136 |
|
79 |
| - N = net.ProblemParameters.networkSize(1); |
80 | 137 | T = net.TrainParam.T;
|
81 | 138 | ib = net.TrainParam.ib;
|
82 |
| - |
83 |
| - if nargin == 1 |
84 |
| - U = rand(N,1)-.5; % TODO Different from Pedro's algorithm |
85 |
| - V = 0.5 + 1e-7*U; % TODO Different from Pedro's algorithm |
86 |
| - end |
87 |
| - if strcmp(net.Setting.ExecutionEnvironment,'GPU') |
88 |
| - U = gpuArray(U); |
89 |
| - V = gpuArray(V); |
90 |
| - end |
| 139 | + |
91 | 140 | % Stopping criteria
|
92 | 141 | stopC1 = power(10, -1 * net.Setting.E);
|
93 | 142 | stopC2 = power(10, -1.5 * net.Setting.E);
|
94 | 143 | maxDiffV = 1;
|
95 | 144 | unstable = false;
|
96 |
| - |
| 145 | + |
| 146 | + trasferFcn2Str = func2str(net.Setting.TransferFcn); |
| 147 | + if strcmp(trasferFcn2Str,'@(u)0.5*(1+tanh(u./net.Setting.U0))') |
| 148 | + trasferFcn2Str = 'tanh'; |
| 149 | + elseif strcmp(trasferFcn2Str,'@(u)net.satlin(u,net.Setting.U0)') |
| 150 | + trasferFcn2Str = 'satlin'; |
| 151 | + else |
| 152 | + error('HopfieldNetworkTSP:sim:notDefinedTransferFcn', ... |
| 153 | + ['TransferFcn derivative not defined for ',net.Setting.TransferFcn]); |
| 154 | + end |
| 155 | + |
97 | 156 | u_e = net.Setting.InvTransferFcn(stopC1);
|
98 | 157 |
|
99 | 158 | iter = 1;
|
|
102 | 161 | % hopfield.tsp.display.printWhenStarting(net);
|
103 | 162 | % end
|
104 | 163 |
|
| 164 | + if ~isSaddle % Logging Checkpoint and plotting Simulation process |
| 165 | + if ~isempty(net.Setting.CheckpointPath) || net.Setting.SimulationPlot |
| 166 | + if ~isempty(net.Setting.CheckpointPath) |
| 167 | + utils.checkpoint.loggingData(fullfile(net.Setting.CheckpointPath, ... |
| 168 | + net.Results.CheckpointFilename),... |
| 169 | + net.Setting.MaxIter,iter,V,zeros(size(V))); |
| 170 | + end |
| 171 | + if net.Setting.SimulationPlot |
| 172 | + fV = viewConvergence(iter,V,net); |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | + |
105 | 177 | while iter < net.Setting.MaxIter && (maxDiffV > stopC1 || ...
|
106 | 178 | (maxDiffV > stopC2 && unstable))
|
107 | 179 |
|
|
113 | 185 |
|
114 | 186 | dU = T*V + ib;
|
115 | 187 |
|
116 |
| - dV = 2./net.Setting.U0 .* V .* (1-V) .*dU; |
117 |
| - |
| 188 | + if strcmp(trasferFcn2Str,'tanh') |
| 189 | + dV = 2./net.Setting.U0 .* V .* (1-V) .*dU; |
| 190 | + elseif strcmp(trasferFcn2Str,'satlin') |
| 191 | + dV = 2./net.Setting.U0 .* dU; |
| 192 | + end |
| 193 | + |
118 | 194 | interiorV = U > u_e & U < -u_e;
|
119 | 195 |
|
120 | 196 | % Computation of dt
|
|
209 | 285 |
|
210 | 286 | %%%
|
211 | 287 | % $E(t + \Delta t) = E(t) - S_{1}\Delta t + \frac{1}{2}S_{2}\Delta t^{2}$
|
212 |
| - if strcmp(net.Setting.ExecutionEnvironment,'GPU') |
| 288 | + if strcmp(net.Setting.ExecutionEnvironment,'gpu') |
213 | 289 | S1 = gather(S1);
|
214 | 290 | S2 = gather(S2);
|
215 | 291 | dt = gather(dt);
|
|
218 | 294 | S1.*dt + 0.5.*S2.*dt.^2;
|
219 | 295 | net.Results.Time(iter+1) = net.Results.Time(iter) + dt;
|
220 | 296 | iter = iter + 1;
|
| 297 | + |
| 298 | + if ~isSaddle % Logging Checkpoint and plotting Simulation process |
| 299 | + if ~isempty(net.Setting.CheckpointPath) || net.Setting.SimulationPlot |
| 300 | + if ~isempty(net.Setting.CheckpointPath) |
| 301 | + utils.checkpoint.loggingData(fullfile(net.Setting.CheckpointPath,... |
| 302 | + net.Results.CheckpointFilename),... |
| 303 | + net.Setting.MaxIter,iter,V,dU); |
| 304 | + end |
| 305 | + if net.Setting.SimulationPlot |
| 306 | + fV = viewConvergence(iter,V,net,fV); |
| 307 | + end |
| 308 | + end |
| 309 | + end |
221 | 310 | end
|
222 | 311 |
|
223 |
| - if strcmp(net.Setting.ExecutionEnvironment,'GPU') && nargout > 1 |
| 312 | + if strcmp(net.Setting.ExecutionEnvironment,'gpu') && nargout > 1 |
224 | 313 | V = gather(V);
|
225 | 314 | U = gather(U);
|
226 |
| - iter = gather(iter); |
227 | 315 | end
|
| 316 | + |
| 317 | + if ~isSaddle % Logging Checkpoint |
| 318 | + if ~isempty(net.Setting.CheckpointPath) |
| 319 | + utils.checkpoint.trimToSimulatedData(net.Setting.CheckpointPath, ... |
| 320 | + net.Results.CheckpointFilename, iter); |
| 321 | + end |
| 322 | + end |
| 323 | + |
228 | 324 | end
|
229 | 325 |
|
230 | 326 | function net = computeSolution(net,V,iter)
|
231 | 327 |
|
232 |
| - V(V > 1 - power(10, -1 * net.Setting.E)) = 1; %FIXME to be removed? |
| 328 | + V(V > 1 - power(10, -1 * net.Setting.E)) = 1; |
233 | 329 | V(V < power(10, -1 * net.Setting.E)) = 0;
|
234 | 330 |
|
235 | 331 | if isequal(net.ProblemParameters.R*V,net.ProblemParameters.b)
|
|
0 commit comments