Sarahi Moran
/
jdijdfie
prueba
Diff: main.cpp
- Revision:
- 1:ed464543ca57
- Parent:
- 0:c344a92d3ca2
--- a/main.cpp Fri Dec 04 03:24:26 2015 +0000 +++ b/main.cpp Fri Dec 04 23:15:18 2015 +0000 @@ -3,7 +3,7 @@ Serial pc(USBTX, USBRX); // tx, rx DigitalOut myled(LED2); DigitalOut led_a(LED3); -AnalogOut pin_a(A0); + #define u1 8 #define M1 1 @@ -13,7 +13,7 @@ #define M3 1 #define pQ3Q1 0.1 // probability of sending Q3 output to the Q1 input #define pQ3Out 0.9 // probability of delivering Q3 output as system output -#define tf 10000 +#define tf 50 #define lambda 10 #define pQ1I_Q2I 0.5 #define sz_a 1000 @@ -27,7 +27,7 @@ float t_events_Q3[sz_a]= {0}; // time stamps for the k events int k=1; // event number -double SI = 0; +double SI = 0.1; double Q1I1 = tf; double Q1I2 = 0; @@ -60,7 +60,10 @@ double min_3(double x, double y, double z); int max_2(int x, int y); double max_3(double x, double y, double z); -float var_rand(int x); +double var_rand_q1(int x); +double var_rand_s(int x); +double var_rand_q2(int x); +double var_rand_q3(int x); int Q1k; int Q2k; @@ -68,7 +71,10 @@ - +uint32_t x_i_s=7; //semilla aleatoria +uint32_t x_i_q1=11; +uint32_t x_i_q2= 17; +uint32_t x_i_q3=3; int i=0; int main() { @@ -79,7 +85,8 @@ // System input - SI = var_rand(lambda); + SI = var_rand_s(lambda); + SI= SI/100; // Q1 @@ -93,13 +100,14 @@ //printf("Q1_lambda: %f \n", Q1_lambda[k]); if(Q1[k] > 0){ - Q1O = u1*M1; // service time in the queue - Q1O= var_rand(Q1O); + int q1O= u1*M1; // service time in the queue + Q1O= var_rand_q1(q1O); + Q1O= Q1O/100; } else{ Q1O= min_2(Q1I1,Q1I2)+1.0; } - //printf("Q1O: %lf \n", Q1O); + printf("Q1O: %lf \n", Q1O); // Update queue Q1 t_events_Q1[k]=(t+min_3(Q1I1,Q1I2,Q1O)); @@ -125,13 +133,14 @@ //printf("Q2_lambda :%f \n",Q2_lambda[k]); if(Q2[k] > 0){ - Q2O = u2*M2; // service time in the queue - Q2O= var_rand(Q2O); + int q2O = u2*M2; // service time in the queue + Q2O= var_rand_q2(q2O); + Q2O= Q2O/100; } else{ Q2O = Q2I1 + 1.0; } - //printf("Q20 :%f \n",Q2O); + printf("Q20 :%f \n",Q2O); // Update queue Q2 t_events_Q2[k]=(t+min_2(Q2I1, Q2O)); @@ -159,14 +168,16 @@ //printf("Q3_lambda: %f \n",Q3_lambda[k]); if(Q3[k] > 0){ - Q3O = u3*M3; // service time in the queue - Q3O= var_rand(Q3O); + int q3O = u3*M3; // service time in the queue + Q3O= var_rand_q3(q3O); + // printf("Q3O_ant %lf", Q3O); + Q3O= Q3O/100; } else{ Q3O = min_2(Q1O, Q2O) + 1.0; } - //printf("Q3O: %lf \n",Q3O); + printf("Q3O: %lf \n",Q3O); // Update queue Q3 t_events_Q3[k]= t+min_3(Q3I1, Q3I2, Q3O); //printf("Q3[k]: %d \n", Q3[k]); @@ -182,7 +193,7 @@ //printf("Q3[k]: %d \n",Q3[k+1]); eventtime_Q3 = min_3(Q3I1, Q3I2, Q3O); t_Q3 = t; - t_Q3 = t_Q3 + eventtime_Q2; + t_Q3 = t_Q3 + eventtime_Q3; // System output SO = Q3O/pQ3Out; @@ -192,64 +203,64 @@ k=k+1; myled=1; - wait(0.5); + wait(0.02); } led_a=0; - wait(1); + wait(.5); pc.printf("k="); pc.printf("%d\r\n",k); wait(.5); pc.printf("Q1\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%d, ",Q1[i]); } pc.printf("\n Q2\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%d, ",Q2[i]); } pc.printf("\n Q3\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%d, ",Q3[i]); } pc.printf("\n Q1_lambda\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",Q1_lambda[i]); } pc.printf("\n Q2_lambda\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",Q2_lambda[i]); } pc.printf("\n Q3_lambda\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",Q3_lambda[i]); } pc.printf("\n t_events_Q1\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",t_events_Q1[i]); } pc.printf("\n t_events_Q2\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",t_events_Q2[i]); } pc.printf("\n t_events_Q3\n"); - for (i=0; i<=k; i++){ + for (i=0; i<k; i++){ pc.printf("%f, ",t_events_Q3[i]); } @@ -315,18 +326,78 @@ return max; } -float var_rand(int x){ - int a = 40692; - int m=2147483399; - int x_i=rand(); //semilla aleatoria - float x_i1=(a*x_i) % m; - float U=(x_i1)/m; //Uniforme [0,1] +double var_rand_s(int x){ + int32_t a_s = 40014; + int32_t m_s=2147283563; + double exprnd_s; + int32_t x_i1_s=(a_s*x_i_s) % m_s; + x_i_s=x_i1_s; + double U_s= float(x_i1_s)/float(m_s); //Uniforme [0,1] + + + if(U_s>=0.2 && U_s<=0.9){ + exprnd_s=-1.0*float(x)*log(U_s); + } + else { + x_i1_s=(a_s*x_i_s) % m_s; + U_s= float(x_i1_s)/float(m_s); + } + // printf("exprnd_s: %lf", exprnd_s); + return exprnd_s; + } + +double var_rand_q1(int x){ + int32_t a_q1 = 40014; + int32_t m_q1=2147283563; + double exprnd_q1; + int32_t x_i1_q1=(a_q1*x_i_q1) % m_q1; + x_i_q1=x_i1_q1; + double U_q1= float(x_i1_q1)/float(m_q1); //Uniforme [0,1] + - float exprnd=-1*x*log(U); - printf ("VAL x_i: %d \n",x_i); - printf ("VAL x_i1: %f \n",x_i1); - printf ("VAL U: %f \n", U); - printf ("VAL exprnd: %f \n", exprnd); + if(U_q1>=0.4 && U_q1<=0.9){ + exprnd_q1=-1.0*float(x)*log(U_q1); + } + else { + x_i1_q1=(a_q1*x_i_q1) % m_q1; + U_q1= float(x_i1_q1)/float(m_q1); + } + return exprnd_q1; + } + + double var_rand_q2(int x){ + int32_t a_q2 = 40014; + int32_t m_q2=2147283563; + double exprnd_q2; + int32_t x_i1_q2=(a_q2*x_i_q2) % m_q2; + x_i_q2=x_i1_q2; + double U_q2= float(x_i1_q2)/float(m_q2); //Uniforme [0,1] - return exprnd; + if(U_q2>=0.4 && U_q2<=0.9){ + exprnd_q2=-1.0*float(x)*log(U_q2); + } + else { + x_i1_q2=(a_q2*x_i_q2) % m_q2; + U_q2= float(x_i1_q2)/float(m_q2); + } + return exprnd_q2; + } + + double var_rand_q3(int x){ + int32_t a_q3 = 40014; + int32_t m_q3=2147283563; + double exprnd_q3; + int32_t x_i1_q3=(a_q3*x_i_q3) % m_q3; + x_i_q3=x_i1_q3; + double U_q3= float(x_i1_q3)/float(m_q3); //Uniforme [0,1] + + if(U_q3>=0.4 && U_q3<=0.9){ + exprnd_q3=-1.0*float(x)*log(U_q3); + } + else { + x_i1_q3=(a_q3*x_i_q3) % m_q3; + U_q3= float(x_i1_q3)/float(m_q3); + } + + return exprnd_q3; } \ No newline at end of file