C#, Numerical Calculation - Calculation Method and Source Program of Dynpro

 

Given a vector nstate whose integer value is the number of states in each stage (1 for the first and last stage), and a given function cost(j,k,i) returns state j at stage i and state k To move between cost stages i+1, this routine returns a vector of the same length as nstate containing the state number of the lowest cost path. The number of states starts at 0, and the first and last components of the returned vector will therefore always be 0.

Given the vector nstate whose integer values are the number of states in each stage(1 for the first and last stages), and given a function cost(j, k, i)that returns the cost of moving between state j of stage i and state k ofstage i+1, this routine returns a vector of the same length as nstatecontaining the state numbers of the lowest cost path. States number from 0,and the first and last components of the returned vector will thus always be 0.

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Given the vector nstate whose integer values are the number of states in each
    /// stage(1 for the first and last stages), and given a function cost(j, k, i)
    /// that returns the cost of moving between state j of stage i and state k of
    /// stage i+1, this routine returns a vector of the same length as nstate
    /// containing the state numbers of the lowest cost path. States number from 0,
    /// and the first and last components of the returned vector will thus always be 0.
    /// </summary>
    public abstract class Dynpro
    {
        public Dynpro()
        {
        }

        public abstract double cost(int jj, int kk, int ii);

        public int[] dynpro(int[] nstate)
        {
            const double BIG = 1.0e99;
            const double EPS = float.Epsilon; //numeric_limits<double>.epsilon();
            int nstage = nstate.Length - 1;
            int[] answer = new int[nstage + 1];
            if (nstate[0] != 1 || nstate[nstage] != 1)
            {
                throw new Exception("One state allowed in first and last stages.");
            }

            double[,] best = new double[nstage + 1, nstate[0]];
            best[0, 0] = 0.0;
            for (int i = 1; i <= nstage; i++)
            {
                for (int k = 0; k < nstate[i]; k++)
                {
                    double b = BIG;
                    for (int j = 0; j < nstate[i - 1]; j++)
                    {
                        double a = best[i - 1, j] + cost(j, k, i - 1);
                        if ((a) < b)
                        {
                            b = a;
                        }
                    }
                    best[i, k] = b;
                }
            }
            answer[nstage] = answer[0] = 0;
            for (int i = nstage - 1; i > 0; i--)
            {
                int k = answer[i + 1];
                double b = best[i + 1, k];
                int j = 0;
                for (; j < nstate[i]; j++)
                {
                    double temp = best[i, j] + cost(j, k, i);
                    if (Math.Abs(b - temp) <= EPS * Math.Abs(temp))
                    {
                        break;
                    }
                }
                answer[i] = j;
            }
            return answer;
        }
    }
}
 

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/132218897