0

私は、特定の条件下では正常に再帰し、他の条件下では確率e ^(E / Temperature)で再帰する必要がある再帰検索関数を設計しています。特定の確率で何かを実行する方法がわからないため、再帰的な手順を除いてすべてのコードが実行されます

Node Search(Node start)
{//first, calculate temperature. count will keep timestep
    count++;
    double temperature = 1000 * (Math.Pow(.995,count));//CALCULATES TEMP

    for (int i = 0; i < start.state.Length; i++)
    {
        string temp = StateReturn(start.state, i);
        if (temp.Length > 1 && temp != start.state 
            &&visited.Contains(temp) == false)
        {
            list.Add(new Node(start, temp));
            visited.Add(temp);
        }
    }
    //add all relevant nodes to list.
    Random gen = new Random();
    int rand = gen.Next(list.Count);//think this should work
    //random number has been taken. now just to pull rand node from list 
    Node next = list.ElementAt(rand);
    list.RemoveAt(rand);
    double E = -(next.wrongNum - start.wrongNum); //we want less wrong 
    // if next has 
    if (E> 0)
    {
        //standard recursion

    }
    else //recurse with probability e^(E/t)
    {

    }
}
4

1 に答える 1

8

私は、特定の条件下では正常に再帰し、他の条件下では確率e ^(E / Temperature)で再帰する必要がある再帰検索関数を設計しています。特定の確率で何かを実行する方法がわからないため、再帰的な手順を除いてすべてのコードが実行されます

より一般的にして、質問を単純化しましょう。

私は、確率p(ここで0 <= p <= 1)で1つのことを実行し、確率で別のことを実行する関数を設計しています1 - p。ある確率で何かを実行させる方法がわかりません。

0から1までの乱数を選択してください。すでにRandom変数にオブジェクトがあるgenので、

double result = gen.NextDouble(); // Produce a random double between zero and one.

そして今、あなたはそれを使ってあなたの選択をすることができます:

if (result <= p)
    DoSomething();
else
    DoSomethingElse();

DoSomething()確率で行われ、確率pDoSomethingElse()行われ1 - pます。

わかる?

于 2013-02-19T22:23:31.027 に答える