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 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;

}

}

}

相关推荐
2501_933329553 小时前
媒介宣发技术实践:Infoseek舆情系统的AI中台架构与应用解析
开发语言·人工智能·架构·数据库开发
DuHz4 小时前
论文精读:大语言模型 (Large Language Models, LLM) —— 一项调查
论文阅读·人工智能·深度学习·算法·机器学习·计算机视觉·语言模型
[J] 一坚4 小时前
嵌入式高手C
c语言·开发语言·stm32·单片机·mcu·51单片机·iot
odoo中国4 小时前
Odoo 19技术教程 : 如何在 Odoo 19 中创建 Many2one 组件
开发语言·odoo·odoo19·odoo技术·many2one
加农炮手Jinx4 小时前
LeetCode 72. Edit Distance 题解
算法·leetcode·力扣
借雨醉东风4 小时前
程序分享--常见算法/编程面试题:旋转矩阵
c++·线性代数·算法·面试·职场和发展·矩阵
逻辑驱动的ken4 小时前
Java高频面试考点场景题14
java·开发语言·深度学习·面试·职场和发展·求职招聘·春招
_深海凉_4 小时前
LeetCode热题100-打家劫舍
算法·leetcode·职场和发展
jghhh015 小时前
使用 MATLAB 实现支持向量回归 (SVR) 预测未来数据
算法·matlab
云泽8085 小时前
笔试算法 - 双指针篇(二):四大经典求和题型 + 有效三角形计数问题
c++·算法