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;

}

}

}

相关推荐
“AI国潮设计-小江”3 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
程序喵大人3 小时前
【C++入门】编译链接模型 - 01 一个 C++ 程序是怎样变成可执行文件的
开发语言·c++·编译链接模型
奋发向前wcx3 小时前
CSP-J复赛模拟赛4 王晨旭补题 2026.10.5
算法
sunshine22 girl5 小时前
Java学习七 Java 项目实战1-项目创建和主页面搭建
java·开发语言·学习
菜鸟~noob2335 小时前
【电子战】第三季预告—波武器与电子系统强力毁伤
开发语言·数据库·matlab·电子战
念何架构之路6 小时前
zap采样器与性能优化内幕
算法·性能优化·哈希算法
惊讶的猫6 小时前
工具选对之后还不够:商城 Agent 的参数与权限治理
开发语言·python
cmpxr_6 小时前
【MicroPython】如何刷新ESP8266的固件
开发语言·micropython
搭贝7 小时前
上厕所的时间搭好一套系统:AI自动生成实测
开发语言·人工智能·低代码·ai·php·搭贝
智感子7 小时前
智能称重·数据上云:从仪表读数到可追溯的计量凭证
开发语言·php