C#,数值计算——Dynpro的计算方法与源程序

给定向量nstate,其整数值是每个状态中的状态数阶段(第一和最后阶段为1),并给定函数成本(j,k,i)返回在阶段i的状态j和的状态k之间移动的成本阶段i+1,此例程返回与nstate长度相同的向量包含最低成本路径的状态号。状态数从0开始,并且返回向量的第一个和最后一个分量将因此总是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 intnstage + 1;

if (nstate0 != 1 || nstatenstage != 1)

{

throw new Exception("One state allowed in first and last stages.");

}

double, best = new doublenstage + 1, nstate\[0];

best0, 0 = 0.0;

for (int i = 1; i <= nstage; i++)

{

for (int k = 0; k < nstatei; k++)

{

double b = BIG;

for (int j = 0; j < nstatei - 1; j++)

{

double a = besti - 1, j + cost(j, k, i - 1);

if ((a) < b)

{

b = a;

}

}

besti, k = b;

}

}

answernstage = answer0 = 0;

for (int i = nstage - 1; i > 0; i--)

{

int k = answeri + 1;

double b = besti + 1, k;

int j = 0;

for (; j < nstatei; j++)

{

double temp = besti, j + cost(j, k, i);

if (Math.Abs(b - temp) <= EPS * Math.Abs(temp))

{

break;

}

}

answeri = j;

}

return answer;

}

}

}

相关推荐
z落落1 小时前
C# 泛型方法(原理、类型推断、多泛型参数)+泛型效率(普通类型 VS Object装箱 VS 泛型)
开发语言·c#
L_09071 小时前
【C++】异常
开发语言·c++
Frostnova丶1 小时前
【算法笔记】数学知识
笔记·算法
吴可可1232 小时前
AutoCAD 2016与2014二次开发关键差异
算法
世辰辰辰2 小时前
批量修改图片/文本名子
开发语言·python·批量修改文件名
雨白3 小时前
哈希:以时间换空间的算法实战
算法
rockey6273 小时前
基于AScript的SQL脚本语言发布啦!
sql·c#·.net·script·expression·动态脚本
啦啦啦啦啦zzzz4 小时前
数据结构:红黑树理论
数据结构·c++·红黑树
z落落4 小时前
C# 四种特殊类:抽象类、密封类、静态类、部分类
开发语言·c#
VidDown4 小时前
Webhook 调试器:让第三方回调“原形毕露”
java·开发语言·javascript·编辑器·postman