在Java

时间:2019-11-18 01:15:30

标签: java arrays algorithm multidimensional-array

根据您的喜好,我有一个程序对应于n位男性[0,1,2,...,n-1]到n位女性[0,1,2,...,n-1]。这些偏好用n x n矩阵表示,一个矩阵表示男性,一个矩阵表示女性。 我想确保匹配尽可能地理想。这可以通过使用Gale-Shapley算法来完成。

我是Java新手,正在尝试执行Gale Shapley算法代码,但是在执行此代码时,出现以下错误:

java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5
    at br.com.entrada.GaleShapley.calcMatches(GaleShapley.java:45)
    at br.com.entrada.GaleShapley.<init>(GaleShapley.java:32)
    at br.com.entrada.GaleShapley.main(GaleShapley.java:155)
Error: Index -1 out of bounds for length 5

下面是正在执行的代码:


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

public class GaleShapley {
    private int N, engagedCount;
    private String[][] menPref;
    private String[][] womenPref;
    private String[] men;
    private String[] women;
    private String[] womenPartner;
    private boolean[] menEngaged;

    /** Constructor **/
    public GaleShapley() {

    }

    public GaleShapley(String[] m, String[] w, String[][] mp, String[][] wp) {
        N = mp.length;
        engagedCount = 0;
        men = m;
        women = w;
        menPref = mp;
        womenPref = wp;
        menEngaged = new boolean[N];
        womenPartner = new String[N];
        calcMatches();
    }

    /** function to calculate all matches **/
    private void calcMatches() {
        while (engagedCount < N) {
            int free;
            for (free = 0; free < N; free++)
                if (!menEngaged[free])
                    break;

            for (int i = 0; i < N && !menEngaged[free]; i++) {
                int index = womenIndexOf(menPref[free][i]);
                if (womenPartner[index] == null) {
                    womenPartner[index] = men[free];
                    menEngaged[free] = true;
                    engagedCount++;
                } else {
                    String currentPartner = womenPartner[index];
                    if (morePreference(currentPartner, men[free], index)) {
                        womenPartner[index] = men[free];
                        menEngaged[free] = true;
                        menEngaged[menIndexOf(currentPartner)] = false;

                    }
                    engagedCount++;

                }

            }
        }
        printCouples();
    }

    /** function to check if women prefers new partner over old assigned partner **/
    private boolean morePreference(String curPartner, String newPartner, int index) {
        for (int i = 0; i < N; i++) {
            if (womenPref[index][i].equals(newPartner))
                return true;
            if (womenPref[index][i].equals(curPartner))
                return false;
        }
        return false;
    }

    /** get men index **/
    private int menIndexOf(String str) {
        for (int i = 0; i < N; i++)
            if (men[i].equals(str))
                return i;
        return -1;
    }

    /** get women index **/
    private int womenIndexOf(String str) {
        for (int i = 0; i < N; i++) {
            if (women[i].equals(str))
                return i;
        }

        return -1;
    }

    /** print couples **/
    public void printCouples() {
        System.out.println("Couples are : ");
        for (int i = 0; i < N; i++) {
            System.out.println(womenPartner[i] + " " + women[i]);
        }
    }

    /** main function **/
    public static void main(String[] args) {
        System.out.println("Gale Shapley Marriage Algorithm\n");
        /** list of men **/
        String[] m = { "0", "1", "2", "3", "4" };
        /** list of women **/
        String[] w = { "0", "1", "2", "3", "4" };

        /** men preference **/

        String[][] mp = null;
        /** women preference **/
        String[][] wp = null;

        try {
            FileInputStream fstream = new FileInputStream("src\\dados.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int line = 0;
            int n = 0;
            int i = 0;
            while ((strLine = br.readLine()) != null) {


                String input = strLine.trim();
                if (line == 0) {
                    n = Integer.valueOf(input);

                    mp = new String[n][n];
                    wp = new String[n][n];
                    line++;
                } else {
                    if (strLine != null && !strLine.equals("") && !strLine.contains("male")
                            && !strLine.contains("female")) {
                        String[] preferences = strLine.split("\\s+ ");

                        if (i < n) {
                            mp[i] = preferences;
                        } else {
                            if (i - n < w.length) {
                                wp[i - n] = preferences;

                            }
                        }
                        i++;
                    }

                }
            }
            in.close();

            new GaleShapley(m, w, mp, wp);

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());
        }
    }

}

以下是以下data.txt文件的格式:

RANDOM INSTANCE STABLE MARIAGE
N      5
MEN'S PREFERENCES
   3    1    2    0    4 
   0    2    1    3    4 
   2    0    4    3    1 
   4    2    3    1    0 
   3    1    4    0    2 
WOMEN'S PREFERENCES
   4    0    1    2    3 
   3    4    2    0    1 
   1    3    2    0    4 
   3    1    2    0    4 
   4    2    0    1    3 

我无法确定并解决这个问题。

0 个答案:

没有答案