我有一个父母数组,我想让孩子,和一个空数组(我需要一个固定的长度)等待填充。
我需要婴儿——孩子——根据他们的父亲有多帅平均分配;然而,我需要每个人都得到一个克隆,以防他们的突变孩子更丑/更漂亮,然后还有另一个机会…(parents.length <= children.length
)
父数组按帅度排序,所以parents[0] = me;
。到目前为止,我所做的是:
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
次,并考虑到爸爸有多帅。
我想做:
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,那么你的后代可以有很大的范围。
我想解决这个问题的一种方法是迭代地运行这个前循环,就像这样:...
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
,我想我搞混了。
关于这个问题的法律含义,你应该联系律师,但我认为我可以帮助你解决这个问题的技术方面;)
Kamino上的工厂蓝图(alpha体育场):
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));
免责声明:本设施不对生成的克隆在任何外部命令下执行的任何操作负责。
我认为大多数代码应该解释自己,应该很容易移植/应用到你的代码库。添加了一些钩子和配置,以便能够操纵算法的行为/分布