Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. //
  2. // main.c
  3. // Rechop
  4. //
  5. // Created by Thomas Werland on 18/12/2017.
  6. // Copyright © 2017 Thomas Werland. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <limits.h>
  12. //STRUCTURE========================================================================================
  13. struct Client{
  14.  
  15. //Boolean
  16. int haveBeenExcluded;
  17. //Boolean
  18. int inTreatment;
  19. int duration;
  20. int clientNumber;
  21. int stationNumber;
  22. float deltaEnter;
  23. float serviceLeft;
  24.  
  25.  
  26.  
  27. };
  28.  
  29. struct Station{
  30. //Boolean
  31. int isOccupied;
  32. //Boolean
  33. int isExpress;
  34.  
  35. int stationNumber;
  36. float timeOccupied;
  37. float timeFree;
  38.  
  39. };
  40.  
  41. typedef struct clientsInTreatment clientsInTreatment;
  42. struct clientsInTreatment{
  43. struct Client client;
  44. struct clientsInTreatment *next;
  45. };
  46. typedef clientsInTreatment* clients;
  47.  
  48. //==============================================================================================================
  49.  
  50.  
  51. //CONST===========================================================================================================
  52. const int sMin = 5;
  53. const int sMax = 7*6;
  54. const float lambda = 2.2;
  55. const int minutes = 960;
  56. const float deltaT = 0.25;
  57. const float priceExpress = 37.5;
  58. const float priceNormal = 25.5;
  59. const float priceEjected = 42.5;
  60. const float priceLineExpress = 30;
  61. const float priceLineNormal = 30;
  62. const float priceStationNoOccuped = 18;
  63. const float priceTransfer = 5;
  64. const int a = 69069;
  65. const int c = 0;
  66. const int arraySize = 6;
  67. //================================================================================================================
  68.  
  69.  
  70. //Funciton declaration=============================================================================================
  71. void deleteClientInTreatment(int clientNumber, clients racine);
  72. int getClientTime(float lambda, float deltaT, int nbRep[], unsigned int *x0);
  73. int isClientHere(float lambda, float deltaT, unsigned int *x0);
  74. int forcedProbability(int nbRep[], unsigned int *x0);
  75. double generateDouble(unsigned int *x0);
  76. //====================================================================================================================================
  77. int main(int argc, const char * argv[]) {
  78. //probability of client or no client computing
  79. //float pr1 = lambda*deltaT;
  80. //float pr0 = 1 - pr1;
  81. for (int i = 0; i<sMax; i++){
  82. unsigned int x0 = 292;
  83. int nbRep[] = {20,30,20,10,18,24};
  84. //Number of clients that have arrived
  85. int numberClient = 0;
  86. double costClient = 0;
  87. //Initializing tab with stations accordingly to sMax
  88. struct Station stationTab[sMax];
  89. for (int j = 0; j<i; j++){
  90. struct Station station;
  91.  
  92. if (j==0||j==1){
  93. station.isExpress = 1;
  94. }else{
  95. station.isExpress =0;
  96. }
  97. //init a -deltat ?
  98. station.isOccupied = 0;
  99. station.timeFree = 0;
  100. station.timeOccupied = 0;
  101. station.stationNumber = j;
  102. stationTab[j] = station;
  103. }
  104.  
  105. //printf("%d\n", stationTab[2].stationNumber);
  106. float timer = 0;
  107.  
  108.  
  109. //Starting simulation
  110. clients clientsInTreatmentRacine;
  111. clientsInTreatmentRacine = NULL;
  112.  
  113. while (timer <= 960/deltaT){
  114.  
  115. //update on arrival in lines
  116.  
  117.  
  118.  
  119.  
  120.  
  121. //is any client done
  122. //+update on client and occupied stations
  123. clients clientInTreatment = clientsInTreatmentRacine;
  124. if (clientInTreatment !=NULL){
  125. while (clientInTreatment->next != NULL){
  126. //printf("passe");
  127. clientInTreatment = clientInTreatment->next;
  128. if(clientInTreatment->client.serviceLeft == 0){
  129.  
  130. stationTab[clientInTreatment->client.stationNumber].isOccupied = 0;
  131. //delete from list
  132. deleteClientInTreatment(clientInTreatment->client.clientNumber, clientsInTreatmentRacine);
  133.  
  134. }else{
  135. clientInTreatment->client.serviceLeft = clientInTreatment->client.serviceLeft - deltaT;
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. //update case of expressLine and normalLine
  143. if(clientInTreatment->client.duration <= 3){
  144. costClient += priceExpress/240;
  145. }else{
  146. costClient +=priceNormal/240;
  147. }
  148.  
  149. stationTab[clientInTreatment->client.stationNumber].timeOccupied +=deltaT;
  150. }
  151. }
  152.  
  153.  
  154. //update on not occupied stations
  155. for (int j =0; j<i; j++){
  156. clientInTreatment = clientsInTreatmentRacine;
  157. int isOccupied = 0;
  158. if (clientInTreatment !=NULL){
  159. while(clientInTreatment->next != NULL){
  160. if(clientInTreatment->client.stationNumber == j){
  161. isOccupied =1;
  162. }
  163. }
  164. }
  165.  
  166. if(isOccupied ==0){
  167. stationTab[j].timeFree +=deltaT;
  168. //get a client here
  169. }
  170. }
  171.  
  172.  
  173.  
  174. //mettre ici le traitement d'un clien
  175. //pas oubleir d'incrementer numberClient
  176.  
  177.  
  178. //Client or no client
  179. int client =getClientTime(lambda, deltaT, nbRep, &x0);
  180. if (client ==0){
  181.  
  182. }else{
  183. numberClient++;
  184. struct Client newClient;
  185. newClient.clientNumber = numberClient;
  186. newClient.deltaEnter = timer;
  187. newClient.duration = client;
  188. newClient.haveBeenExcluded = 0;
  189. newClient.inTreatment = 0;
  190. newClient.serviceLeft = client;
  191. //newClient.stationNumber = void;
  192.  
  193. //add to file list
  194. printf("%d\n",newClient.clientNumber);
  195. }
  196.  
  197.  
  198. timer = timer + deltaT;
  199.  
  200. }
  201. }
  202. //printf("%f\n", timer);
  203. return 0;
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210. int fact(int x){
  211. int res=1;
  212. for(;x>1;--x)res*=x; // Ca sert à rien de multiplier par 1 à la fin
  213. return res;
  214. }
  215.  
  216. void deleteClientInTreatment(int clientNumber, clients racine){
  217. clients pile = racine;
  218. while(pile->client.clientNumber!=clientNumber || pile->next!=NULL){
  219. clients pile2 = pile;
  220. pile2 = pile;
  221. pile = pile->next;
  222. if(pile->client.clientNumber!=clientNumber==clientNumber){
  223. pile2->next = pile->next;
  224. }
  225. }
  226.  
  227.  
  228.  
  229.  
  230. }
  231.  
  232. double generateDouble(unsigned int *x0) {
  233. *x0 = a * (*x0) + c;
  234. return *x0 / (UINT_MAX + 1.);
  235. }
  236.  
  237. int forcedProbability(int nbRep[], unsigned int *x0) {
  238. int totalRep = 0;
  239. int i;
  240. int x = 0;
  241. double rand;
  242.  
  243. for (i = 0; i < arraySize; i++) {
  244. totalRep += nbRep[i];
  245. }
  246.  
  247. rand = generateDouble(x0);
  248. for (i = 0; i < arraySize; i++) {
  249. x += nbRep[i];
  250. double probTest = (double)x / (double)totalRep;
  251. if (probTest > rand) {
  252. return (i+1);
  253. }
  254. }
  255. return 0;
  256. }
  257.  
  258. int isClientHere(float lambda, float deltaT, unsigned int *x0) {
  259. double rand = generateDouble(x0);
  260. double probHere = lambda * deltaT;
  261.  
  262. if (rand < probHere) {
  263. return 1;
  264. } else {
  265. return 0;
  266. }
  267. }
  268.  
  269. int getClientTime(float lambda, float deltaT, int nbRep[], unsigned int *x0) {
  270. if (isClientHere(lambda, deltaT, x0)) {
  271. return forcedProbability(nbRep, x0);
  272. } else {
  273. return 0;
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement