Guest User

Untitled

a guest
Dec 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.85 KB | None | 0 0
  1. package dryan;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.text.DecimalFormat;
  6. import java.util.HashMap;
  7.  
  8. public class Torkalka {
  9.  
  10.     private static final String ERROR_MATRICA_NE_SOGLASOVANA = "!!!Матрица не является согласованной! "
  11.             + "Отношение согласованности больше 10% !!! \n"
  12.             + "необходимо обратиться к ЛПР для пересмотра значений "
  13.             + "предпочтений данной матрицы\n";
  14.  
  15.     private static HashMap<Double, Double> drobi = new HashMap<Double, Double>();
  16.     private static HashMap<Integer, Double> SS = new HashMap<Integer, Double>();
  17.  
  18.     private static double[] x;
  19.  
  20.     static {
  21.         drobi.put(1.0000, 1.0000);
  22.         drobi.put(2.0000, 0.5000);
  23.         drobi.put(3.0000, 0.3333);
  24.         drobi.put(4.0000, 0.2500);
  25.         drobi.put(5.0000, 0.2000);
  26.         drobi.put(6.0000, 0.1667);
  27.         drobi.put(7.0000, 0.1429);
  28.         drobi.put(8.0000, 0.1250);
  29.         drobi.put(9.0000, 0.1111);
  30.         drobi.put(0.5000, 2.0000);
  31.         drobi.put(0.3333, 3.0000);
  32.         drobi.put(0.2500, 4.0000);
  33.         drobi.put(0.2000, 5.0000);
  34.         drobi.put(0.1667, 6.0000);
  35.         drobi.put(0.1429, 7.0000);
  36.         drobi.put(0.1250, 8.0000);
  37.         drobi.put(0.1111, 9.0000);
  38.  
  39.         SS.put(1, 0.0);
  40.         SS.put(2, 0.0);
  41.         SS.put(3, 0.58);
  42.         SS.put(4, 0.9);
  43.         SS.put(5, 1.12);
  44.         SS.put(6, 1.24);
  45.         SS.put(7, 1.32);
  46.         SS.put(8, 1.41);
  47.         SS.put(9, 1.45);
  48.         SS.put(10, 1.49);
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         clean();
  53.         stukInd();
  54.     }
  55.     private static void stukInd() {
  56.         System.out.println("Индивидуальное задание:");
  57.         individStuk();
  58.  
  59.         br();
  60.         System.out.println("Вычислим векторы локальных приоритетов для"
  61.                 + " матриц парных сравнений 3-его уровня:");
  62.         br();
  63.         mk1Stuk();
  64.         mk2Stuk();
  65.         mk3Stuk();
  66.         mk4Stuk();
  67.         mk5Stuk();
  68.         mk6Stuk();
  69.         mk7Stuk();
  70.     }          
  71.    
  72.     public static void clean() {
  73.         x = null;
  74.     }
  75.  
  76.     public static void individStuk() {
  77.         // @formatter:off
  78.         double[][] input = new double[][] {
  79.         //1                  2                   3                   4                   5                   6                   7      
  80.        {1.0,                7.0,                5.0,                3.0,                5.0,                5.0,                9.0}, // 1
  81.        {round4(1.0/7.0),    1.0,                3.0,                3.0,                7.0,                3.0,                3.0}, // 2
  82.        {round4(1.0/5.0),    round4(1.0/3.0),    1.0,                5.0,                3.0,                3.0,                7.0}, // 3
  83.        {round4(1.0/3.0),    round4(1.0/3.0),    round4(1.0/5.0),    1.0,                3.0,                5.0,                7.0}, // 4
  84.        {round4(1.0/5.0),    round4(1.0/7.0),    round4(1.0/3.0),    round4(1.0/3.0),    1.0,                3.0,                3.0}, // 5
  85.        {round4(1.0/5.0),    round4(1.0/3.0),    round4(1.0/3.0),    round4(1.0/5.0),    round4(1.0/3.0),    1.0,                3.0}, // 6
  86.        {round4(1.0/9.0),    round4(1.0/3.0),    round4(1.0/7.0),    round4(1.0/7.0),    round4(1.0/3.0),    round4(1.0/3.0),    1.0}  // 7
  87.         };
  88.         // @formatter:on
  89.         run(input);
  90.     }
  91.  
  92.  
  93. public static void mk1Stuk() {
  94.         System.out.println("Матрица 1:");
  95.         // @formatter:off
  96.         double[][] input = new double[][] {
  97.         {1.0,   round4(1.0/5.0),    round4(1.0/9.0)},
  98.         {5.0,   1.0,    round4(1.0/7.0)},
  99.         {9.0,   7.0,    1.0},            
  100.         };
  101. //         @formatter:on
  102.         run(input);
  103.     }
  104.  
  105. public static void mk2Stuk() {
  106.     System.out.println("Матрица 2:");
  107.     // @formatter:off
  108.         double[][] input = new double[][] {
  109.         { 1.0,                 7.0,     5.0 },
  110.         { round4(1.0 / 7.0),   1.0,     round4(1.0 / 3.0) },
  111.         { round4(1.0 / 5.0),   3.0,     1.0 }, };
  112. //     @formatter:on
  113.     run(input);
  114. }
  115.  
  116.  
  117. public static void mk3Stuk() {
  118.         System.out.println("Матрица 3:");
  119.         // @formatter:off
  120.         double[][] input = new double[][] {
  121.  
  122.         {1.0,   3.0,    1.0},
  123.         { round4(1.0/3.0)1.0,    round4(1.0/3.0)},
  124.         {1.0,   3.0,    1.0},
  125.         };
  126. //         @formatter:on
  127.         run(input);
  128.     }
  129.  
  130. public static void mk4Stuk() {
  131.         System.out.println("Матрица 4:");
  132.         // @formatter:off
  133.         double[][] input = new double[][] {
  134.         {1.0,   5.0,    round4(1.0/3.0)},
  135.         { round4(1.0/5.0)1.0,    round4(1.0/7.0)},
  136.         {3.0,   7.0,    1.0},
  137.         };
  138. //         @formatter:on
  139.         run(input);
  140.     }
  141.  
  142.  
  143. public static void mk5Stuk() {
  144.         System.out.println("Матрица 5:");
  145.         // @formatter:off
  146.         double[][] input = new double[][] {
  147.         {1.0,   round4(1.0/9.0),    round4(1.0/9.0)},
  148.         {9.0,   1.0,    1.0},
  149.         {9.0,   1.0,    1.0},
  150.         };
  151. //         @formatter:on
  152.         run(input);
  153.     }
  154.  
  155.  
  156. public static void mk6Stuk() {
  157.         System.out.println("Матрица 6:");
  158.         // @formatter:off
  159.         double[][] input = new double[][] {
  160.         {1.0,   5.0,    3.0},
  161.         { round4(1.0/5.0)1.0,    3.0},
  162.         { round4(1.0/3.0),  round4(1.0/3.0),    1.0},
  163.         };
  164. //         @formatter:on
  165.         run(input);
  166.     }
  167.  
  168. public static void mk7Stuk() {
  169.         System.out.println("Матрица 7:");
  170.         // @formatter:off
  171.         double[][] input = new double[][] {
  172.         {1.0,   7.0,    9.0},
  173.         { round4(1.0/7.0)1.0,    5.0},
  174.         { round4(1.0/9.0),  round4(1./5.)1.0},
  175.         };
  176. //         @formatter:on
  177.         run(input);
  178.     }
  179.    
  180.     public static void run(double[][] input) {
  181.  
  182.         if (checkMatrix(input)) {
  183.  
  184.             // number of criteries
  185.             int matrixSize = input.length;
  186.  
  187.             System.out.println("===============================");
  188.             br();
  189.  
  190.             printDoubleMatrix(input);
  191.             br();
  192.  
  193.             double[] b = new double[matrixSize];
  194.  
  195.             for (int i = 0; i < matrixSize; i++) {
  196.  
  197.                 double multiplication = input[i][0];
  198.                 for (int j = 1; j < matrixSize; j++) {
  199.                     multiplication = (multiplication * input[i][j]);
  200.                 }
  201.                 multiplication = round4(multiplication); // хз, округлять ли
  202.                                                          // произведение
  203.                 b[i] = Math.pow(multiplication, 1.0 / matrixSize);
  204.                 System.out.println("b" + (i + 1) + " = sqrt" + matrixSize + "("
  205.                         + multiplication + ")" + " = " + n4(b[i]));
  206.  
  207.             }
  208.             br();
  209.  
  210.             double B = sum(b);
  211.             System.out.println("B = " + B);
  212.  
  213.             br();
  214.  
  215.             x = new double[matrixSize];
  216.             for (int i = 0; i < matrixSize; i++) {
  217.                 x[i] = round4(b[i] / B);
  218.                 System.out.println("x" + (i + 1) + " = " + x[i]);
  219.             }
  220.  
  221.             br();
  222.  
  223.             double sum = sum(x);
  224.             System.out.println("Проверка: cумма всех xi равна " + sum);
  225.             if (sum != 1.0) {
  226.                 pogreshnost(sum);
  227.             }
  228.  
  229.             br();
  230.  
  231.             double ymax = yMax(input);
  232.  
  233.             br();
  234.  
  235.             double IS = round4((ymax - matrixSize) / (matrixSize - 1));
  236.             System.out.println("ИС = " + IS);
  237.  
  238.             double ss = SS.get(matrixSize);
  239.  
  240.             double OS = round4(IS / ss * 100.0);
  241.  
  242.             br();
  243.  
  244.             System.out.println("ОС = (ИС / СС(" + matrixSize + ")) * 100% = (" + IS + " / " + ss
  245.                     + ") * 100% = " + OS + "%");
  246.  
  247.             if (OS > 10.0) {
  248.                 br();
  249.                 System.out.println(ERROR_MATRICA_NE_SOGLASOVANA);
  250.             }
  251.  
  252.             br();
  253.             System.out.println("===============================");
  254.  
  255.         }
  256.  
  257.     }
  258.  
  259.     private static void pogreshnost(double value) {
  260.         double sigma = Math.abs(1.0 - value) * 100.0 / 1.0;
  261.         sigma = round4(sigma);
  262.         System.out.println("Погрешность равна | 1 - " + value
  263.                 + " | * 100% / 1 = " + sigma + "%.");
  264.     }
  265.  
  266.     public static double yMax(double[][] input) {
  267.         double yMax = 0;
  268.  
  269.         for (int i = 0; i < input.length; i++) {
  270.             double yi = round4(sum(getColumn(input, i)));
  271.             System.out.println("y" + (i + 1) + " = " + yi);
  272.         }
  273.  
  274.         br();
  275.  
  276.         System.out.print("yMax = ");
  277.  
  278.         for (int i = 0; i < input.length; i++) {
  279.             double yi = round4(sum(getColumn(input, i)));
  280.             double promezhutocnii = round4(x[i] * yi);
  281.  
  282.             System.out.print(promezhutocnii
  283.                     + (i != input.length - 1 ? " + " : ""));
  284.  
  285.             yMax += promezhutocnii;
  286.         }
  287.  
  288.         yMax = round4(yMax);
  289.  
  290.         if (yMax < input.length) {
  291.             System.out.print(" = получено значение, меньшее  " + input.length + " (" +
  292.                     yMax + "). Округлено до размерности матрицы = до " + input.length + "." );
  293.             yMax = input.length;
  294.         } else {
  295.             System.out.print(" = " + yMax);
  296.         }
  297.         br();
  298.         return yMax;
  299.     }
  300.  
  301.     public static double[] getColumn(double[][] input, int nomerStolbca) {
  302.         double[] column = new double[input.length];
  303.         for (int i = 0; i < input.length; i++) {
  304.             column[i] = input[i][nomerStolbca];
  305.         }
  306.         return column;
  307.     }
  308.  
  309.     public static double sum(double[] mas) {
  310.         double sum = 0.0;
  311.         for (double num : mas) {
  312.             sum += num;
  313.         }
  314.         return round4(sum);
  315.     }
  316.  
  317.     public static void br() {
  318.         System.out.println();
  319.     }
  320.  
  321.     // public static double round3(double what) {
  322.     // return round(what, 3);
  323.     // }
  324.  
  325.     public static double round4(double what) {
  326.         return round(what, 4);
  327.     }
  328.  
  329.     public static double round(double what, int precision) {
  330.         BigDecimal x = new BigDecimal(what + "");
  331.         x = x.setScale(precision, BigDecimal.ROUND_HALF_UP);
  332.         double result = x.doubleValue();
  333.         return result;
  334.     }
  335.  
  336.     public static String n4(double what) {
  337.         DecimalFormat decimalFormat = new DecimalFormat("#.####");
  338.         decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
  339.         return (decimalFormat.format(what));
  340.     }
  341.  
  342.     public static double log2(double what) {
  343.         if (what == 0.0) {
  344.             return 0.0;
  345.         } else {
  346.             return round4(Math.log(what) / Math.log(2.0));
  347.         }
  348.     }
  349.  
  350.     public static void log2Print(double what) {
  351.         if (what == 0.0) {
  352.             System.out.println(0.0);
  353.         } else {
  354.             System.out.println(round4(Math.log(what) / Math.log(2.0)));
  355.         }
  356.     }
  357.  
  358.     // prints the [][] double matrix
  359.     public static void printDoubleMatrix(double[][] matrix) {
  360.         for (int i = 0; i < matrix.length; i++) {
  361.             System.out.print(" |  ");
  362.             for (int j = 0; j < matrix[0].length; j++) {
  363.                 System.out.print(normalize(matrix[i][j], 4) + " ");
  364.             }
  365.             System.out.println(" | ");
  366.         }
  367.     }
  368.  
  369.     // prints the [] double matrix
  370.     public static void printMatrix(double[] matrix) {
  371.         for (int i = 0; i < matrix.length; i++) {
  372.             System.out.print(normalize(matrix[i], 4) + " |  ");
  373.         }
  374.     }
  375.  
  376.     public static String normalize(double what, int precision) {
  377.         String result = what + "";
  378.         try {
  379.             while (result.substring(result.indexOf(".") + 1).length() < precision)
  380.                 result = result + "0";
  381.         } catch (Exception m) {
  382.             m.printStackTrace();
  383.         }
  384.         return result;
  385.     }
  386.  
  387.     public static boolean checkMatrix(double[][] matrix) {
  388.         boolean result = true;
  389.         for (int i = 0; i < matrix.length; i++) {
  390.             for (int j = 0; j < matrix[0].length; j++) {
  391.                 if (i != j && !checkElements(matrix[i][j], matrix[j][i])) {
  392.                     result = false;
  393.                     System.out.println("Неверный элемент матрицы: i=" + (i + 1)
  394.                             + ", j=" + (j + 1) + " значение = " + matrix[i][j]
  395.                             + ", противоположное значение = " + matrix[j][i]);
  396.                     break;
  397.                 }
  398.             }
  399.         }
  400.         return result;
  401.     }
  402.  
  403.     public static boolean checkElements(Double a, Double b) {
  404.         if (((Double) drobi.get(a)).equals((Double) b)) {
  405.             return true;
  406.         } else {
  407.             return false;
  408.         }
  409.     }
  410.  
  411. }
Add Comment
Please, Sign In to add comment