Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // Rechop
- //
- // Created by Thomas Werland on 18/12/2017.
- // Copyright © 2017 Thomas Werland. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- //STRUCTURE========================================================================================
- struct Client{
- //Boolean
- int haveBeenExcluded;
- //Boolean
- int inTreatment;
- int duration;
- int clientNumber;
- int stationNumber;
- float deltaEnter;
- float serviceLeft;
- };
- struct Station{
- //Boolean
- int isOccupied;
- //Boolean
- int isExpress;
- int stationNumber;
- float timeOccupied;
- float timeFree;
- };
- typedef struct clientsInTreatment clientsInTreatment;
- struct clientsInTreatment{
- struct Client client;
- struct clientsInTreatment *next;
- };
- typedef clientsInTreatment* clients;
- //==============================================================================================================
- //CONST===========================================================================================================
- const int sMin = 5;
- const int sMax = 7*6;
- const float lambda = 2.2;
- const int minutes = 960;
- const float deltaT = 0.25;
- const float priceExpress = 37.5;
- const float priceNormal = 25.5;
- const float priceEjected = 42.5;
- const float priceLineExpress = 30;
- const float priceLineNormal = 30;
- const float priceStationNoOccuped = 18;
- const float priceTransfer = 5;
- const int a = 69069;
- const int c = 0;
- const int arraySize = 6;
- //================================================================================================================
- //Funciton declaration=============================================================================================
- void deleteClientInTreatment(int clientNumber, clients racine);
- int getClientTime(float lambda, float deltaT, int nbRep[], unsigned int *x0);
- int isClientHere(float lambda, float deltaT, unsigned int *x0);
- int forcedProbability(int nbRep[], unsigned int *x0);
- double generateDouble(unsigned int *x0);
- //====================================================================================================================================
- int main(int argc, const char * argv[]) {
- //probability of client or no client computing
- //float pr1 = lambda*deltaT;
- //float pr0 = 1 - pr1;
- for (int i = 0; i<sMax; i++){
- unsigned int x0 = 292;
- int nbRep[] = {20,30,20,10,18,24};
- //Number of clients that have arrived
- int numberClient = 0;
- double costClient = 0;
- //Initializing tab with stations accordingly to sMax
- struct Station stationTab[sMax];
- for (int j = 0; j<i; j++){
- struct Station station;
- if (j==0||j==1){
- station.isExpress = 1;
- }else{
- station.isExpress =0;
- }
- //init a -deltat ?
- station.isOccupied = 0;
- station.timeFree = 0;
- station.timeOccupied = 0;
- station.stationNumber = j;
- stationTab[j] = station;
- }
- //printf("%d\n", stationTab[2].stationNumber);
- float timer = 0;
- //Starting simulation
- clients clientsInTreatmentRacine;
- clientsInTreatmentRacine = NULL;
- while (timer <= 960/deltaT){
- //update on arrival in lines
- //is any client done
- //+update on client and occupied stations
- clients clientInTreatment = clientsInTreatmentRacine;
- if (clientInTreatment !=NULL){
- while (clientInTreatment->next != NULL){
- //printf("passe");
- clientInTreatment = clientInTreatment->next;
- if(clientInTreatment->client.serviceLeft == 0){
- stationTab[clientInTreatment->client.stationNumber].isOccupied = 0;
- //delete from list
- deleteClientInTreatment(clientInTreatment->client.clientNumber, clientsInTreatmentRacine);
- }else{
- clientInTreatment->client.serviceLeft = clientInTreatment->client.serviceLeft - deltaT;
- }
- //update case of expressLine and normalLine
- if(clientInTreatment->client.duration <= 3){
- costClient += priceExpress/240;
- }else{
- costClient +=priceNormal/240;
- }
- stationTab[clientInTreatment->client.stationNumber].timeOccupied +=deltaT;
- }
- }
- //update on not occupied stations
- for (int j =0; j<i; j++){
- clientInTreatment = clientsInTreatmentRacine;
- int isOccupied = 0;
- if (clientInTreatment !=NULL){
- while(clientInTreatment->next != NULL){
- if(clientInTreatment->client.stationNumber == j){
- isOccupied =1;
- }
- }
- }
- if(isOccupied ==0){
- stationTab[j].timeFree +=deltaT;
- //get a client here
- }
- }
- //mettre ici le traitement d'un clien
- //pas oubleir d'incrementer numberClient
- //Client or no client
- int client =getClientTime(lambda, deltaT, nbRep, &x0);
- if (client ==0){
- }else{
- numberClient++;
- struct Client newClient;
- newClient.clientNumber = numberClient;
- newClient.deltaEnter = timer;
- newClient.duration = client;
- newClient.haveBeenExcluded = 0;
- newClient.inTreatment = 0;
- newClient.serviceLeft = client;
- //newClient.stationNumber = void;
- //add to file list
- printf("%d\n",newClient.clientNumber);
- }
- timer = timer + deltaT;
- }
- }
- //printf("%f\n", timer);
- return 0;
- }
- int fact(int x){
- int res=1;
- for(;x>1;--x)res*=x; // Ca sert à rien de multiplier par 1 à la fin
- return res;
- }
- void deleteClientInTreatment(int clientNumber, clients racine){
- clients pile = racine;
- while(pile->client.clientNumber!=clientNumber || pile->next!=NULL){
- clients pile2 = pile;
- pile2 = pile;
- pile = pile->next;
- if(pile->client.clientNumber!=clientNumber==clientNumber){
- pile2->next = pile->next;
- }
- }
- }
- double generateDouble(unsigned int *x0) {
- *x0 = a * (*x0) + c;
- return *x0 / (UINT_MAX + 1.);
- }
- int forcedProbability(int nbRep[], unsigned int *x0) {
- int totalRep = 0;
- int i;
- int x = 0;
- double rand;
- for (i = 0; i < arraySize; i++) {
- totalRep += nbRep[i];
- }
- rand = generateDouble(x0);
- for (i = 0; i < arraySize; i++) {
- x += nbRep[i];
- double probTest = (double)x / (double)totalRep;
- if (probTest > rand) {
- return (i+1);
- }
- }
- return 0;
- }
- int isClientHere(float lambda, float deltaT, unsigned int *x0) {
- double rand = generateDouble(x0);
- double probHere = lambda * deltaT;
- if (rand < probHere) {
- return 1;
- } else {
- return 0;
- }
- }
- int getClientTime(float lambda, float deltaT, int nbRep[], unsigned int *x0) {
- if (isClientHere(lambda, deltaT, x0)) {
- return forcedProbability(nbRep, x0);
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement