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
| #include <math.h>
#include <time.h>
#include <stdbool.h>
#include <stdio.h>
#define NBRE_FOURNISSEURS (4)
#define NBRE_DEMANDE (50)
#define RANDOMIZE() (srand((unsigned)time(NULL)))
typedef struct {
int capacite_max;
} t_fournisseurs;
void initialise_population();
int myrand(int, int);
int main()
{
t_fournisseurs tableau[NBRE_FOURNISSEURS];
tableau[0].capacite_max = 10;
tableau[1].capacite_max = 20;
tableau[2].capacite_max = 30;
tableau[3].capacite_max = 40;
RANDOMIZE();
initialise_population();
}
int myrand(int min, int max)
{
return rand()%(min-max+1)+min;
}
/*
******************************************
* initialise_population *
* Creates and initialize a population *
******************************************
*/
void initialise_population()
{
int j;
int pos;
int s=0;
int tableau[NBRE_FOURNISSEURS];
while (s!= NBRE_DEMANDE)
{
for ( j = 0 ; j < NBRE_FOURNISSEURS ; j++)
tableau [j] = 0;
s = 0;
for ( j = 0 ; j < NBRE_FOURNISSEURS ; j++ )
{
pos = myrand(0, NBRE_FOURNISSEURS);
if (tableau[pos] == 0)
{
if ( s == NBRE_DEMANDE )
tableau[j] = 0;
else
{
if ( tableau[j] <= NBRE_DEMANDE - s )
{
// tableau[j] = myrand(0, (tableau[j].capacite_max));
tableau[j] = myrand(0, 40);
}
else
{
tableau[j] = myrand(0, (NBRE_DEMANDE - s));
}
}
s+= tableau[j] ;
}
}
}
// Affichage de ses valeurs pour vérifier
for (j = 0 ; j < NBRE_FOURNISSEURS ; j++)
{
printf("%d\n", tableau[j]);
}
printf("\n");
} |
Partager