Javascript 你们如何正确地分配儿童?

Javascript 你们如何正确地分配儿童?,javascript,arrays,algorithm,sorting,genetic-algorithm,Javascript,Arrays,Algorithm,Sorting,Genetic Algorithm,我有一个父母数组,我想生孩子,还有一个空数组(我需要固定长度)等待填充 我需要婴儿——孩子——根据他们父亲的英俊程度平均分配;然而,我需要每个人都得到一个克隆体,以防他们的突变孩子更丑/更美,然后还有另一个机会……(parents.length关于这个问题的法律含义,你应该联系律师,但我想我可以帮你解决这个问题的技术方面;) 卡米诺(阿尔法体育场)工厂的蓝图: var int=v=>0 | v; //主要创建平均值和低于平均值的值,较少创建高值 var randomHandsomeness=()

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


我需要婴儿——孩子——根据他们父亲的英俊程度平均分配;然而,我需要每个人都得到一个克隆体,以防他们的突变孩子更丑/更美,然后还有另一个机会……(
parents.length关于这个问题的法律含义,你应该联系律师,但我想我可以帮你解决这个问题的技术方面;)

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

var int=v=>0 | v;
//主要创建平均值和低于平均值的值,较少创建高值
var randomHandsomeness=()=>int(Math.pow(Math.random(),2)*100)+1;
var breedClone=(父项)=>({
id:int(Math.random()*0x8000000).toString(36),//没有时间写名字
漂亮:int(.25*parent.handsomeness+.75*randomHandsomeness()),//有点遗传,但大部分是运气
父:父//仅用于记录
});
var deriveBatch=(父代、克隆繁殖、minChildrenPerParent、分布)=>{
console.log(“开始批次”,numClonesToBreed);
if(minChildrenPerParent的类型==“函数”){
distribution=minChildrenPerParent;
minChildrenPerParent=0;
}
if(分布类型!=“功能”){
分布=(漂亮)=>漂亮;
}
minChildrenPerParent=Math.max(int(minChildrenPerParent),0);
//我将把这些添加回循环中
numClonesToBreed-=parents.length*minChildrenPerParent;
if(numClonesToBreed<0){
抛出新错误(“增加批量,这些规格的容量不足”);
}
//顺序不重要,只是漂亮与总漂亮的关系
var totalHandsomeness=父母减少((acc,p)=>acc+分布(p.handsomeness),0);
返回父级。reduce((newBatch,parent)=>{
//这种计算不仅计算父对象相对于组的相对舒适度,
//还有他应得的孩子数量,但也平衡了三角洲
//在计算值和取整的numChildren之间
//(部分克隆没有用处,因此避免使用)。
//同时,重要的是既不能过度生产,也不能低于目标。
var handomeness=分布(parent.handomeness);
var numChildren=Math.round(漂亮/完全漂亮*numClonesToBreed);
总体美貌-=美貌;
Numclonestorede-=儿童;
//将每个父项的最小子项数量添加到计算/分发的numChildren中
numChildren+=minChildrenPerParent;
log(“handsomeness:%i,children:%i”,parent.handsomeness,numChildren);
而(numChildren--)newBatch.push(breedClone(parent));
返回新批次;
}, []);
}
让生产开始:

//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));
//准备第一批
var parents=deriveBatch([{
身份证:“詹戈·费特”,
美貌:75,
父项:null//文件已损坏
}], 10);
//从父批次中培育新克隆
//创建30个克隆
//每个父级至少创建一个克隆
//还有一个权重函数,即漂亮程度对孩子数量的影响
//pow<1:英俊的人有更多的孩子,但不是更多
//pow=1:漂亮度与分布的线性相关性
//pow>1:帅哥生的孩子明显多
var childrens=deriveBatch(父母,30,1,漂亮=>Math.pow(漂亮,1.25));
免责声明:本机构不对所生成克隆在任何外部命令下执行的任何操作负责


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

如何正确分布子对象?
这听起来更像是一个法律问题,而不是编程问题。听起来在道德上也有点可疑。我不得不对这个问题和你的评论@SamAxe投了反对票。前提是历史性的。哈哈,我需要孩子。LOL父数组是按美观程度排序的,所以
parents[0]=me
整个问题都是秘密的贩卖广告。你知道你的软件永远不会工作,对吗?所有的父母都是爸爸。。虽然在某些地方这在社会上是可以接受的。。无论您的位置、政治、道德等如何,这都是不可能复制的。
makeABabyWith(aRandomDaddy)
看这就是当今社会的问题伟大的解决方案,非常优雅您能在javascript/console.log()中进行“%i”类型替换吗?还是像我注意到的那样,它是ES6的一个功能?您使用了一些lambda函数。@Tobiq
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);
    }
...
...
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");
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;
    }, []);
}
//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));