我可以编写使用用户选择的Gale Shapley算法的JS代码吗?

时间:2019-05-28 20:30:02

标签: javascript html algorithm

我正在研究Java脚本代码(Gale Shapley算法)。我想从8个用户输入中读取一些条目,并显示HTML代码的输出。我的输入是:每个用户4个等级(摘要8个用户)输出:夫妇(代码有两个输出,我只需要“夫妇”

试图调试代码并得到错误:无法读取空行57(BOLD IT)的属性'fiance'

function Person(name) {

    var candidateIndex = 0;

    this.name = name;
    this.fiance = null;
    this.candidates = [];

    this.rank = function(p) {
        for (i = 0; i < this.candidates.length; i++)
            if (this.candidates[i] === p) return i;
        return this.candidates.length + 1;
    }

    this.prefers = function(p) {
        return this.rank(p) < this.rank(this.fiance);
    }

    this.nextCandidate = function() {
        if (candidateIndex >= this.candidates.length) return null;
        return this.candidates[candidateIndex++];
    }

    this.engageTo = function(p) {
        if (p.fiance) p.fiance.fiance = null;
        p.fiance = this;
        if (this.fiance) this.fiance.fiance = null;
        this.fiance = p;
    }

    this.swapWith = function(p) {
        console.log("%s & %s swap partners", this.name, p.name);
        var thisFiance = this.fiance;
        var pFiance = p.fiance;
        this.engageTo(pFiance);
        p.engageTo(thisFiance);
    }
}

function isStable(guys, gals) {
    for (var i = 0; i < guys.length; i++)
        for (var j = 0; j < gals.length; j++)
            if (guys[i].prefers(gals[j]) && gals[j].prefers(guys[i]))
                return false;
    return true;
}

function engageEveryone(guys) {
    var done;
    do {
        done = true;
        for (var i = 0; i < guys.length; i++) {
            var guy = guys[i];
            if (!guy.fiance) {
                done = false;
                var gal = guy.nextCandidate();
                **if (!gal.fiance || gal.prefers(guy))**
                    guy.engageTo(gal);
            }
        }
    } while (!done);
}

function doMarriage() {

    var p1  = new Person("Picker1");
    var p2  = new Person("Picker2");
    var p3  = new Person("Picker3");
    var p4  = new Person("Picker4");
    var c1   = new Person("Chosen1");
    var c2 = new Person("Chosen2");
    var c3  = new Person("Chosen3");
    var c4  = new Person("Chosen4");
    //
    var p1o1 = document.getElementById("pref11");
    var p1o2 = document.getElementById("pref12");
    var p1o3 = document.getElementById("pref13");
    var p1o4 = document.getElementById("pref14");
    //
    var p2o1 = document.getElementById("pref21");
    var p2o2 = document.getElementById("pref22");
    var p2o3 = document.getElementById("pref23");
    var p2o4 = document.getElementById("pref24");
    //
    var p3o1 = document.getElementById("pref31");
    var p3o2 = document.getElementById("pref32");
    var p3o3 = document.getElementById("pref33");
    var p3o4 = document.getElementById("pref34");
    //
    var p4o1 = document.getElementById("pref41");
    var p4o2 = document.getElementById("pref42");
    var p4o3 = document.getElementById("pref43");
    var p4o4 = document.getElementById("pref44");
    //////////////////////////////////////////////////
    var c1o1 = document.getElementById("chos11");
    var c1o2 = document.getElementById("chos12");
    var c1o3 = document.getElementById("chos13");
    var c1o4 = document.getElementById("chos14");
    //
    var c2o1 = document.getElementById("chos21");
    var c2o2 = document.getElementById("chos22");
    var c2o3 = document.getElementById("chos23");
    var c2o4 = document.getElementById("chos24");
    //
    var c3o1 = document.getElementById("chos31");
    var c3o2 = document.getElementById("chos32");
    var c3o3 = document.getElementById("chos33");
    var c3o4 = document.getElementById("chos34");
    //
    var c4o1 = document.getElementById("chos41");
    var c4o2 = document.getElementById("chos42");
    var c4o3 = document.getElementById("chos43");
    var c4o4 = document.getElementById("chos44");

    p1.candidates  = [p1o1, p1o2, p1o3, p1o4];
    p2.candidates  = [p2o1, p2o2, p2o3, p2o4];
    p3.candidates  = [p2o1, p3o2, p3o3, p3o4];
    p4.candidates  = [p2o1, p4o2, p4o3, p4o4];
    /////////////////////////////////////////
    c1.candidates  = [c1o1, c1o2, c1o3, c1o4];
    c2.candidates  = [c2o1, c2o2, c2o3, c2o4];
    c3.candidates = [c3o1, c3o2, c3o3, c3o4];
    c4.candidates  = [c4o1, c4o2, c4o3, c4o4];

    var guys = [p1, p2, p3, p4];
    var gals = [c1, c2, c3, c4];

    engageEveryone(guys);

    for (var i = 0; i < guys.length; i++) {
        console.log("%s is engaged to %s", guys[i].name, guys[i].fiance.name);
    }
    console.log("Stable = %s", isStable(guys, gals) ? "Yes" : "No");
    jon.swapWith(fred);
    console.log("Stable = %s", isStable(guys, gals) ? "Yes" : "No");
}

doMarriage();

0 个答案:

没有答案