Advertisement
Guest User

code qui marche pas c'est trop triste

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.Stack;
  3.  
  4. public class StableMatching implements StableMatchingInterface {
  5.  
  6.     public int[][] M;
  7.  
  8.     StableMatching() {
  9.         this.M = null;
  10.     }
  11.  
  12.     StableMatching(int[] menGroupCount, int[] womenGroupCount, int[][] menPrefs, int[][] womenPrefs) {
  13.         this.M = constructStableMatching(menGroupCount, womenGroupCount, menPrefs, womenPrefs);
  14.     }
  15.  
  16.     /*
  17.      * public int[][] constructStableMatchingC(int[] menGroupCount, int[]
  18.      * womenGroupCount, int[][] menPrefs, int[][] womenPrefs) { int m =
  19.      * menGroupCount.length; int w = womenGroupCount.length; int[][] M = new
  20.      * int[m][w]; int nb_men = 0; for (int i = 0; i < m; i++) nb_men +=
  21.      * menGroupCount[i]; int[] indexWomen = new int[nb_men]; LinkedList<Integer> men
  22.      * = new LinkedList<Integer>(); int[] WomenGroom = new int[nb_men]; for (int i =
  23.      * 0; i < nb_men; i++) { men.add(i); indexWomen[i] = 0; WomenGroom[i] = -1; }
  24.      *
  25.      * int[][] positionGroom = new int[w][m]; for (int i = 0; i < w; i++) { for (int
  26.      * j = 0; j < m; j++) { positionGroom[i][womenPrefs[i][j]] = j; } } while
  27.      * (!(men.isEmpty())) { int g = men.poll(); int f = menPrefs[g][indexWomen[g]];
  28.      * indexWomen[g] += 1; if (WomenGroom[f] == -1) { M[g][f] = 1; WomenGroom[f] =
  29.      * g; } else { int g2 = WomenGroom[f]; if (positionGroom[f][g] <
  30.      * positionGroom[f][g2]) { men.addFirst(g2); WomenGroom[f] = g; M[g][f] = 1;
  31.      * M[g2][f] = 0; } else { men.addFirst(g); }
  32.      *
  33.      * }
  34.      *
  35.      * } return M;
  36.      *
  37.      * }
  38.      *
  39.      * public int[][] constructStableMatchingB(int[] menGroupCount, int[]
  40.      * womenGroupCount, int[][] menPrefs, int[][] womenPrefs) { int m =
  41.      * menGroupCount.length; int w = womenGroupCount.length; if (m == 0) return new
  42.      * int[0][0]; int nb_men = 0; for (int i = 0; i < m; i++) nb_men +=
  43.      * menGroupCount[i]; int[] menGroupCount2 = new int[nb_men]; int[]
  44.      * womenGroupCount2 = new int[nb_men]; int[][] womenPrefs2 = new
  45.      * int[nb_men][nb_men]; int[][] menPrefs2 = new int[nb_men][nb_men]; for (int i
  46.      * = 0; i < nb_men; i++) { womenGroupCount2[i] = 1; menGroupCount2[i] = 1; }
  47.      * int[] indices_h = new int[m]; // tableau donnant les indices des groupes
  48.      * d'hommes int[] indices_f = new int[w]; indices_h[0] = 0; indices_f[0] = 0;
  49.      * for (int i = 1; i < m; i++) { indices_h[i] = menGroupCount[i - 1] +
  50.      * indices_h[i - 1]; } for (int i = 1; i < w; i++) { indices_f[i] =
  51.      * womenGroupCount[i - 1] + indices_f[i - 1]; } for (int i = 0; i < w; i++) {
  52.      * for (int j = 0; j < womenGroupCount[i]; j++) { int b = 0; for (int l = 0; l <
  53.      * m; l++) { int a = womenPrefs[i][l]; int indice = indices_h[a]; for (int z =
  54.      * 0; z < menGroupCount[a]; z++) womenPrefs2[j + indices_f[i]][z + b] = indice +
  55.      * z; b += menGroupCount[l]; } } } for (int i = 0; i < w; i++) { for (int j = 0;
  56.      * j < menGroupCount[i]; j++) { int b = 0; for (int l = 0; l < m; l++) { int a =
  57.      * menPrefs[i][l]; int indice = indices_f[a]; for (int z = 0; z <
  58.      * womenGroupCount[a]; z++) menPrefs2[j + indices_h[i]][z + b] = indice + z; b
  59.      * += womenGroupCount[l]; } } } int[][] M =
  60.      * constructStableMatchingC(menGroupCount2, womenGroupCount2, menPrefs2,
  61.      * womenPrefs2); int[][] N = new int[m][w]; for (int i = 0; i < m; i++) { for
  62.      * (int j = 0; j < menGroupCount[i]; j++) { for (int l = 0; l < w; l++) { for
  63.      * (int z = 0; z < womenGroupCount[l]; z++) N[i][l] += M[j + indices_h[i]][z +
  64.      * indices_f[l]]; }
  65.      *
  66.      * } } return N; }
  67.      */
  68.  
  69.     public int[][] constructStableMatching(int[] menGroupCount, int[] womenGroupCount, int[][] menPrefs,
  70.             int[][] womenPrefs) {
  71.        
  72.         int m = menGroupCount.length;
  73.         int w = womenGroupCount.length;
  74.         int[][] M = new int[m][w];
  75.  
  76.         if (w == 0 && m == 0)
  77.             return M;
  78.  
  79.         int[] indexWomen = new int[m]; // indique l'indice des groupes de femmes auxquels chaque groupe d'homme doit
  80.                                         // s'intéresser par la suite
  81.         int[][] positionGroom = new int[w][m]; // indique le rang de chaque groupe d'homme dans les préférences de
  82.                                                 // chaque groupe de femmes
  83.         int[] worseMen = new int[w]; // indique le rang du pire mari pour chaque groupe de femmes ; par défaut, m pour des femmes célibataires
  84.         int[][] womenGroom = new int[w][m + 1]; // donne le nombre de femmes du groupe i mariées au groupe j (on ajoute
  85.                                                 // une colonne supplémentaire pour les femmes célibataires)
  86.         int[] menCelibs = new int[m]; // donne le nombre de célibataires restant par groupe d'hommes
  87.         int total_unengaged = 0;
  88.         Stack<Integer> men = new Stack<Integer>();
  89.  
  90.         for (int i = 0; i < w; i++) {
  91.             worseMen[i] = m;
  92.             womenGroom[i][m] = womenGroupCount[i]; // toutes les femmes sont initialement célibataires
  93.         }
  94.         for (int j = 0; j < m; j++) {
  95.             menCelibs[j] = menGroupCount[j];
  96.             indexWomen[j] = 0;
  97.             total_unengaged += menGroupCount[j];
  98.         }
  99.         for (int i = 0; i < w; i++) {
  100.             for (int j = 0; j < m; j++) {
  101.                 positionGroom[i][womenPrefs[i][j]] = j;
  102.                 womenGroom[i][j]=0;
  103.             }
  104.         }
  105.         for (int i = 0; i < m; i++) {
  106.             if (menCelibs[i] > total_unengaged / (2 * m))
  107.                 men.push(i);
  108.         }
  109.  
  110.         int tmp = men.pop();
  111.         while (total_unengaged > 0) {
  112.             int groupf = menPrefs[tmp][indexWomen[tmp]];
  113.             int rank = worseMen[groupf];
  114.             int groupMen2 = -1;
  115.             if (rank != m)
  116.                 groupMen2 = womenPrefs[groupf][rank];
  117.             else
  118.                 groupMen2 = m;
  119.             if (rank > positionGroom[groupf][tmp]) {
  120.                 int newMarriages = Math.min(menCelibs[tmp], womenGroom[groupf][groupMen2]);
  121.                 womenGroom[groupf][tmp] += newMarriages;
  122.                 womenGroom[groupf][groupMen2] -= newMarriages;
  123.                 menCelibs[tmp] -= newMarriages;
  124.                 if (groupMen2 != m)
  125.                     menCelibs[groupMen2] += newMarriages;
  126.                 if (groupMen2 == m) {
  127.                     total_unengaged -= newMarriages;
  128.                 }
  129.                 if (womenGroom[groupf][groupMen2] == 0) {
  130.                     rank--;
  131.                     while (womenGroom[groupf][womenPrefs[groupf][rank]] == 0)// on actualise le pire mari pour groupf
  132.                                                                                 // // groupf
  133.                         rank--;
  134.                     worseMen[groupf] = rank;
  135.                 }
  136.             } else
  137.                 indexWomen[tmp]++;
  138.             if (men.isEmpty() && total_unengaged > 0) { // on remplit la pile avec les hommes dont le groupe possède une
  139.                                                         // fraction suffisante de célibataires
  140.                 for (int i = 0; i < m; i++) {
  141.                     if (menCelibs[i] > total_unengaged / (2 * m))
  142.                         men.push(i);
  143.                 }
  144.             }
  145.             if (!(men.isEmpty()))
  146.                 tmp = men.pop();
  147.         }
  148.  
  149.         for (int i = 0; i < m; i++) {
  150.             for (int j = 0; j < w; j++)
  151.                 M[i][j] = womenGroom[j][i];
  152.         }
  153.         return M;
  154.     }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement