本文实例讲述了C语言实现运筹学中的马氏决策算法。分享给大家供大家参考,具体如下:
一、概述
马氏决策(Markov decision)是马尔可夫决策过程(Markov Decision Processes,简记为MDP)的简称,是研究随机序贯决策问题的一门重要理论。马氏决策是一类可连续进行观察的随机动态系统的最优化决策,它将(确定性)动态规划与马尔可夫过程相结合,是随机离散事件动态系统惟一的动态控制方法。
关于马氏决策的具体说明可参考百度百科:https://baike.baidu.com/item/%E9%A9%AC%E6%B0%8F%E5%86%B3%E7%AD%96
二、实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include<stdio.h>
#include<cstdlib>
#define N 100
float p[N][N],s[N][N],a[N],b[N];
int o;
void set_TPM() //输入转移概率矩阵(Transition Probability Matrix)
{ int i,j;
printf ( "Please input Number of State:" );
scanf ( "%d" ,&o);
for (i=0;i<o;i++)
for (j=0;j<o;j++)
{
printf ( "Please input state%d,state%d:" ,i,j);
scanf ( "%f" ,&p[i][j]);
rewind (stdin);
}
}
void set_Initial_Prob() //输入初始概率状态(Initial Probability)
{
int i;
for (i=0;i<o;i++)
{
printf ( "Please input state%d Initial Prob:" ,i);
scanf ( "%f" ,&a[i]);
rewind (stdin);
}
}
void run_Markov( int count) //Markov主算法
{
int i,j,k;
float c[N];
for (i=0;i<o;i++) c[i]=a[i];
for (k=0;k<count;k++)
{
for (i=0;i<o;i++)
for (j=0;j<o;j++)
{
s[i][j]=p[i][j]*c[i];
}
for (i=0;i<o;i++)
{
b[i]=0;
for (j=0;j<o;j++)
{
b[i]=b[i]+s[j][i];
}
c[i]=b[i];
}
}
for (i=0;i<o;i++) c[i]=0;
}
void print_Result() //输出周期结果
{
int i,j;
for (i=0;i<o;i++)
for (j=0;j<o;j++)
{
printf ( " %f" ,s[i][j]);
if (j==2) printf ( "/n" );
}
for (i=0;i<o;i++)
{
printf ( " %f" ,b[i]);
}
printf ( "/n" );
}
main() //主函数
{
int a,count,i,j;
for (count=0;;)
{
printf ( "Create New Project:/n" );
set_TPM();
set_Initial_Prob();
for (;;)
{
printf ( "***********************************/n" ); //展示选择菜单
printf ( "1.Times periods from initial./n" );
printf ( "2.Next Period./n" );
printf ( "3.Create New Porject./n" );
printf ( "4.Exit./n/n" );
printf ( "**********************************/n" );
printf ( "Please input your choose:/n" );
scanf ( "%d" ,&a);
rewind (stdin);
if (a==3) break ;
switch (a)
{
case 1:
printf ( "Input number of time periods from initial:/n" );
scanf ( "%d" ,&count);
rewind (stdin);
run_Markov(count);
print_Result();
break ;
case 2:
run_Markov(count++);
print_Result();
break ;
case 4: exit (1);
default : printf ( "Error choose!!/n" ); break ;
}
}
}
system ( "pause" );
}
|
希望本文所述对大家C语言程序设计有所帮助。
原文链接:http://blog.csdn.net/reghi/article/details/5543871