Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. // Part 1: Declare variables and read file
  11.  
  12. //// Part 1.1: Declare variables
  13. struct Zone { // zone struct
  14. int a; // y axis position
  15. int b; // x axis position
  16. double W, // demand of this position
  17. C, // build cost
  18. Wbar, // total demand of R region
  19. Q; // assessed value = Wbar/C
  20. bool isQ, // is qualified to build new facility
  21. isF, // is selected to build new facility
  22. isB; // is assigned to a specific facility
  23. };
  24. int M, // y axis range (1~M)
  25. N, // x axis range (1~N)
  26. U = 0, // amount of qualified zones
  27. L, // amount of facilities to build
  28. R; // the range of facilities, using Manhattan Distance
  29. double Qstar; // specified accessed value
  30. Zone *V; // start pointer of zone array
  31. int *VF; // start pointer of facility index array
  32. bool **E; // start pointer of two dimension responsible array, candidate point <-> Zone position
  33.  
  34. char filename[] = "data1.txt"; // file name and default filename
  35. fstream file; // file stream
  36. int remained, // remained step (remained Manhattan Distance for x-direction)
  37. tm, // temp y-position
  38. cm, // current y-position
  39. cn, // current x-position
  40. zi, // zone index (m*N+n)
  41. u_max, // facility index of max demand facility
  42. i_max; // zone index of max-demand facility
  43. double w_bar, // temp w_bar
  44. q, // temp q
  45. w_bar_max, // w_bar max
  46. w, // temp w
  47. total_satisfied_w = 0; // total satisfied w
  48. bool debug = false; // debug flag
  49.  
  50. //// Part 1.2: Get input file name
  51. if (!debug) { // show input and get filename when not in debug mode
  52. cout << "Enter filename: ";
  53. cin >> filename;
  54. }
  55.  
  56. //// Part 1.3: Read and parse file
  57. file.open(filename, ios::in);
  58. file >> M;
  59. file >> N;
  60. file >> Qstar;
  61. file >> L;
  62. file >> R;
  63. V = new Zone[M*N];
  64. VF = new int[M*N];
  65. for(int i = 0; i < M*N; i++){
  66. file >> V[i].W;
  67. V[i].a = floor(i/N);
  68. V[i].b = i % N;
  69. V[i].isQ = false;
  70. V[i].isF = false;
  71. V[i].isB = false;
  72. }
  73. for(int i = 0; i < M*N; i++) file >> V[i].C;
  74. file.close();
  75.  
  76. //// Part 1.4: Print result
  77. ////// Part 1.4.1: Print variables
  78. printf("M=%d; N=%d; Q*=%.1lf; L=%d; R=%d;\n", M, N, Qstar, L, R);
  79.  
  80. ////// Part 1.4.2: Print W matrix
  81. printf("%6c", 'W');
  82. for (int n = 0; n < N; n++) printf("%6d", n+1);
  83. printf("\n");
  84. for (int m = 0; m < M; m++) {
  85. printf("%6d", m+1);
  86. for (int n = 0; n < N; n++) printf("%6.1lf", V[m*N+n].W);
  87. printf("\n");
  88. }
  89. printf("\n");
  90.  
  91. ////// Part 1.4.3: Print C matrix
  92. printf("%6c", 'C');
  93. for (int n = 0; n < N; n++) printf("%6d", n+1);
  94. printf("\n");
  95. for (int m = 0; m < M; m++) {
  96. printf("%6d", m+1);
  97. for (int n = 0; n < N; n++) printf("%6.1lf", V[m*N+n].C);
  98. printf("\n");
  99. }
  100. printf("\n");
  101.  
  102. // Part 2: Calculate Wbar, Qi, VF, U, E:
  103. //// Part 2.1: Calculate Wbar, Qi, VF and U:
  104. for (int m = 0; m < M; m++) {
  105. for (int n = 0; n < N; n++) {
  106. w_bar = 0;
  107. for(int dm = -R; dm <= R; dm++) {
  108. tm = m + dm;
  109. if(tm < 0 || tm >= M) continue;
  110. remained = R - abs(dm);
  111. for(int tn = n-remained; tn <= n+remained; tn++) {
  112. if (tn < 0 || tn >= N) continue;
  113. w_bar += V[tm*N+tn].W;
  114. }
  115. }
  116. zi = m*N+n;
  117. q = w_bar / V[zi].C;
  118. if (q > Qstar) {
  119. VF[U++] = zi;
  120. V[zi].isQ = true;
  121. }
  122. V[zi].Wbar = w_bar;
  123. V[zi].Q = q;
  124. }
  125. }
  126.  
  127. //// Part 2.2: Calculate E
  128. E = new bool*[U];
  129. for (int u = 0; u < U; u++) {
  130. E[u] = new bool[M*N];
  131. zi = VF[u];
  132. cm = V[zi].a;
  133. cn = V[zi].b;
  134. for(int dm = -R; dm <= R; dm++) {
  135. tm = cm + dm;
  136. if(tm < 0 || tm >= M) continue;
  137. remained = R - abs(dm);
  138. for(int tn = cn-remained; tn <= cn+remained; tn++) {
  139. if (tn < 0 || tn >= N) continue;
  140. E[u][tm*N+tn] = true;
  141. }
  142. }
  143. }
  144.  
  145. //// Part 2.3: Print result
  146. printf("U=%d, ", U);
  147. printf("VF:");
  148. for (int u = 0; u < U; u++) printf(" %d", VF[u]);
  149. printf("\n");
  150. printf("\n");
  151. printf("%6s", "W_bar");
  152. for (int n = 0; n < N; n++) printf("%8d", n+1);
  153. printf("\n");
  154. for (int m = 0; m < M; m++) {
  155. printf("%6d", m+1);
  156. for (int n = 0; n < N; n++) printf("%8.1lf", V[m*N+n].Wbar);
  157. printf("\n");
  158. }
  159. printf("\n");
  160. printf("%6s", "Q");
  161. for (int n = 0; n < N; n++) printf("%6d", n+1);
  162. printf("\n");
  163. for (int m = 0; m < M; m++) {
  164. printf("%6d", m+1);
  165. for (int n = 0; n < N; n++) printf("%6.2lf", V[m*N+n].Q);
  166. printf("\n");
  167. }
  168. printf("\n");
  169. printf("\n");
  170. printf("%6s", "E");
  171. for (int i = 0; i < M*N; i++) printf("%3d", i);
  172. printf("\n");
  173. for (int u = 0; u < U; u++) {
  174. printf("%6d", u);
  175. for (int i = 0; i < M*N; i++) printf("%3d", E[u][i]);
  176. printf("\n");
  177. }
  178. printf("\n");
  179.  
  180. // Part 3: Greedy method
  181. for (int cl = 1; cl <= L; cl++) {
  182. // Part 3.1: Select facility with max Wbar in unselected candidate
  183. w_bar_max = 0;
  184. u_max = -1;
  185. for (int u = 0; u < U; u++) {
  186. zi = VF[u];
  187. if(V[zi].isF) continue;
  188. w_bar = V[zi].Wbar;
  189. if(w_bar <= w_bar_max) continue;
  190. w_bar_max = w_bar;
  191. u_max = u;
  192. }
  193. i_max = VF[u_max];
  194. V[i_max].isF = true;
  195. total_satisfied_w += w_bar_max;
  196.  
  197. // Part 3.2: Update isB, Wbar and E
  198. for (int i = 0; i < M*N; i++) { // for each zone
  199. if(!E[u_max][i]) continue; // skip if this zone is not near selected facility
  200. if(V[i].isB) continue; // skip if this zone is assigned
  201. V[i].isB = true; // this zone is assigned to selected faciity
  202. w = V[i].W;
  203. for (int u = 0; u < U; u++) { // for each facility
  204. if(V[VF[u]].isF) continue; // skip already selected facilities
  205. if(!E[u][i]) continue; // skip if assigned zone is not near this facility
  206. V[VF[u]].Wbar -= w; // remove this zone's demand from facility's Wbar
  207. E[u][i] = false; // drop the relationship between the zone and the facility
  208. }
  209. }
  210.  
  211. // Part 3.3: print iteration result
  212. printf("Iteration %3d: select Facility %3d, satisfied %8.1lf demands for zone ", cl, i_max, w_bar_max);
  213. for (int i = 0; i < M*N; i++) if(E[u_max][i]) printf("%d,", i);
  214. printf("\n");
  215. }
  216. // Part 3.4: Print total result
  217. printf("Total satisfied demands: %6.1lf\n", total_satisfied_w);
  218. }
Add Comment
Please, Sign In to add comment