Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dryan;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.HashMap;
- public class Torkalka {
- private static final String ERROR_MATRICA_NE_SOGLASOVANA = "!!!Матрица не является согласованной! "
- + "Отношение согласованности больше 10% !!! \n"
- + "необходимо обратиться к ЛПР для пересмотра значений "
- + "предпочтений данной матрицы\n";
- private static HashMap<Double, Double> drobi = new HashMap<Double, Double>();
- private static HashMap<Integer, Double> SS = new HashMap<Integer, Double>();
- private static double[] x;
- static {
- drobi.put(1.0000, 1.0000);
- drobi.put(2.0000, 0.5000);
- drobi.put(3.0000, 0.3333);
- drobi.put(4.0000, 0.2500);
- drobi.put(5.0000, 0.2000);
- drobi.put(6.0000, 0.1667);
- drobi.put(7.0000, 0.1429);
- drobi.put(8.0000, 0.1250);
- drobi.put(9.0000, 0.1111);
- drobi.put(0.5000, 2.0000);
- drobi.put(0.3333, 3.0000);
- drobi.put(0.2500, 4.0000);
- drobi.put(0.2000, 5.0000);
- drobi.put(0.1667, 6.0000);
- drobi.put(0.1429, 7.0000);
- drobi.put(0.1250, 8.0000);
- drobi.put(0.1111, 9.0000);
- SS.put(1, 0.0);
- SS.put(2, 0.0);
- SS.put(3, 0.58);
- SS.put(4, 0.9);
- SS.put(5, 1.12);
- SS.put(6, 1.24);
- SS.put(7, 1.32);
- SS.put(8, 1.41);
- SS.put(9, 1.45);
- SS.put(10, 1.49);
- }
- public static void main(String[] args) {
- clean();
- stukInd();
- }
- private static void stukInd() {
- System.out.println("Индивидуальное задание:");
- individStuk();
- br();
- System.out.println("Вычислим векторы локальных приоритетов для"
- + " матриц парных сравнений 3-его уровня:");
- br();
- mk1Stuk();
- mk2Stuk();
- mk3Stuk();
- mk4Stuk();
- mk5Stuk();
- mk6Stuk();
- mk7Stuk();
- }
- public static void clean() {
- x = null;
- }
- public static void individStuk() {
- // @formatter:off
- double[][] input = new double[][] {
- //1 2 3 4 5 6 7
- {1.0, 7.0, 5.0, 3.0, 5.0, 5.0, 9.0}, // 1
- {round4(1.0/7.0), 1.0, 3.0, 3.0, 7.0, 3.0, 3.0}, // 2
- {round4(1.0/5.0), round4(1.0/3.0), 1.0, 5.0, 3.0, 3.0, 7.0}, // 3
- {round4(1.0/3.0), round4(1.0/3.0), round4(1.0/5.0), 1.0, 3.0, 5.0, 7.0}, // 4
- {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
- {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
- {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
- };
- // @formatter:on
- run(input);
- }
- public static void mk1Stuk() {
- System.out.println("Матрица 1:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, round4(1.0/5.0), round4(1.0/9.0)},
- {5.0, 1.0, round4(1.0/7.0)},
- {9.0, 7.0, 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void mk2Stuk() {
- System.out.println("Матрица 2:");
- // @formatter:off
- double[][] input = new double[][] {
- { 1.0, 7.0, 5.0 },
- { round4(1.0 / 7.0), 1.0, round4(1.0 / 3.0) },
- { round4(1.0 / 5.0), 3.0, 1.0 }, };
- // @formatter:on
- run(input);
- }
- public static void mk3Stuk() {
- System.out.println("Матрица 3:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, 3.0, 1.0},
- { round4(1.0/3.0), 1.0, round4(1.0/3.0)},
- {1.0, 3.0, 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void mk4Stuk() {
- System.out.println("Матрица 4:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, 5.0, round4(1.0/3.0)},
- { round4(1.0/5.0), 1.0, round4(1.0/7.0)},
- {3.0, 7.0, 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void mk5Stuk() {
- System.out.println("Матрица 5:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, round4(1.0/9.0), round4(1.0/9.0)},
- {9.0, 1.0, 1.0},
- {9.0, 1.0, 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void mk6Stuk() {
- System.out.println("Матрица 6:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, 5.0, 3.0},
- { round4(1.0/5.0), 1.0, 3.0},
- { round4(1.0/3.0), round4(1.0/3.0), 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void mk7Stuk() {
- System.out.println("Матрица 7:");
- // @formatter:off
- double[][] input = new double[][] {
- {1.0, 7.0, 9.0},
- { round4(1.0/7.0), 1.0, 5.0},
- { round4(1.0/9.0), round4(1./5.), 1.0},
- };
- // @formatter:on
- run(input);
- }
- public static void run(double[][] input) {
- if (checkMatrix(input)) {
- // number of criteries
- int matrixSize = input.length;
- System.out.println("===============================");
- br();
- printDoubleMatrix(input);
- br();
- double[] b = new double[matrixSize];
- for (int i = 0; i < matrixSize; i++) {
- double multiplication = input[i][0];
- for (int j = 1; j < matrixSize; j++) {
- multiplication = (multiplication * input[i][j]);
- }
- multiplication = round4(multiplication); // хз, округлять ли
- // произведение
- b[i] = Math.pow(multiplication, 1.0 / matrixSize);
- System.out.println("b" + (i + 1) + " = sqrt" + matrixSize + "("
- + multiplication + ")" + " = " + n4(b[i]));
- }
- br();
- double B = sum(b);
- System.out.println("B = " + B);
- br();
- x = new double[matrixSize];
- for (int i = 0; i < matrixSize; i++) {
- x[i] = round4(b[i] / B);
- System.out.println("x" + (i + 1) + " = " + x[i]);
- }
- br();
- double sum = sum(x);
- System.out.println("Проверка: cумма всех xi равна " + sum);
- if (sum != 1.0) {
- pogreshnost(sum);
- }
- br();
- double ymax = yMax(input);
- br();
- double IS = round4((ymax - matrixSize) / (matrixSize - 1));
- System.out.println("ИС = " + IS);
- double ss = SS.get(matrixSize);
- double OS = round4(IS / ss * 100.0);
- br();
- System.out.println("ОС = (ИС / СС(" + matrixSize + ")) * 100% = (" + IS + " / " + ss
- + ") * 100% = " + OS + "%");
- if (OS > 10.0) {
- br();
- System.out.println(ERROR_MATRICA_NE_SOGLASOVANA);
- }
- br();
- System.out.println("===============================");
- }
- }
- private static void pogreshnost(double value) {
- double sigma = Math.abs(1.0 - value) * 100.0 / 1.0;
- sigma = round4(sigma);
- System.out.println("Погрешность равна | 1 - " + value
- + " | * 100% / 1 = " + sigma + "%.");
- }
- public static double yMax(double[][] input) {
- double yMax = 0;
- for (int i = 0; i < input.length; i++) {
- double yi = round4(sum(getColumn(input, i)));
- System.out.println("y" + (i + 1) + " = " + yi);
- }
- br();
- System.out.print("yMax = ");
- for (int i = 0; i < input.length; i++) {
- double yi = round4(sum(getColumn(input, i)));
- double promezhutocnii = round4(x[i] * yi);
- System.out.print(promezhutocnii
- + (i != input.length - 1 ? " + " : ""));
- yMax += promezhutocnii;
- }
- yMax = round4(yMax);
- if (yMax < input.length) {
- System.out.print(" = получено значение, меньшее " + input.length + " (" +
- yMax + "). Округлено до размерности матрицы = до " + input.length + "." );
- yMax = input.length;
- } else {
- System.out.print(" = " + yMax);
- }
- br();
- return yMax;
- }
- public static double[] getColumn(double[][] input, int nomerStolbca) {
- double[] column = new double[input.length];
- for (int i = 0; i < input.length; i++) {
- column[i] = input[i][nomerStolbca];
- }
- return column;
- }
- public static double sum(double[] mas) {
- double sum = 0.0;
- for (double num : mas) {
- sum += num;
- }
- return round4(sum);
- }
- public static void br() {
- System.out.println();
- }
- // public static double round3(double what) {
- // return round(what, 3);
- // }
- public static double round4(double what) {
- return round(what, 4);
- }
- public static double round(double what, int precision) {
- BigDecimal x = new BigDecimal(what + "");
- x = x.setScale(precision, BigDecimal.ROUND_HALF_UP);
- double result = x.doubleValue();
- return result;
- }
- public static String n4(double what) {
- DecimalFormat decimalFormat = new DecimalFormat("#.####");
- decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
- return (decimalFormat.format(what));
- }
- public static double log2(double what) {
- if (what == 0.0) {
- return 0.0;
- } else {
- return round4(Math.log(what) / Math.log(2.0));
- }
- }
- public static void log2Print(double what) {
- if (what == 0.0) {
- System.out.println(0.0);
- } else {
- System.out.println(round4(Math.log(what) / Math.log(2.0)));
- }
- }
- // prints the [][] double matrix
- public static void printDoubleMatrix(double[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- System.out.print(" | ");
- for (int j = 0; j < matrix[0].length; j++) {
- System.out.print(normalize(matrix[i][j], 4) + " ");
- }
- System.out.println(" | ");
- }
- }
- // prints the [] double matrix
- public static void printMatrix(double[] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- System.out.print(normalize(matrix[i], 4) + " | ");
- }
- }
- public static String normalize(double what, int precision) {
- String result = what + "";
- try {
- while (result.substring(result.indexOf(".") + 1).length() < precision)
- result = result + "0";
- } catch (Exception m) {
- m.printStackTrace();
- }
- return result;
- }
- public static boolean checkMatrix(double[][] matrix) {
- boolean result = true;
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[0].length; j++) {
- if (i != j && !checkElements(matrix[i][j], matrix[j][i])) {
- result = false;
- System.out.println("Неверный элемент матрицы: i=" + (i + 1)
- + ", j=" + (j + 1) + " значение = " + matrix[i][j]
- + ", противоположное значение = " + matrix[j][i]);
- break;
- }
- }
- }
- return result;
- }
- public static boolean checkElements(Double a, Double b) {
- if (((Double) drobi.get(a)).equals((Double) b)) {
- return true;
- } else {
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment