Esempio n. 1
0
    /// <summary>
    /// Simulated Annealing algorithm : begin with start node, choose random child, calculate childs evaluation
    /// if evaluation better than before, go with child, else check if child passes acceptor function.
    /// algorithm does random walk to maximum
    /// </summary>
    /// <param name="start"> starting junction - can reduce proccess </param>
    /// <param name="t"> starting temprature </param>
    /// <param name="end_t"> ending temprature </param>
    /// <param name="limit"> epochs for each temprature </param>
    public static Junction Compute(Junction start, float t, float end_t, int limit)
    {
        Junction jen      = start;
        Junction best_jen = start;

        System.Random rand = new System.Random();
        int           eval = start.Eval();

        while (t > end_t)
        {
            for (int i = 0; i < limit; i++)
            {
                Junction jen_new   = jen.Choose_Random_Successor();
                int      eval_new  = jen_new.Eval();
                int      delta_jen = eval_new - eval;
                if (delta_jen > 0 || (rand.NextDouble() < Acceptor(delta_jen, t) && eval_new > 0))
                {
                    jen  = jen_new;
                    eval = eval_new;
                    if (eval >= best_jen.Eval())
                    {
                        best_jen = jen;
                    }
                }
            }
            t = Schedule(t);
        }
        return(best_jen);
    }