Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 5.50 KB | None | 0 0
  1. funcprot(0);
  2. function []=display_all(arr_prices, arr_markers, arr_zeroes)
  3. disp("Текущее состояние матрицы стоимостей:");
  4. disp(arr_prices);
  5. disp("Текущее состояние матрицы :");
  6. disp(arr_markers);
  7. disp("Current status of zeroes:");
  8. disp(arr_zeroes);
  9. endfunction
  10. function [result]=minimize_rows(arr)
  11. for i = 1:size(arr,1)
  12. minimum = min(arr(i,:));
  13. arr(i,:) = arr(i,:)-minimum
  14. end
  15. result = arr;
  16. disp("Результат минимизации строк:");
  17. disp(result);
  18. endfunction
  19. function [result]=minimize_columns(arr)
  20. for i = 1:size(arr,2)
  21. minimum = min(arr(:,i));
  22. arr(:, i) = arr(:, i)-minimum
  23. end
  24. result = arr;
  25. disp("Результат минимизации столбцов:");
  26. disp(result);
  27. endfunction
  28. function [result]=combined_minimize(arr)
  29. arr = minimize_rows(arr);
  30. arr = minimize_columns(arr);
  31. result = arr;
  32. endfunction
  33. //-----------------------------------
  34. function [result_markers, result_zeroes]=scan_columns(arr_prices, arr_markers, arr_zeroes)
  35. result_zeroes=arr_zeroes;
  36. result_markers=arr_markers;
  37. for row = 1:size(arr_prices, 1)
  38. zeroes_in_row = find(arr_prices(row, :) == 0);
  39. unmarked_zeroes_in_row = find(result_markers(row, zeroes_in_row) == 0);
  40. if length(unmarked_zeroes_in_row) == 1 then
  41. zero_index = zeroes_in_row(unmarked_zeroes_in_row(1));
  42. if arr_markers(row, zero_index) == 0 then
  43. result_zeroes(size(result_zeroes, 1) + 1, :) = [row, zero_index];
  44. result_markers(:, zero_index) = result_markers(:, zero_index) +1;
  45. end
  46. end
  47. end
  48. endfunction
  49. function [result_markers, result_zeroes]=scan_rows(arr_prices, arr_markers, arr_zeroes)
  50. result_zeroes=arr_zeroes;
  51. result_markers=arr_markers;
  52. for column = 1:size(arr_prices, 2)
  53. zeroes_in_column = find(arr_prices(:,column) == 0);
  54. unmarked_zeroes_in_col = find(result_markers( zeroes_in_column, column) == 0);
  55. if length(unmarked_zeroes_in_col) == 1 then
  56. zero_index = zeroes_in_column( unmarked_zeroes_in_col(1));
  57. if result_markers(zero_index, column) == 0 then
  58. result_zeroes(size(result_zeroes, 1) + 1, :) = [zero_index, column];
  59. result_markers(zero_index, :) = result_markers(zero_index, :) + 1;
  60. end
  61. end
  62. end
  63. endfunction
  64. function [result_markers, result_zeroes]=combined_scan(arr_prices)
  65. result_markers = zeros(size(arr_prices, 1), size(arr_prices,2));
  66. result_zeroes = [];
  67. [result_markers, result_zeroes] = scan_columns(arr_prices, result_markers, result_zeroes);
  68. [result_markers, result_zeroes] = scan_rows(arr_prices, result_markers, result_zeroes);
  69. for row = 1:size(arr_prices, 1)
  70. zeroes_in_row = find(arr_prices(row, :) == 0);
  71. unmarked_zeroes_in_row = find(result_markers(row, zeroes_in_row) == 0);
  72. if length(unmarked_zeroes_in_row) > 0 then
  73. zero_index = zeroes_in_row(unmarked_zeroes_in_row(1));
  74. if result_markers(row, zero_index) == 0 then
  75. result_zeroes(size(result_zeroes, 1) + 1, :) = [row, zero_index];
  76. result_markers(:, zero_index) = result_markers(:, zero_index) +1;
  77. end
  78. end
  79. end
  80. disp("Результат зачеркивания строк и столбцов:");
  81. disp(result_markers);
  82. endfunction
  83. //-----------------------------------
  84. function [result_prices]=compensate(arr_markers, arr_prices)
  85. intersections = find(arr_markers == 2);
  86. free_spaces = find(arr_markers == 0);
  87. minimum = min(arr_prices(free_spaces));
  88. arr_prices(intersections) = arr_prices(intersections) + minimum;
  89. arr_prices(free_spaces) = arr_prices(free_spaces) - minimum;
  90. result_prices=arr_prices;
  91. disp("Результат компенсации значений:");
  92. disp(result_prices);
  93. endfunction
  94. //-----------------------------------
  95. function [result]=check_for_solved(arr_zeroes, arr_prices)
  96. printf("Количество вычеркнутых нулей: %d\n", size(arr_zeroes,1));
  97. if size(arr_zeroes,1)==size(arr_prices,1) then
  98. disp("Решение найдено.");
  99. result = 1;
  100. else result = 0;
  101. disp("На данной итерации решение не найдено.");
  102. end
  103. endfunction
  104. function []=display_answer(arr)
  105. disp("Финальная матрица:");
  106. arr = gsort(arr, 'lr', 'i');
  107. disp(arr);
  108. answer_string = msprintf("Работник %d идёт на работу %d\n", arr(:,1), arr(:,2));
  109. x_dialog("Результат работы венгерского метода:",answer_string)
  110. endfunction
  111. //-----------------------------------
  112. function []=solve_destinations(arr_prices, iteration)
  113. iteration = iteration+1;
  114. if iteration~=8 then
  115. printf("\n\nИтерация %d\n", iteration);
  116. markers = zeros(size(arr_prices, 1), size(arr_prices,2));
  117. zeroes = [];
  118. //Первый шаг - минимизация строк и столбцов
  119. arr_prices = combined_minimize(arr_prices);
  120. //Второй шаг - сканирование строк и столбцов
  121. [markers, zeroes] = combined_scan(arr_prices);
  122. //Третий шаг - проверка, решена ли задача
  123. if check_for_solved(zeroes, arr_prices) ==1 then
  124. //Если да, то выводим ответ и завершаем работу функции
  125. display_answer(zeroes);
  126. else
  127. //Иначе компенсируем значения и запускаем новую итерацию
  128. arr_prices = compensate(markers, arr_prices);
  129. disp("Переход к следующей итерации...");
  130. solve_destinations(arr_prices);
  131. end
  132. end
  133. endfunction
  134. //-----------------------------------
  135. answer = x_matrix(['Введите условие задачи о назначениях в формате матрицы NxN'],[18 1 8 13 18 7 9; 11 5 17 1 12 14
  136. 5; 6 3 4 1 13 9 3; 1 15 14 8 8 16 2; 20 2 19 2 5 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement