File: n1gc2a.f

package info (click to toggle)
scilab 2.4-1
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 55,196 kB
  • ctags: 38,019
  • sloc: ansic: 231,970; fortran: 148,976; tcl: 7,099; makefile: 4,585; sh: 2,978; csh: 154; cpp: 101; asm: 39; sed: 5
file content (355 lines) | stat: -rw-r--r-- 9,714 bytes parent folder | download | duplicates (4)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
      subroutine n1gc2a(simul,prosca,n,x,f,g,dx,df1,eps,imp,io,
     /                  niter,nsim,info,memh,d,xx,gg,tabaux,h,
     /                  izs,rzs,dzs)
      implicit double precision (a-h,o-z)
c     Copyright INRIA

c
c parametres
      double precision  zero     , un     , deux     , ro
      parameter  (  zero=0.0d+0, un=1.0d+0, deux=2.0d+0, ro=0.20d+0 )
c declaration des tableaux
      double precision    x(n),g(n),d(n),xx(n),gg(n),tabaux(n),h(*),
     /                    dzs(*)
      real rzs(*)
      integer  izs(*)
c declaration des scalaires
      double precision  f, dx, eps, df1
      double precision   dg1, dg, alpha, normg0, aux1, aux2, mu, eta,
     / omega, normg, gcarre, ggcarr, nu, sigma, sscalg, uscalg,
     / sscaek
      integer  n, memh, imp, io, nsim, niter, info
      integer  memuti, nrzuti, memsup, m, retour, iter,
     /   ntotap, nmisaj, i, iu, is, ieta, inu, j, kj, k, kp1
      logical  gc, iterqn, intfor, redfor, redem, termi
c
      external  simul, prosca
c
c  *************************************************************
c  phase i:determination de la methode ( et de m le cas echeant)
c  *************************************************************
c
      memuti=n*(n+1)/2
c
c memsup est aussi la dimension minimale de la matrice h
      memsup=2*n+2
c
      if (memh .ge. memuti) then
      gc=.false.
      nrzuti=memuti+4*n
      if (imp .gt. 1) write(io,1) nrzuti
      else if (memh .lt. memsup) then
      info=3
      return
      else
      gc=.true.
c m est le nombre de mises a jour admissible
      m=memh / memsup
c memuti est ici le nombre de places memoire utilisees pour stocker h
      memuti=m * memsup
      nrzuti=memuti+4*n
      if (imp .gt. 1) write(io,2) m,nrzuti
      endif
1     format(40h     methode de quasi-newton. nrz utile=,i7)
2     format(38h     methode du gradient conjugue avec,i3,
     / 14h mises a jour.,11h nrz utile=,i7)
c
c  ***********************************************
c  phase ii:initialisations propres a l'optimiseur
c  ***********************************************
c
c initialisation des compteurs
      iter=0
      ntotap=1
c
c  ******************************************************************
c  phase iii:demarrage a partir de x(0) avec descente suivant -(grad)
c  ******************************************************************
c
3000  i=0
      nmisaj=0
c
c calcul de la direction de descente
      do 3100 j=1,n
      d(j)=-g(j)
3100  continue
c
      call prosca(n,g,d,dg1,izs,rzs,dzs)
      normg0=sqrt(abs(dg1))
      if (iter .eq. 1) then
      omega=eps * normg0
      endif
c
c ************************************************************
c phase iv:debut de l'iteration x(i-1) donne x(i) le long de d
c ************************************************************
c
4000  if (iter .eq. niter) then
      info=4
      goto 99999
      endif
      iter=iter + 1
      i=i+1
c
c determination du type de pas
      if (gc) then
      iterqn=(i .le. m) .and. (2 .le. i)
      endif
c
c  *******************************
c  phase v:initialisation de alpha
c  *******************************
c
      if (iter .eq. 2) then
      alpha=deux * df1 /(-dg1)
      else if (gc) then
                   if (i.eq.1) then
                   alpha=un / normg0
                   else
                            if (iterqn) then
                            alpha=un
                            else
                            alpha=alpha * dg / dg1
                            endif
                   endif
      else
      alpha=un
      endif
c
c ***************************
c phase vi:recherche lineaire
c ***************************
c
      dg=dg1
      intfor=( gc .and. (.not.iterqn)).or. ((.not.gc) .and.(i.eq.1))
      do 6000 j=1,n
      xx(j)=x(j)
      gg(j)=g(j)
6000  continue
      call n1gc2b(n,simul,prosca,xx,f,dg,alpha,d,x,g,imp,io,retour,
     /           ntotap,nsim,intfor,dx,eps,izs,rzs,dzs)
c
      if (imp .gt. 3) write(io,6003)
      if ((retour .eq. 4).or.((retour .eq. 1).and.(i .eq. 1))) then
      info=6
      return
      else if (retour .eq. 1) then
      if (imp .gt. 1) write(io,6002) iter,ntotap
      goto 3000
      else
c calcul de (g,g)
      if((i .gt. 1) .and. gc) ggcarr=gcarre
      call prosca(n,g,g,gcarre,izs,rzs,dzs)
      normg=sqrt(gcarre)
      if (imp .gt. 2) write(io,6001)iter,ntotap,f
      if (retour .eq. 2) then
      info=0
      goto 99999
      else if (retour .eq. 3)then
      info=5
      goto 99999
      endif
      endif
6001  format(4x,6h n1gc2,3x,i4,6h iters,3x,i4,7h simuls,3x,2hf=,d15.9)
6002  format(4x,6h n1gc2,3x,i4,6h iters,3x,i4,7h simuls,
     / 33h necessite d'un redemarrage total)
6003  format()
c
c  ******************************************************
c  phase vii:test d'arret par obtention de la convergence
c  ******************************************************
c
      termi=normg .lt. omega
      if (termi) then
      info=1
      goto 99999
      else
      continue
      endif
c
c *******************************************
c  phase viii:test x(i) point de redemarrage?
c *******************************************
c
c doit on forcer un redemarrage?
      redfor=gc .and. ((i .eq. 1) .or. (i .eq. m+n))
      if (redfor) then
      redem=.true.
      else if (gc .and. .not. iterqn) then
      call prosca(n,g,gg,aux1,izs,rzs,dzs)
      redem=abs(aux1) .gt. abs(ro * ggcarr)
      else
      redem=.false.
      endif
c
c  ********************
c  phase ix:mise a jour
c  ********************
c
c calcul de s stocke dans d et de y stocke dans xx
      do 9000 j=1,n
      d(j)=alpha * d(j)
      xx(j)=g(j)-gg(j)
9000  continue
      if (redem) then
c cas ou x(i) est un point de redemarrage
      i=1
      nmisaj=1
c sauvegarde de s qui est actuellement dans d
c                u=h*y=y
c                nu=(y,hy)=(y,y)
c                eta=(s,y)
c calcul des indices
      inu=1
      ieta=inu + 1
      iu=ieta
      is=iu + n
c
      do 9100 j=1,n
      h(iu +j)=xx(j)
      h(is +j)=d(j)
9100  continue
      call prosca(n,xx,xx,nu,izs,rzs,dzs)
      h(inu)=nu
      call prosca(n,d,xx,eta,izs,rzs,dzs)
      h(ieta)=eta
c h1 est maintenant definie
c calcul de h1*g que l'on range dans xx
      call fmulb1(n,h,g,xx,tabaux,nmisaj,prosca,izs,rzs,dzs)
c
      else if (gc) then
c cas de gc sans redamarrage
c calcul de h*y range dans gg
       call fmulb1(n,h,xx,gg,tabaux,nmisaj,prosca,izs,rzs,dzs)
c calculs de  nu, eta, sscalg, uscalg
      call prosca(n,xx,gg,nu,izs,rzs,dzs)
      call prosca(n,d,xx,eta,izs,rzs,dzs)
      call prosca(n,d,g,sscalg,izs,rzs,dzs)
      call prosca(n,gg,g,uscalg,izs,rzs,dzs)
c calcul de sigma et de mu
      sigma=(uscalg -(un + nu / eta)* sscalg) / eta
      mu=sscalg /eta
c calcul de h*g que l'on range dans xx
      call fmulb1(n,h,g,xx,tabaux,nmisaj,prosca,izs,rzs,dzs)
c calcul de la nouvelle direction de recherche:
c h*g - mu * u - sigma * s
      do 9200 j=1,n
      xx(j)= xx(j) - mu * gg(j) - sigma * d(j)
9200  continue
c
c cas d'une iteration de type quasi newton
      if (iterqn) then
      nmisaj=nmisaj + 1
c sauvegarde des termes utiles pour stocker la matrice mise a jour
      inu=inu + memsup
      ieta=inu + 1
      iu=ieta
      is=iu + n
      do 9300 j=1,n
      h(iu +j)=gg(j)
      h(is +j)=d(j)
9300  continue
      h(inu)=nu
      h(ieta)=eta
      endif
c cas de la methode quasi newton
      else
c calcul de eta=(s,y)
      call prosca(n,d,xx,eta,izs,rzs,dzs)
      if (i .eq. 1) then
c etape initiale calcul de l'approximation initiale de l'inverse de la
c matrice hessienne
c calcul de nu=(y,h0*y)=(y,y)
      call prosca(n,xx,xx,nu,izs,rzs,dzs)
c stockage de cette matrice h=(eta / nu) * i
      kj=1
      aux1=eta / nu
      do 9500 k=1,n
      h(kj)=aux1
      kj=kj +1
      kp1=k+1
      if (n .ge. kp1) then
      do 9400 j=kp1,n
      h(kj)=zero
      kj=kj +1
9400  continue
      endif
      gg(k)=aux1 * xx(k)
9500  continue
      nu=eta
      else
      call fmuls1(n,h,xx,gg)
      call prosca(n,xx,gg,nu,izs,rzs,dzs)
      endif
c calcul de la matrice mise a jour (utilisation de la formule bfgs )
c nu, eta et h*y (stocke dans gg) sont connus
      aux1=un + nu / eta
      kj=1
      do 9800 k=1,n
c calcul du vecteur contenant la keme colonne de h
      lk=k
      km1=k-1
      if (k .ge. 2) then
      do 9610 l=1,km1
      tabaux(l)=h(lk)
      lk=lk + (n-l)
9610  continue
      endif
      do 9620 l=k,n
      tabaux(l)=h(lk)
      lk=lk+1
9620  continue
c
      call prosca(n,xx,tabaux,aux2,izs,rzs,dzs)
      do 9630 l=1,n
      tabaux(l)=zero
9630  continue
      tabaux(k)=un
      call prosca(n,tabaux,d,sscaek,izs,rzs,dzs)
      kj=k-n
      do 9700 j=1,k
      kj=kj+n-j+1
      h(kj)=h(kj) - ( (aux2 - aux1*sscaek)*d(j) + sscaek*gg(j) )/eta
9700  continue
9800  continue
      endif
c
c  *****************************************************
c  phase x :calcul de la nouvelle direction de recherche
c  *****************************************************
c
      if (gc) then
c xx contient -d
      do 10000 j=1,n
      d(j)=-xx(j)
10000 continue
c
       else
c cas de la methode de quasi newton
c la nouvelle direction d egale -(h * g)
      call fmuls1(n,h,g,d)
      do 10100 j=1,n
      d(j)=-d(j)
10100 continue
      endif
c
c test:la direction de recherche est elle bien de descente
      call prosca(n,d,g,dg1,izs,rzs,dzs)
      if (dg1 .ge. zero) then
      info=7
      if (imp .gt. 1) write(io,10101) dg1
      goto 99999
      else
      goto 4000
      endif
c
c retour au programme appelant
99999 niter=iter
      nsim=ntotap
      if (i .eq. 0) then
      eps=normg0
      else
      eps=normg
      endif
10101 format(40h n1gc2a   erreur dans la hessienne   dg=,d9.2)
      end