首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确分配孩子?

如何正确分配孩子?
EN

Stack Overflow用户
提问于 2016-10-01 03:59:36
回答 1查看 173关注 0票数 3

我有一个想要生孩子的父母数组,还有一个等待填充的空数组(我需要其中的一个固定长度)。

我需要婴儿-孩子-根据他们的父亲有多帅来平均分配;然而,我需要每个人都得到一个克隆,以防他们突变的孩子更丑/更漂亮,然后就会有另一次机会……(parents.length <= children.length)

父数组是按外观排序的,所以是parents[0] = me;。到目前为止,我所做的是:

代码语言:javascript
复制
for (var p = parents.legth; parent--;) {
    var myself = parents[p],
        aRandomDaddy = parents[~~(Math.random()*parents.length)]
        iDeserveMoreThan1Child = parents.length-p;
        // if this is 0 they're last in the array and just get their 1 clone. Should be different, right?

    makeABabyWith(myself);
    if (iDeserveMoreThan1Child) {
        makeABabyWith(aRandomDaddy);
    }
}

我现在想做的是找出一种算法来计算makeABabyWith(aRandomDaddy)children.length - parents.length时间,并考虑到爸爸们有多帅。

我已经想过要做的事情:

代码语言:javascript
复制
for(var c = parents.length, totalHandsomeness = 0; c--;)
    totalHandsomeness+= parents[c].handsomeness;
...

    makeABabyWith(myself);
    for (var handsomenessComparedToEveryoneElse
         = ~~(myself.handsomeness * children.length / totalHandsomeness);
         handsomenessComparedToEveryoneElse--;) {
        makeABabyWith(aRandomDaddy);
    }
...

现在这给出了一个相对于他们的百分比的分布,父母。然而,当地板出现时,你有时会得到0。因此,如果子数组的长度为20,那么您的子数组的范围可以非常宽。

我想要解决这个问题的一种方法是迭代地运行这个前循环,就像这样:

代码语言:javascript
复制
...
var childrenToBeCreated = children.length - parents.length;
...

    makeABabyWith(myself);

    while (childrenToBeCreated) for (var handsomenessComparedToEveryoneElse
         = ~~(myself.handsomeness * children.length / totalHandsomeness);
        handsomenessComparedToEveryoneElse--;) {
        if (childrenToBeCreated) {
            makeABabyWith(aRandomDaddy);
            childrenToBeCreated--;
        } else break;
    }

//EDIT: realised this would run to the end in the first loop and break, instead of run through all and check for left overs.
//EDIT OF THE EDIT: no it wouldn't...
//ED...: Yes it would, the while loops inside the for loop.
...

console.log("is","this"+"a good way to do it?? would this even work");

尽管这是用JS编写的,但它在任何语言中都是完全相同的原则。

我在写这个问题时想出的方法足够了吗,你会怎么做?

编辑:最后一个例子是使用childrenToBeCreated的百分比,而不是ptotal,我想我搞混了。

EN

回答 1

Stack Overflow用户

发布于 2016-10-01 06:27:00

关于这个问题的法律含义,你应该联系律师,但我想我可以帮助你解决这个问题的技术方面;)

卡米诺(阿尔法体育场)工厂的蓝图:

代码语言:javascript
复制
var int = v => 0|v;

//creates mostly average and below values, and fewer high values
var randomHandsomeness = () => int( Math.pow(Math.random(), 2) * 100 ) + 1;

var breedClone = (parent) => ({ 
    id: int(Math.random() * 0x80000000).toString(36),  //no time for names
    handsomeness: int( .25 * parent.handsomeness + .75 * randomHandsomeness() ), //a bit genetic heritage but mostly luck
    parent: parent //just for the record
});

var deriveBatch = (parents, numClonesToBreed, minChildrenPerParent, distribution) => {
    console.log("starting batch of", numClonesToBreed);

    if(typeof minChildrenPerParent === "function"){
        distribution = minChildrenPerParent;
        minChildrenPerParent = 0;
    }

    if(typeof distribution !== "function"){
        distribution = (handsomeness) => handsomeness;
    }

    minChildrenPerParent = Math.max(int(minChildrenPerParent), 0);

    //I'll add these back in the loop
    numClonesToBreed -= parents.length * minChildrenPerParent; 
    if(numClonesToBreed < 0){
        throw new Error("increase batch size, insufficient capacities for these specs");
    }

    //order doesn't matter, only handsomeness in relation to the total handsomeness
    var totalHandsomeness = parents.reduce((acc, p) => acc + distribution( p.handsomeness ), 0); 

    return parents.reduce((newBatch, parent) => {   
        //this computation doesn't only compute the relative handsomeness of the parent to the group,
        //and the amount of children he's entitled to, but also balances the delta 
        //between the computed value and the rounded numChildren
        //(partial clones are of no use and are therefore avoided).
        //At the same time it's important to neither overproduce nor stay short of the goal.
        var handsomeness = distribution( parent.handsomeness );
        var numChildren = Math.round( handsomeness / totalHandsomeness * numClonesToBreed );
        totalHandsomeness -= handsomeness;
        numClonesToBreed -= numChildren;

        //add the minimum amount of children per parent to the computed/distributed numChildren
        numChildren += minChildrenPerParent;
        console.log("handsomeness: %i, children: %i", parent.handsomeness, numChildren);

        while(numChildren--) newBatch.push( breedClone( parent ) );
        return newBatch;
    }, []);
}

让生产开始吧:

代码语言:javascript
复制
//prepare a first batch
var parents = deriveBatch([{
    id: "Jango Fett",
    handsomeness: 75,
    parent: null //file corrupted
}], 10);

//breed new clones from the parent batch
//create 30 clones
//create at least 1 clone per parent
//and a weighting function how the handsomeness impacts the amount of children
//pow < 1: handsome people get more children, but not that much more
//pow = 1: linear correlation of handsomeness to distribution
//pow > 1: handsome people get significantly more children
var children = deriveBatch(parents, 30, 1, handsomeness => Math.pow(handsomeness, 1.25));

免责声明:对于在任何外来命令下生成的克隆所执行的任何操作,此工具不承担任何责任。

我认为大多数代码应该可以自己解释,并且应该很容易移植/应用到您的代码库中。添加了一些钩子和配置,以便能够操纵算法的行为/分布。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39799233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档