Advertisement
Guest User

Untitled

a guest
May 1st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.06 KB | None | 0 0
  1. <?php
  2.  
  3. define('BR', '<br/>'); #Html :)
  4. define('ACTIVAR_LOG', 0); #Mostrar mas mensajes
  5. define('HV', 999999); #High Value
  6. define('DURACION', 90000);
  7. define('CANTIDAD_DE_EXTRACCIONISTAS_ADULTOS', 1);
  8. define('CANTIDAD_DE_EXTRACCIONISTAS_DELICADOS', 1);
  9. define('IA_MIN',5); #Mínimo intervalo entre arribos, fdp lineal
  10. define('IA_MAX',15); #Máximo intervalo entre arribos, fdp lineal
  11. define('TAA_MIN', 5); #Mínimo tiempo de atención, fdp lineal
  12. define('TAA_MAX',15); #Máximo tiempo de atención, fdp lineal
  13. define('TAD_MIN', 5); #Mínimo tiempo de atención, fdp lineal
  14. define('TAD_MAX',15); #Máximo tiempo de atención, fdp lineal
  15.  
  16. $simulador = new simulador(CANTIDAD_DE_EXTRACCIONISTAS_ADULTOS, CANTIDAD_DE_EXTRACCIONISTAS_DELICADOS,DURACION);
  17. $simulador->ejecutar();
  18.  
  19. class simulador {
  20.     private $T=0; #Tiempo actual
  21.    private $X=null; #Cantidad de puestos
  22.    private $Y=null; #Cantidad de puestos
  23.    private $tiempo_final=null; #Duración de la simulación
  24.    private $TPSA = array();
  25.     private $TPSD = array();
  26.     private $TPLL = 0;
  27.     private $NSD = 0;
  28.     private $TD = 0;
  29.     private $STOD = array();
  30.     private $STSD = 0;
  31.     private $STLLD = 0;
  32.     private $NSA = 0;
  33.     private $TA = 0;
  34.     private $STOA = array();
  35.     private $ITOD = array();
  36.     private $ITOA = array();
  37.     private $STAD = 0;
  38.     private $STAA = 0;
  39.     private $STSA = 0;
  40.     private $STLLA = 0;
  41.  
  42.     /**
  43.      * @param integer $this->tiempo_final Tiempo de ejecución
  44.      * @return void
  45.      */
  46.     public function __construct($X, $Y,$tiempo_final){
  47.         $this->X = $X;
  48.         $this->Y = $Y;
  49.         $this->tiempo_final = $tiempo_final;
  50.         return $this;
  51.     }
  52.  
  53.     public function ejecutar(){
  54.         $this->inicio = microtime(true);
  55.         $this->inicializar();
  56.  
  57.         do
  58.         {
  59.             $menor_tpsa_key = $this->min_key($this->TPSA);
  60.             $menor_tpsd_key = $this->min_key($this->TPSD);
  61.  
  62.             $menor_tpsa = $this->TPSA[$menor_tpsa_key];
  63.             $menor_tpsd = $this->TPSD[$menor_tpsd_key];
  64.  
  65.             /*echo "<br>TPLL: " . $this->TPLL;
  66.             echo "Listas de TPSA: " .BR;
  67.             print_r($this->TPSA);
  68.             echo BR."Menor tpsa key: " .$menor_tpsa_key." => Menor tpsa: " . $menor_tpsa . "<br>".BR.BR;
  69.  
  70.             echo "Listas de TPSD: " .BR;
  71.             print_r($this->TPSD);
  72.             echo BR."Menor tpsd key: " .$menor_tpsd_key." => Menor tpsd: " . $menor_tpsd . "<br>".BR.BR.BR.BR.BR.BR;*/
  73.  
  74.             if ($this->TPLL <= $menor_tpsa )
  75.             {
  76.                 if ($this->TPLL <= $menor_tpsd) # LLEGADA
  77.                {
  78.                     $this->T = $this->TPLL;
  79.  
  80.                     $IA = $this->generar_intervalo_entre_arribos();
  81.  
  82.                     $this->TPLL = $this->T + $IA;
  83.  
  84.                     $tipo_paciente = $this->generar_tipo_paciente();
  85.  
  86.                     if ($tipo_paciente == "DELICADO") # PACIENTES DELICADOS
  87.                    {
  88.                         $this->NSD++;
  89.                         $this->TD++;
  90.                         $this->STLLD = $this->STLLD + $this->T;
  91.  
  92.                         $cola_hv_key = $this->buscar_cola_hv($this->TPSD);
  93.  
  94.                         if ($this->NSD <= $this->Y) {
  95.                             $TAD = $this->generar_tiempo_de_atencion_delicado();
  96.  
  97.                             $this->TPSD[$cola_hv_key] = $this->T + $TAD;
  98.                             $this->STOD[$cola_hv_key] = $this->STOD[$cola_hv_key] + ($this->T - $this->ITOD[$cola_hv_key]);
  99.                             $this->STAD = $this->STAD + $TAD;
  100.                         }
  101.                     }
  102.                     else # PACIENTES ADULTOS
  103.                    {
  104.                         $this->NSA++;
  105.                         $this->TA++;
  106.                         $this->STLLA = $this->STLLA + $this->T;
  107.  
  108.                         $cola_hv_key = $this->buscar_cola_hv($this->TPSA);
  109.  
  110.                         if ($this->NSA <= $this->X) {
  111.                             $TAA = $this->generar_tiempo_de_atencion_adulto();
  112.  
  113.                             /*echo "HAY UNA LLEGADA".BR.BR;
  114.  
  115.                             echo "TIEMPO DE ATENCION DE ADULTOS: ". $TAA.BR;
  116.                             echo "CANTIDAD DE ADULTOS: " . $this->TA.BR.BR;*/
  117.  
  118.                             $this->TPSA[$cola_hv_key] = $this->T + $TAA;
  119.                             $this->STOA[$cola_hv_key] = $this->STOA[$cola_hv_key] + ($this->T - $this->ITOA[$cola_hv_key]);
  120.                             $this->STAA = $this->STAA + $TAA;
  121.                         }
  122.                     }
  123.                 } else { # SALIDA DELICADOS
  124.                    $this->procesarSalida(null, $menor_tpsd_key);
  125.                 }
  126.             }
  127.             else # SALIDA ADULTOS
  128.            {
  129.                 $this->procesarSalida($menor_tpsa_key, $menor_tpsd_key);
  130.             }
  131.  
  132.             //$this->NT++;
  133.         } while($this->T <= $this->tiempo_final);
  134.  
  135.  
  136.         $this->vaciamiento();
  137.  
  138.         $this->imprimir();
  139.     }
  140.  
  141.     private function procesarSalida($menor_tpsa_key=null, $menor_tpsd_key=null) {
  142.         if (!$menor_tpsa_key && !$menor_tpsd_key) # VACIAMIENTO
  143.        {
  144.             $menor_tpsa_key = $this->min_key($this->TPSA);
  145.             $menor_tpsd_key = $this->min_key($this->TPSD);
  146.  
  147.             $menor_tpsa = $this->TPSA[$menor_tpsa_key];
  148.             $menor_tpsd = $this->TPSD[$menor_tpsd_key];
  149.  
  150.             if ($menor_tpsd <= $menor_tpsa) { # SALE UN DELICADO
  151.                $this->procesarSalidaDelicado($menor_tpsd_key);
  152.             } else { # SALE UN ADULTO
  153.                $this->procesarSalidaAdulto($menor_tpsa_key);
  154.             }
  155.         }
  156.         else if($menor_tpsa_key == null && $menor_tpsd_key != null) # SACO SOLO DELICADOS
  157.        {
  158.             $this->procesarSalidaDelicado($menor_tpsd_key);
  159.         }
  160.         else #HAY QUE VER SI SACO ADULTOS O DELICADOS
  161.        {
  162.             $menor_tpsa = $this->TPSA[$menor_tpsa_key];
  163.             $menor_tpsd = $this->TPSD[$menor_tpsd_key];
  164.  
  165.             if ($menor_tpsd <= $menor_tpsa)
  166.             {
  167.                 $this->procesarSalidaDelicado($menor_tpsd_key);
  168.             } else {
  169.                 $this->procesarSalidaAdulto($menor_tpsa_key);
  170.             }
  171.         }
  172.     }
  173.  
  174.     private function procesarSalidaDelicado($menor_tpsd_key) {
  175.         $this->T = $this->TPSD[$menor_tpsd_key];
  176.  
  177.         $this->STSD = $this->STSD + $this->TPSD[$menor_tpsd_key];
  178.         $this->NSD--;
  179.  
  180.         if ($this->NSD >= $this->Y)
  181.         {
  182.             $TAD = $this->generar_tiempo_de_atencion_delicado();
  183.  
  184.             $this->TPSD[$menor_tpsd_key] = $this->T + $TAD;
  185.             $this->STAD = $this->STAD + $TAD;
  186.         }
  187.         else
  188.         {
  189.             $this->ITOD[$menor_tpsd_key] = $this->T;
  190.             $this->TPSD[$menor_tpsd_key] = HV;
  191.         }
  192.     }
  193.  
  194.     private function procesarSalidaAdulto($menor_tpsa_key) {
  195.         $this->T = $this->TPSA[$menor_tpsa_key];
  196.  
  197.         $this->STSA = $this->STSA + $this->TPSA[$menor_tpsa_key];
  198.         $this->NSA--;
  199.  
  200.         if ($this->NSA >= $this->X)
  201.         {
  202.             $TAA = $this->generar_tiempo_de_atencion_adulto();
  203.  
  204.             /*echo "HAY UNA SALIDA".BR.BR;
  205.  
  206.             echo "TIEMPO DE ATENCION DE ADULTOS: ". $TAA.BR;
  207.             echo "CANTIDAD DE ADULTOS: " . $this->TA.BR.BR;*/
  208.  
  209.             $this->TPSA[$menor_tpsa_key] = $this->T + $TAA;
  210.             $this->STAA = $this->STAA + $TAA;
  211.         }
  212.         else
  213.         {
  214.             $this->ITOA[$menor_tpsa_key] = $this->T;
  215.             $this->TPSA[$menor_tpsa_key] = HV;
  216.         }
  217.     }
  218.  
  219.     private function generar_tipo_paciente() {
  220.         $r = mt_rand (0*10, 1*10) / 10;
  221.  
  222.         if ($r <= 0.15)
  223.             return "DELICADO";
  224.         else
  225.             return "ADULTO";
  226.     }
  227.  
  228.     private function buscar_cola_hv($cola) {
  229.         foreach ($cola as $key => $puesto) {
  230.             if ($puesto == HV) {
  231.                 return $key;
  232.             }
  233.         }
  234.  
  235.         return false;
  236.     }
  237.  
  238.     private function generar_tiempo_de_atencion_delicado() {
  239.         #fdp lineal
  240.        return rand(TAD_MIN,TAD_MAX);
  241.     }
  242.  
  243.     private function generar_tiempo_de_atencion_adulto() {
  244.         #fdp lineal
  245.        return rand(TAA_MIN,TAA_MAX);
  246.     }
  247.  
  248.     private function inicializar(){
  249.         for ($i=1; $i <= $this->X; $i++) {
  250.             $this->TPSA[$i] = HV;
  251.             $this->STOA[$i] = 0;
  252.             $this->ITOA[$i] = 0;
  253.         }
  254.  
  255.         for ($j=1; $j <= $this->Y; $j++) {
  256.             $this->TPSD[$j] = HV;
  257.             $this->STOD[$j] = 0;
  258.             $this->ITOD[$j] = 0;
  259.         }
  260.     }
  261.  
  262.     private function min_key($array){
  263.         $min = HV;
  264.         $key_num = 0;
  265.         foreach ($array as $key => $value) {
  266.             if($array[$key] <= $min) {
  267.                 $min = $array[$key];
  268.                 $key_num = $key;
  269.             }
  270.         }
  271.  
  272.         return $key_num;
  273.     }
  274.  
  275.  
  276.     private function generar_intervalo_entre_arribos(){
  277.         #fdp lineal
  278.        return rand(IA_MIN,IA_MAX);
  279.     }
  280.  
  281.     private function vaciamiento(){
  282.         $total_restantes = $this->NSA + $this->NSD;
  283.  
  284.         if ($total_restantes != 0) {
  285.             $this->TPLL = HV;
  286.  
  287.             for ($i = 1; $i <= $total_restantes; $i++) {
  288.                 $this->procesarSalida();
  289.             }
  290.         }
  291.     }
  292.  
  293.     private function imprimir(){
  294.         echo 'Duracion de la simulación (CPU): '.(string)(microtime(true)-$this->inicio).BR;
  295.         echo 'Variable Exógena de Control->Cantidad de Extraccionistas Adultos X: '.$this->X.BR;
  296.         echo 'Variable Exógena de Control->Cantidad de Extraccionistas Delicados Y: '.$this->Y.BR;
  297.         echo 'Variable Exógena de Control->Duracion de la simulacion: '.$this->tiempo_final.BR.BR;
  298.  
  299.         echo 'Cantidad de adultos atendidos: ' . $this->TA.BR;
  300.         echo 'Sumatoria de tiempos de llegada adultos: '.number_format($this->STLLA,2).BR;
  301.         echo 'Sumatoria de tiempos de salida adultos: '.number_format($this->STSA,2).BR;
  302.         echo 'Sumatoria de tiempos de atencion adultos: '.number_format($this->STAA,2).BR.BR;
  303.  
  304.  
  305.         echo 'Cantidad de delicados atendidos: ' . $this->TD.BR;
  306.         echo 'Sumatoria de tiempos de llegada delicados: '.number_format($this->STLLD,2).BR;
  307.         echo 'Sumatoria de tiempos de salida delicados: '.number_format($this->STSD,2).BR;
  308.         echo 'Sumatoria de tiempos de atencion delicados: '.number_format($this->STAD,2).BR.BR;
  309.  
  310.  
  311.         $PECA = ($this->STSA - $this->STLLA - $this->STAA) / $this->TA;
  312.         $PECD = ($this->STSD - $this->STLLD - $this->STAD) / $this->TD;
  313.  
  314.  
  315.         echo 'Promedio de espera en cola de adultos: '.$PECA.BR ;
  316.         echo 'Promedio de espera en cola de delicados: '.$PECD.BR;
  317.  
  318.         for ($i = 1; $i <= $this->X; $i++) {
  319.             echo 'Promedio tiempo oscioso del extraccionista de adultos: <b>' . $i . '</b>: ' . ($this->STOA[$i]*100) / $this->T.BR;
  320.         }
  321.  
  322.         for ($j = 1; $j <= $this->Y; $j++) {
  323.             echo 'Promedio tiempo oscioso del extraccionista de delicados <b>' . $j . '</b>: ' . ($this->STOD[$j]*100) / $this->T.BR;
  324.         }
  325.  
  326.         echo BR;
  327.  
  328.  
  329.  
  330.     }
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement