Bonjour,
Quelqu'un pourrait m'aider à corriger ce code ?


Modélisation de la croissance de la population par l'application logistique:

Xn+1=AXn(1-Xn)=f(Xn) (A paramètre de non-linéarité, 0<Xn<1 n elem N)
Les fonctions utiles

1.Generation des valeurs de Xn
2.Transposer d'une matrice
3.Produit d'une matrice
4.Diagonalisation d'une matrice NXN
5.Fonction cosinus/sinus
6.Valeur propre
7.Calcul du nombre de couche d'entrée en RNA
8.Calcul d'erreur d'approximation moyenne
9.Apprentissage du réseau Prédiction



Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include<math.h>
 
TForm1 *Form1;
 
int itr=0;
float paramA,X[500],X1[500][500],transX1[500][500],produitX1transX1[500][500],transP[500][500],mat1[500][500],mat2[500][500],pdsortieW[500],N[500],saveN[500] ;
float A[500][500],derniereMatrice[500][500],valeurpropre[500],valeurpropre1[500],P[500][500],erreurappr[500],V[500][500],W[500][500],Sortie[500];
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
     Memo1->Text="";
     if (Edit1->Text=="")
       ShowMessage("Entrer une valeur pour le paramètre de non-linéarité");
     else {
         paramA=StrToFloat(Edit1->Text);
         X[0]=0.1;
 
         for(int i=0;i<500;i++)
         {
            X[0]=0.1;
            X[i+1]=paramA*X[i]*(1-X[i]);
            Memo1->Lines->Add("X["+IntToStr(i)+"]="+X[i]);
         }
      }  
 
}
//---------------------------------------------------------------------------
//--------------------------construction de la matrice X1 à partir de X-------------------------------------------------
 
void creer_matriceX1(float matX1[500],int dimension)
{
 
  for(int i=0;i<dimension;i++)
  {
        for(int j=0;j<dimension;j++)
        {
        if(i>=0){X1[i][0]=matX1[i];}
        else X1[i][j]=0;
        }
  }
}
 
//---------------------------------------------------------------------------
//--------------------------transposer de la matrice X1------------------------------------------------
 
 
void transposer_matriceX1(float t_X1[500][500],int dimension)
{
 for(int i=0;i<dimension;i++)
 {
        for(int j=0;j<dimension;j++)
        {
          transX1[i][j]=t_X1[j][i];
        }
 }
}
 
//---------------------------------------------------------------------------
//--------------------------produit de la matrice X1 avec la matrice transX1(COVARIANCE)-------------------------------------------------
 
 
 
void produit_matrice(float pX1[500][500],float ptransX1[500][500],int dimension)
{
for(int i=0;i<dimension;i++)
for(int j=0;j<dimension;j++)
        {
        float somme=0;
        for(int k=0;k<dimension;k++)
                {
                 somme+=pX1[i][k]*ptransX1[k][j];
                }
        produitX1transX1[i][j]=somme;
        }
}
 
 
 
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
 
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Memo1->Text="";
 creer_matriceX1(X,StrToInt(Edit2->Text));
 transposer_matriceX1(X1,StrToInt(Edit2->Text));
 produit_matrice(X1,transX1,StrToInt(Edit2->Text));
 
  for(int i=0;i<StrToInt(Edit2->Text);i++)
 {
    for(int j=0;j<StrToInt(Edit2->Text);j++)
         Memo1->Lines->Add("covariance["+  IntToStr(i)+"]["+  IntToStr(j)+"]="+produitX1transX1[i][j]);
 }
}
//---------------------------------------------------------------------------
 //-------------------------------------------------------------------------
 
 
 
 
 //---------------------ETAPE 2----------------------------------------------
 
 
 
 //-------------------------------------------------------------------------
 //-------------------------------creation de la matrice A--------------------------------------------
 
void creer_matriceA(float A1[500][500],int dimension)
{
 for(int i=0;i<dimension;i++)
     for(int j=0;j<dimension;j++)
        A1[i][j]=produitX1transX1[i][j];
 
}
 
  //------------------------------------------------------------------
   //--------maximum ligne--------------------------------------------
int maxiligne(float pA[500][500],int dimension)
{
float maximum=pA[0][1];
int maxligne=0;
for(int i=0;i<dimension;i++)
{
        for(int j=0;j<dimension;j++)
        {
                if(i!=j)
                {
                   if(pA[i][j]>=maximum)
                   {
                      maximum=pA[i][j];
                      maxligne=i;
                   }
                }
        }
}
return maxligne;
}
 
 //------------------------------------------------------------------
   //--------maximum colonne--------------------------------------------
int maxicolonne(float pA[500][500],int dimension)
{
float maximum=pA[0][1];
int maxcolonne=0;
for(int i=0;i<dimension;i++)
{
        for(int j=0;j<dimension;j++)
        {
                if(i!=j)
                {
                   if(pA[i][j]>=maximum)
                   {
                      maximum=pA[i][j];
                      maxcolonne=j;
                   }
                }
        }
}
return maxcolonne;
}
 
  //-------------------------------------------------------------------------
 //---------cosinus-----------------------------------
double get_cos(double c,double b)
{
double cos_teta;
cos_teta=sqrt(0.5*(1+(b/sqrt(c*c+b*b))));
return cos_teta;
}
//-------------------------------------------------------------------------
//---------sinus----------
double get_sin(double c,double b)
{
double sin_teta,cos_teta;
cos_teta=sqrt(0.5*(1+(b/sqrt(c*c+b*b))));
sin_teta=c/(2*cos_teta*sqrt(c*c+b*b));
return sin_teta;
}
 
//-------------------------------------------------------------------------
 //---------creation de P-----------------------------------
 
void creer_matriceP(float pP[500][500],float val1,float val2,int lignmax,int colonnmax,int dimension)
{
 
for(int i=0;i<dimension;i++)
{
        for(int j=0;j<dimension;j++)
        {
            if((i==lignmax)&&(j==colonnmax))
                pP[i][j]=-val2;
            if((i==lignmax)&&(j==lignmax))
                pP[i][j]=val1;
            if((i==colonnmax)&&(j==colonnmax))
                pP[i][j]=val1;
            if((i==colonnmax)&&(j==lignmax))
                pP[i][j]=val2;
        }
}
for(int i=0;i<dimension;i++)
{
    for(int j=0;j<dimension;j++)
    {
        if(i==j)
        {
           if(pP[i][j]==0)
              pP[i][i]=1;
        }
    }
}
}
 
//-------------------------------------------------------------------------
 //---------transposer de P-----------------------------------
 
 
void transposer_matriceP(float ptransP[500][500],float pP[500][500],int dimension)
{
 
  for(int i=0;i<dimension;i++)
    for(int j=0;j<dimension;j++)
        ptransP[i][j]=pP[j][i];
}
 
 
 //---------------------------------------------------------------------------------------------------
//-----------produit de matrices  P*Pt-------------------------------------------------------------------
void produit_mat1(float pA[500][500],float ptransP[500][500],int dimension)
 {
 
  for(int i=0;i<dimension;i++)
  {
    for(int j=0;j<dimension;j++)
    {
        float somme=0;
        for(int k=0;k<dimension;k++)
            somme+=pA[i][k]*ptransP[k][j];
        mat1[i][j]=somme;
    }
  }
 }
 
 void produit_mat2(float pP[500][500],float pmat1[500][500],int dimension)
 {
 
  for(int i=0;i<dimension;i++)
  {
    for(int j=0;j<dimension;j++)
    {
  float somme=0;
      for(int k=0;k<dimension;k++)
      {
      somme+=pP[i][k]*pmat1[k][j];
      }
       mat2[i][j]=somme;
    }
  }
 }
 
//-------------------------------------------------------------------------
 //-------------------------------Diagonalisation--------------------------------------------
 
 
double diagonalisation(float pA[500][500],int dimension)
{
   float lnlnP;
   float cnlnP;
   double a,b;
 
 
 
   int lnmax=maxiligne(pA,dimension);
   int cnmax=maxicolonne(pA,dimension);
 
   a=2*pA[lnmax][lnmax];
   b=pA[lnmax][lnmax]-pA[cnmax][cnmax];
   if(pA[lnmax][lnmax]!=pA[cnmax][cnmax])
   {
        lnlnP=get_cos(a,b);
        lnlnP=get_sin(a,b);
   }
   else
   {
        lnlnP=sqrt(2)/2;
        cnlnP=sqrt(2)/2;
   }
   creer_matriceP(P,lnlnP,cnlnP,lnmax,cnmax,dimension);
   transposer_matriceP(transP,P,dimension);
   produit_mat1(A,transP,dimension);
   produit_mat2(P,mat1,dimension);
 
   return(0);
 
}
 
//-------------------------------------------------------------------------
 //-------------------------------Valeur propre-------------------------------------------
 
 
 
void __fastcall TForm1::Button3Click(TObject *Sender)
{
   itr++;
   creer_matriceA(A,StrToInt(Edit2->Text));
   diagonalisation(mat2,StrToInt(Edit2->Text));
   Memo1->Lines->Add("              ");
   Memo1->Lines->Add("              ");
   Memo1->Lines->Add("Valeur propre________________________");
   Memo1->Lines->Add(IntToStr(itr)+"iteration");
   for(int i=0;i<StrToInt(Edit2->Text);i++)
   {
       valeurpropre[i]=mat2[i][i];
       Memo1->Lines->Add("valeurpropre["+IntToStr(i)+"]="+valeurpropre[i]);
   }
}
 
//---------------------------------------------------------------------------
 //-----------------------------Ordre décroissant----------------------------------------------
 
void __fastcall TForm1::Button4Click(TObject *Sender)
{
   Memo1->Text="";
   float save;
   for(int i=0;i<StrToInt(Edit2->Text);i++)
       valeurpropre1[i]=valeurpropre[i];
   for(int i=0;i<StrToInt(Edit2->Text);i++)
   {
       for(int j=i+1;j<StrToInt(Edit2->Text);j++)
       {
           if(valeurpropre1[i]<valeurpropre1[j])
           {
              save=valeurpropre1[j];
              valeurpropre1[j]=valeurpropre1[i];
              valeurpropre1[i]=save;
           }
        }
   }
   for(int i=0;i<StrToInt(Edit2->Text);i++)
       Memo1->Lines->Add(valeurpropre1[i]);
 
}
//---------------------------------------------------------------------------
 
 
int Nombredecouchedentree(float perreurappr[500],int dimension)
{
   for (int i=0;i<dimension;i++)
        perreurappr[i]=sqrt(valeurpropre1[i]+1);
 
   float c1,c2;
   float error;
   int n;
   c1=perreurappr[0];
   c2=perreurappr[1];
   for(int i=2;i<dimension;i++)
   {
       error=c2-c1;
       if(error<0.01){n=i;return (n-1);}
       else
       {
          c1=c2;
          c2=perreurappr[i];
       }
   }
}
 
 
void __fastcall TForm1::Button5Click(TObject *Sender)
{
   Memo1->Text="";
   Memo1->Lines->Add("Nombre de couche d'entrée : "+IntToStr(Nombredecouchedentree(erreurappr,StrToInt(Edit2->Text))));
   for(int i=0;i<StrToInt(Edit2->Text);i++)
       Memo1->Lines->Add("Erreur d'aproximation moyenne : "+FloatToStr(erreurappr[i]));
}
//---------------------------------------------------------------------------
 
//---------------------------------------------------------------------------
//--------------------------------Motif d'entrée-------------------------------------------
 
void motifdentree(int valeur)
{
 
for(int i=1;i<=2;i++)
        {
        V[1][i]=X[valeur+i-1];
        Form1->Memo1->Lines->Add("Motif d'entrée V[1]["+IntToStr(i)+"]="+V[1][i]);
        }
}
//---------------------------------------------------------------------------
//--------------------------------Poidsd'entrée-------------------------------------------
 
void poiddentree(int nbdunitecache)
{
 
        for(int i=1;i<=nbdunitecache;i++)
            for(int j=1;j<=2;j++)
            {
                W[i][j]=0.5*j/1.5;
                Form1->Memo1->Lines->Add("poids d'entrée W["+IntToStr(i)+"]["+IntToStr(j)+"]="+W[i][j]);
            }
}
 
//---------------------------------------------------------------------------
//--------------------------------Poidsdesortie-------------------------------------------
 
 
void poiddesortie(int nbdunitecache)
{
 
   for(int i=1;i<=nbdunitecache;i++)
        {
           pdsortieW[i]=0.2*i/0.7;
           Form1->Memo1->Lines->Add("poids de sortie["+IntToStr(i)+"]="+pdsortieW[i]);
        }
}
//---------------------------------------------------------------------------
//--------------------------------fonction d'activation-------------------------------------------
float fonctionG(float x)
{
    float y;
    y=1/(1+exp(-x));
    return y;
}
//---------------------------------------------------------------------------
//--------------------------------fonction d'activation (Primitive)-------------------------------------------
 
float primitiveG(float x)
{
     float y;
     y=exp(-x)/(1+exp(-x));
     return y;
}
 
 //---------------------------------------------------------------------------
//---------------addition entree--------------
float additionentree(float V[500][500],float W[500][500],int valeur)
{
    float sum=0;
    for(int i=1;i<=2;i++)
        sum+=W[valeur][i]*V[1][i];
    return sum;
}
 
 //---------------------------------------------------------------------------
//---------------addition sortie--------------
 
float additionsortie(float V[500][500],float pdsortieW[500],int valeur)
{
    float sum=0;
    for(int i=1;i<=valeur;i++)
        sum+=pdsortieW[i]*V[2][i];
    return sum;
}
 
 
//---------------------------------------------------------------------------
//--------------------------------propagation vers l'avant-------------------------------------------
 
 
void propagationverslavant(float V[500][500],float W[500][500],int nbdunitecache)
{
     float h[500];
     for(int i=1;i<=nbdunitecache;i++)
     {
        h[i]=additionentree(V,W,i);
        V[2][i]=fonctionG(h[i]*h[i]);
        Form1->Memo1->Lines->Add("pro vers lavant V[2]["+IntToStr(i)+"]="+FloatToStr(V[2][i]));
     }
}
 
 //---------------------------------------------------------------------------
//--------------------------------Sortie prédite-------------------------------------------
 
 
 
void sortiepredite(float V[500][500],float W[500][500],int nbunitecache,int valeur)
{
  V[3][1]=additionsortie(V,pdsortieW,nbunitecache);
  Sortie[valeur+2]=V[3][1];
  Form1->Memo1->Lines->Add("V[3][1]="+FloatToStr(V[3][1]));
}
 
//---------------------------------------------------------------------------
//--------------------------------Variance-------------------------------------------
 
 
float Xvariance(float X[500],float W[500][500],int dimension)
{
   float moyenne,variance,sum=0;
   float sum1=0;
   for(int i=1;i<=dimension;i++)
       sum1+=X[i-1]*W[1][i];
   moyenne=sum1/10;
   for(int k=1;k<dimension;k++)
       sum+=W[1][k]*(X[k-1]-moyenne)*(X[k-1]-moyenne);
   variance=sum/10;
   return(variance);
}
 
//---------------------------------------------------------------------------
//--------------------------------Calcul du nombre d'unité caché-------------------------------------------
 
 
int calculnbrdUcache(float Sortie[50],float X[500],float W[500][500],int dimension,int nbunitecache1)
{
  double sum=0,resultat;
  float save;
  int nombredunite;
  for(int i=3;i<=dimension;i++)
      sum+=(X[i]-Sortie[i])*(X[i]-Sortie[i]);
  resultat=sum/(8*Xvariance(X,W,dimension)*Xvariance(X,W,dimension));
  N[nbunitecache1]=resultat;
  for(int i=1;i<=dimension-2;i++)
      saveN[i]=N[i];
  for(int i=1;i<=dimension-2;i++)
  {
        for(int j=i+1;j<dimension-2;j++)
        {
            if(saveN[i]<saveN[j])
            {
               save=saveN[j];
               saveN[j]=saveN[i];
               saveN[i]=save;
               nombredunite=i+1;
            }
        }
  }
  return nombredunite;
}
 
void __fastcall TForm1::Button6Click(TObject *Sender)
{
   Memo1->Clear();
   for(int nombredunitecache=1;nombredunitecache<=StrToInt(Edit2->Text);nombredunitecache++)
   {
       Form1->Memo1->Lines->Add("------------------nombre couche="+IntToStr(nombredunitecache));
       for(int j=0;j<10;j++){
           motifdentree(j);
           poiddentree(nombredunitecache);
           poiddesortie(nombredunitecache);
           propagationverslavant(V,W,nombredunitecache);
           sortiepredite(V,W,nombredunitecache,j);
       }
       Memo1->Lines->Add("Nombre d'unité caché="+IntToStr(calculnbrdUcache(Sortie,X,W,StrToInt(Edit2->Text),nombredunitecache)));
   }
 
}
 
//---------------------------------------------------------------------------
Merci de m'aider.