Exemplo n.º 1
0
        public override bool GetNext(double[] probDist, int nOps)
        {
            // Create an array of operators that we are going to perform to generate neighbors
            NOp[] ops = schedule.GetOperations(probDist, nOps);

            NOp    best = null;
            double opt  = 0; // (the best needs to atleast lower the score)

            // = double.MaxValue (just find the best schedule)
            // Find the operation the lowers the score the most:
            for (int i = 0; i < nOps; i++)
            {
                // Check if the operator may be performed, without actually performing the operator
                if (ops[i].Evaluate())
                {
                    // Check if the deltascore of this neighbor is better than the current delta
                    double delta = ops[i].TotalDelta;
                    if (delta < opt)
                    {
                        best = ops[i];
                        opt  = delta;
                    }
                }
            }
            // No operation found that meets the requirements:
            if (best == null)
            {
                return(false);
            }

            // Apply the best operation, this does modify the schedule:
            best.Apply();
            return(true);
        }
Exemplo n.º 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);
        }