2016-09-30 4 views
3

私は赤ちゃんを作りたい親の配列を持っていて、空の配列(そのうちの固定長である必要があります)は満たされるのを待っています。どのように子供たちを適切に配っていますか?

私は赤ちゃんが必要です - 子供 - どのようにハンサムな父親に基づいて均等に配布する;しかし、私は突然変異した子が醜い/美しい人であれば誰もがクローンを取得する必要があります。次に

親配列は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); 
    } 
... 

さて、これは親の彼らのpercantageへの分配を相対的に与えます。 しかし、フロアリングが発生すると、時々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その正確な同じ原理で書かれているが:私はこれに対抗考え

一つの方法はiteritively、このような何かを、このforeloopを実行することです。

この質問を十分に書いているうちに思っていた方法ですが、どうやったらいいですか?

編集:最後の例は、childrenToBeCreatedのパーセントを使用することを意図していませんでした。ptotalではなく、私は混乱していると思います。この質問あなたは弁護士に連絡する必要がありますが、私は、問題の技術的な側面であなたを助けることができると思うの法的意味について

+12

「あなたはどのように子供たちを適切に頒布していますか?」これはプログラミング上の問題ではなく法的な質問のように聞こえます。少しでも道徳的に疑わしいと聞こえる。 –

+3

私はこの質問とあなたのコメント@SamAxeをupvoteしなければならなかった。前提は、神聖である。 LOL「私は赤ちゃんが必要です」 LOL親配列はハンサムでソートされていますので、 'parents {0} = me' –

+5

この質問は密かに人身売買広告です。 –

答えて

0

;)神野(アルファスタジアム)の工場のための

青写真:

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)); 

免責事項:この機能ではありませんいかなる外国命令の下でも生産されたクローンによって行われた行為に対して責任を負う。

私はコードのほとんどがそれ自身を説明し、コードベースに簡単に移植/適用されるべきだと思います。 algoの動作/分布を操作できるようにいくつかのフックと設定を追加しました。

+0

素晴らしい解決策、それは本当にエレガントです –

+0

javascript/console.log()で "%i"型の置換えできますか?それとも、ES6の機能ですが、ラムダ関数を使用していることに気付きました。 –

+0

@Tobiq [MDN:コンソールUsing_string_substitutions](https://developer.mozilla.org/en-US/docs/Web/API/Console#Using_string_substitutions) – Thomas

関連する問題