0

I am designing a recursive search function that, under certain conditions, will recurse normally, and under other conditions, must recurse with probability e^(E/Temperature). All the code is done except the recursive steps, because I cannot figure out how to make something execute according to a certain probability

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

    }
}
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
  • 1
    You need to describe what you mean by: "with probability e^(E/t)". Are you saying the odds of recursion should be 1 in e^(E/t)? – Spencer Ruport Feb 19 '13 at 22:15
  • @Spencer No, that should actually be clear, at least it’s standard notation in probability theory. The probability *P* of recursing is *P* = e^(*E* / *t*) – Konrad Rudolph Feb 19 '13 at 22:25
  • @SpencerRuport: To clarify Konrad's statement: when someone says "with probability p" they mean that p is a number between zero and one, where zero is no chance, 1 is certainty, 0.25 is one in four, and so on. – Eric Lippert Feb 19 '13 at 22:57
  • Iit is generally preferable to avoid creating new instances of `Random` at each recursion step. Note that `Random`'s default constructor is documented to use a time-dependent value as the seed (e.g., in .Net 4, it uses `Environment.TickCount`). If your recursive algorithm runs fast enough, you will end up using the same seed for each instance of `Random`. If you prefer not to code this yourself, you could just use Jon Skeet's code: http://msmvps.com/blogs/jon_skeet/archive/2009/11/04/revisiting-randomness.aspx . – Brian Feb 20 '13 at 19:05

1 Answers1

8

I am designing a recursive search function that, under certain conditions,will recurse normally, and under other conditions, must recurse with probability e^(E/Temperature). All the code is done except the recursive steps, because I cannot figure out how to make something execute according to a certain probability

Let's simplify your question by making it more general.

I am designing a function that will do one thing with probability p (where 0 <= p <= 1) and something else with probability 1 - p. I cannot figure out how to make something execute according to a certain probability.

Choose a random number between zero and one. You already have a Random object in variable gen, so just call

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

and now you can use that to make your choice:

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

DoSomething() is done with probability p, and DoSomethingElse() is done with probability 1 - p.

Make sense?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067