Advertisement
guerratron

RankingScore - For Code-eval

Jul 23rd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. /* CLASE DE UTILIDAD PARA CALCULAR RANKINGs DE DESAFÍOS CODEEVAL.
  2.  * Juan José Guerra - dinertron@gmail.com - 2014 */
  3. /**
  4.  * * <cite><small>FROM 'CODEEVAL' (<a
  5.  * href="https://www.codeeval.com/ranking/">https
  6.  * ://www.codeeval.com/ranking/</a>)</small></cite><br />
  7.  * <small><b>LANGUAGE</b>: Spanish<small> <h1>CALCULO DEL RECORD EN EL RANKING</h1>
  8.  * <p>
  9.  * Es una clase MUY SIMPLE (y MEJORABLE) que utilizo para calcular el record
  10.  * conseguido en un desafío de los propuestos en CODEEVAL, con propósitos
  11.  * estadísticos y de archivación.<br />
  12.  * No está finalizada al completo (faltaría MANEJO DE ERRORES, DEPURACIÓN, ...)
  13.  * pero de momento me basta para hacerme una idea del Ranking de cada desafío.
  14.  * </p>
  15.  * <p style="background:lightGray;">
  16.  * UTILIZACIÓN: ver javadoc {@link #main(String...)} para los parámetros
  17.  * ejecutables.
  18.  * </p>
  19.  *
  20.  * @author guerraTron - dinertron@gmail.com - 2014
  21.  **/
  22. public abstract class RankingScore {
  23.     /*
  24.      * # if submission takes more than 10 seconds # or uses more than 20MB of
  25.      * memory # the score is 0
  26.      */
  27.     static final int MAX_MEMORY = 20 * 1024 * 1024; // 20 MB
  28.     static final int MAX_TIME = 10 * 1000; // 10 sec
  29.  
  30.     /**
  31.      * @param args
  32.      *            : (String[]) donde:
  33.      *            <ul>
  34.      *            <li>args[0] : <b>SCORE</b> : la puntuación recibida por los
  35.      *            tests (the score which is received by test-cases).</li>
  36.      *            <li>args[1] : <b>MEMORY-TAKEN</b> : memoria ocupada por el
  37.      *            codigo empleado (memory taken by submission).</li>
  38.      *            <li>args[2] : <b>TIME-TAKEN</b> : tiempo que ha tomado el
  39.      *            codigo empleado (time taken by submission).</li>
  40.      *            <li>args[3] : <b>CATEGORY</b> :
  41.      *            FACIL-Easy(1)/MEDIO-Moderate(2)/DIFICIL-Hard(3).</li>
  42.      *            </ul>
  43.      **/
  44.     public static void main(String... args) {
  45.         if (args.length == 0) {
  46.             System.out.println("WITHOUT ARGUMENTS -SIN ARGUMENTOS-");
  47.             // System.exit(-1);
  48.             return;
  49.         }
  50.         float memoryFactor = get_memory_factor(Long.parseLong(args[1]),
  51.                 MAX_MEMORY);
  52.         float timeFactor = get_time_factor(Long.parseLong(args[2]), MAX_TIME);
  53.         System.out.println("MEMORY FACTOR -FACTOR DE MEMORIA- : "
  54.                 + memoryFactor);
  55.         System.out.println("TIME FACTOR -FACTOR DE TIEMPO- : " + timeFactor);
  56.         System.out.println("TOTAL FACTOR -FACTOR TOTAL- : "
  57.                 + get_total_factor(memoryFactor, timeFactor));
  58.         System.out.println("TOTAL SCORE -PUNTUACION TOTAL- : "
  59.                 + get_submission_score(Float.parseFloat(args[0]),
  60.                         Long.parseLong(args[1]), Long.parseLong(args[2]),
  61.                         Byte.parseByte(args[3])));
  62.     }
  63.  
  64.     /**
  65.      * Formula utilizada para calcular La Puntuacion (SCORE) a utilizar para el
  66.      * RANKING.
  67.      *
  68.      * @param score
  69.      *            : la puntuación recibida por los tests (the score which is
  70.      *            received by test-cases)
  71.      * @param memory_taken
  72.      *            : memoria ocupada por el codigo empleado (memory taken by
  73.      *            submission)
  74.      * @param time_taken
  75.      *            : tiempo que ha tomado el codigo empleado (time taken by
  76.      *            submission)
  77.      * @param category
  78.      *            : FACIL-Easy(1)/MEDIO-Moderate(2)/DIFICIL-Hard(3)
  79.      **/
  80.     public static float get_submission_score(float score, long memory_taken,
  81.             long time_taken, byte category) {
  82.         int[] total_max = new int[] { 0, // NO UTILIZADO
  83.                 35, // max 35 points for Easy challenge
  84.                 65, // max 65 points for Moderate challenge
  85.                 100 // max 100 points for Hard challenge
  86.         };
  87.  
  88.         /*
  89.          * # if submission takes more than 10 seconds # or uses more than 20MB
  90.          * of memory # the score is 0
  91.          */
  92.         if (memory_taken > MAX_MEMORY || time_taken > MAX_TIME)
  93.             return 0;
  94.  
  95.         int max_total_score = total_max[category];
  96.  
  97.         float memory_factor = get_memory_factor(memory_taken, MAX_MEMORY);
  98.         float time_factor = get_time_factor(time_taken, MAX_TIME);
  99.         float factor = get_total_factor(memory_factor, time_factor);
  100.  
  101.         return score * max_total_score * factor / 100;
  102.     }
  103.  
  104.     /**
  105.      * Formula utilizada para calcular el FACTOR DE MEMORIA a utilizar para la
  106.      * formula del RANKING.<br />
  107.      * Esto se emplea en el calculo de la formula mediante el metodo
  108.      * {@link #get_submission_score(float,long,long,byte)}
  109.      *
  110.      * @param memory_taken
  111.      *            : memoria ocupada por el codigo empleado (memory taken by
  112.      *            submission)
  113.      * @param max_memory
  114.      *            : memoria maxima permitida
  115.      **/
  116.     public static float get_memory_factor(long memory_taken, long max_memory) {
  117.         /*
  118.          * # if submission takes more than 10 seconds # or uses more than 20MB
  119.          * of memory # the score is 0
  120.          */
  121.         if (memory_taken > max_memory)
  122.             return 0;
  123.  
  124.         return (1 - ((float) memory_taken / max_memory));
  125.     }
  126.  
  127.     /**
  128.      * Formula utilizada para calcular el FACTOR DE TIEMPO a utilizar para la
  129.      * formula del RANKING.<br />
  130.      * Esto se emplea en el calculo de la formula mediante el metodo
  131.      * {@link #get_submission_score(float,long,long,byte)}
  132.      *
  133.      * @param time_taken
  134.      *            : tiempo que ha tomado el codigo empleado (time taken by
  135.      *            submission)
  136.      * @param max_time
  137.      *            : tiempo maximo permitido
  138.      **/
  139.     public static float get_time_factor(long time_taken, long max_time) {
  140.         /*
  141.          * # if submission takes more than 10 seconds # or uses more than 20MB
  142.          * of memory # the score is 0
  143.          */
  144.         if (time_taken > max_time)
  145.             return 0;
  146.  
  147.         return (1 - ((float) time_taken / max_time));
  148.     }
  149.  
  150.     /**
  151.      * Formula utilizada para calcular el FACTOR TOTAL (DE MEMORIA Y TIEMPO) a
  152.      * utilizar para la formula del RANKING.<br />
  153.      * Esto se emplea en el calculo de la formula mediante el metodo
  154.      * {@link #get_submission_score(float,long,long,byte)}
  155.      *
  156.      * @param memory_factor
  157.      *            : factor de memoria ocupada por el codigo empleado
  158.      * @param time_factor
  159.      *            : factor de tiempo que ha tomado el codigo empleado
  160.      **/
  161.     public static float get_total_factor(float memory_factor, float time_factor) {
  162.         return ((memory_factor + time_factor) / 2);
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement