Exemple #1
0
 /// <summary>
 /// Creates an array of neighboroperations.
 /// Each operation has a chance to get in the array
 /// given by the provided probability distribution.
 /// </summary>
 /// <param name="probDist">The chance for each operation to get in the array</param>
 /// <param name="nOps">Length of the neighboroperations array</param>
 /// <returns>An array of neighboroperations</returns>
 public NeighborOperation[] GetOperations(double[] probDist, int nOps)
 {
     NeighborOperation[] res = new NeighborOperation[nOps];
     for (int j = 0; j < nOps; j++)
     {
         double acc = 0;
         double p   = StaticRandom.NextDouble();
         for (int i = 0; i < probDist.Length; i++)
         {
             acc += probDist[i];
             if (p <= acc)
             {
                 res[j] = ops[i](this);
                 break;
             }
         }
     }
     return(res);
 }
Exemple #2
0
        public override bool GetNext(double[] probDist, int nOps)
        {
            // Create an array of operators that we are going to perform to generate neighbors
            List <NOp> ops = new List <NOp>(schedule.GetOperations(probDist, nOps));

            // Take a random operator
            // if it may be performed
            //  - Apply it if the operator decreases the score
            //  - Apply the operator with chance decpending on the temperator if the score is increased
            while (ops.Count != 0)
            {
                int i  = StaticRandom.Next(0, ops.Count);
                NOp op = ops[i];
                ops.RemoveAt(i);
                if (op.Evaluate())
                {
                    double delta = op.TotalDelta;
                    if (delta < 0)
                    {
                        op.Apply();
                        return(true);
                    }
                    else
                    {
                        double p = Prob(delta, c);
                        double r = StaticRandom.NextDouble();

                        if (p > r)
                        {
                            op.Apply();
                            return(true);
                        }
                    }
                }
                c *= a; // Update temperature
            }
            return(false);
        }