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;

}

}

}

相关推荐
CRMEB系统商城1 小时前
开源自建还是SaaS订阅?算一笔3年经济账
java·大数据·开发语言·开源
keyipatience2 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
我的xiaodoujiao2 小时前
快速学习Python基础知识详细图文教程9--函数进阶
开发语言·python·学习·测试工具
战族狼魂2 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一3 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH3 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
爱喝水的鱼丶3 小时前
SAP-ABAP:ALV通用封装实践——搭建可复用的ALV开发工具类,减少80%重复代码
开发语言·性能优化·sap·abap·erp·alv
中达瑞和-高光谱·多光谱3 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机
脱胎换骨-军哥3 小时前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust
songroom3 小时前
Kimi K3:Rust封装XTP接口详细教程实践
开发语言·后端·rust