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;

}

}

}

相关推荐
zzzll11112 小时前
Claude Code离线安装方案揭秘
开发语言·php
wabs6662 小时前
关于图论【卡码网117.软件构建的思考】
数据结构·算法·软件构建·图论·卡码网
mifengxing3 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
wling03013 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴3 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
Tongzhi20263 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
Wang's Blog4 小时前
AI Agent白手起家30: 动态示例选择器之根据长度选择 Few Shot 示例
算法
xrandzj4 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_5 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯
oyguyteggytrrwwwrt5 小时前
自制交叉线路识别算法
算法