ここでの最初のコメント擬似コードです:JavaScriptは無限ループを防ぎます - 動的認識パターンを持つ可能性がありますか?
// TODO:
// Person A (80kg) nutrition intake requirements are:
// nutrient || Unit(g)
// Vitamin A : 30,
// Vitamin B1 : 2,
// Vitamin C : 300,
// Vitamin D : 3000,
// Protein : 100,
// Calcium : 30
var nutrient_requirements = {
vita: {min: 30 ,max: 38},
vitb1: {min:2, max:4},
vitc: {min:300, max: 800},
vitd: {min: 300, max:3000},
protein: {min:100, max:200},
calcium: {min:30, max:50}
}
// The calculated estimate amount of food
// the person eats on a daily base is around 900(g)
// The amount will be distributed among the terms
// with a predefined pattern
var food_amount = {
fruits:[200,200],
meat: [500]
}
// START (usefull anchor for this pseudocode)
var calculated_nutrition = {
vita: 0,
vitb1: 0,
vitc: 0,
vitd: 0,
protein: 0,
calcium: 0
}
// The daily nutrition intake of this person
// needs to be achieved by the following terms:
// apple, banana, chicken breast
// Term nutrient values per gramm
var terms = {
fruits:[
apple:{
vita: 0.02,
vitc: 0.30,
vitd: 0.01,
protein: 0.08,
calcium: 0
},
banana:{
vita: 0.1,
vitc: 0.09,
vitd: 0.00,
protein: 0.1,
calcium: 0.2
}
],
meat:[
chicken_breast:{
vita: 0.07,
vitc: 0.08,
vitd: 0.03,
protein: 0.4,
calcium: 0.2
}
]
}
// Now we want to see if the normal amount and distribution
// of the food covers the required amount
// To do that we need to multiply the matching food amount
// with the matching food/term and sum up all values
for(let prop in terms){
for(let i = 0; i < terms[prop].length; i++){
for(let propb in terms[prop][i]){
calculated_nutrition[propb] = terms[prop][i][propb] * food_amount[prop][i];
}
}
}
// After that is done, we can compare calculated_nutrition to
// nutrient_requirements to see whether something is too much
// or too little
for(let propa in nutrient_requirements){
if(nutrient_requirements[propa].min > calculated_nutrition[propa]){
// this nutrient level is too little
// now we need to increase some food/term
// in order to achieve the required minimum
// of this nutrient
alter_amount(propa, "up");
return;
}else if(nutrient_requirements[propa].max < calculated_nutrition[propa]){
// this nutrient level is too high
// now we need to decrease some food/term
// in order to achieve the required minimum
alter_amount(propa, "down");
return;
}else{
// this nutrient level is ok
return;
}
}
function alter_amount(prop, direction){
// here we look in terms which food
// has the highest amount of prop
switch(direction){
case "down":
// here we decrease the amount of
// the matching term in the amount object
// and re-run this whole calculation from
// point START
break;
case "up":
// here we increase the amount of
// the matching term in the amount object
// and re-run this whole calculation from
// point START
break;
}
}
は、私は簡単にこの例を説明しましょう。
計算された期待される結果は、毎日の栄養目標を達成するために、人は1日にX量のリンゴ、Y量のバナナおよび鶏の胸部を1日に食べる必要があるということです。
私の擬似コードでは、私のプログラムの基本的な機能を書き留めました。私が直面している現在の問題は、ある特定の食べ物が、あるループで量が増えるのにふさわしい場合、別のループの量を減らすのに最適です - 私は無限ループに終わります。
私の最小限の例に基づいて、アップルの量が増えれば、それは次のループで減少しないはずですが、私の実際のプログラムでは、より多くのプロパティ。したがって、それをカバーする複雑さは非常に高くなります。
パターンを認識するための方法を探していますが、結果が得られず、無限ループで終わらないように、2番目に優れた食べ物を増減させるよう指示します。
このような何かを回避されることを:
apple ++
apple ++
banana ++
apple ++
banana ++
meat --
apple --
apple --
banana --
apple --
banana --
meat ++
apple ++
apple ++
banana ++
apple ++
banana ++
meat --
apple --
apple --
banana --
apple --
banana --
meat ++
...
EDIT
いくつかのハッシュを促進し、システムを格納下記の答えは、私は私のカスタムブラックリスト方式で発生する同じ結果につながります。
私のブラックリスト方式は、次のように動作します
アップルの量は、(減少/増加)に変更されました - >配列をブラックリストに載せることを保存します。
blacklist = [{product: "apple", altered: "down/up"},...]
増分または減分する食品を選択する前のすべてのループごとに、ブラックリストがスキャンされます。完璧なフィットが配列内にある場合は、2番目に最適なフィットが選択されます。
次のようないくつかの追加の制限があります。りんごは総量のx%を超えてはならない。またはリンゴは総量のx%以上であってはならない。進行なしの制約の組み合わせ+で
私のプログラムは、それが増減するためにこれ以上の異なる食品を持っていないし、単に何も変更しないと無限ループに終了状態で終了しブラックリストに載った製品
Iドンその問題をプログラマチックに解決する方法があるかどうかを知ることさえありません。あるいは、ちょっと、ちょっと、その問題は解決できません。
私が考えているのは、機能が実装されていることをプログラムが認識していることを認識していることです。 - >固まった状態につながるパターンを記憶して、別のアプローチで再度試みます。しかし、それは考え過ぎるかもしれません。
スタックオーバーフローのガイドラインでは、コードを質問に入れる必要があります。質問を理解するため、および/または回答を形成するために必要なコードは、質問自体に含まれていなければなりません。あなたがjsFiddleで持っているコードの量は、あまりにも多くのコードを直接質問に入れることではありません。この理由は、外部リンクが消えたり変更されたりする傾向があるため、将来の参照ソースとして役立たずになります。 – jfriend00
ありがとう私はコードを追加しました。 –
サンプルテストケースを入力してください(入力と希望出力)。 –