# An example of markov decision algorithm in operations research is implemented in C language

• 2020-05-30 20:46:38
• OfStack

In this paper, the markov decision algorithm in operations research is implemented by C. I will share it with you for your reference as follows:

1. An overview of the

Markov decision making (Markov decision), short for markov decision making process (Markov Decision Processes), is an important theory for the study of stochastic sequential decision making. Markov decision is the optimization decision of a class of stochastic dynamic systems that can be continuously observed. It combines (deterministic) dynamic programming with markov process, and is the dynamic control method of stochastic discrete event dynamic systems.

Details about markov decision may refer to baidu encyclopedia: https: / / baike baidu. com item/B0 E6 AC A9 E9 % % % % % % 8 F AD E7 B3 % % % % E5%86% 96

2. Implementation code

``````
#include<stdio.h>
#include<cstdlib>
#define N 100
float p[N][N],s[N][N],a[N],b[N];
int o;
void set_TPM()  // Input transition probability matrix (Transition Probability Matrix)
{  int i,j;
scanf("%d",&o);
for(i=0;i<o;i++)
for(j=0;j<o;j++)
{
scanf("%f",&p[i][j]);
rewind(stdin);
}
}
void set_Initial_Prob() // Input initial probability state ( Initial Probability )
{
int i;
for(i=0;i<o;i++)
{
scanf("%f",&a[i]);
rewind(stdin);
}
}
void run_Markov(int count) //Markov The main algorithm
{
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() // Output cycle 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() // The main function
{
int a,count,i,j;
for(count=0;;)
{
printf("Create New Project:/n");
set_TPM();
set_Initial_Prob();
for(;;)
{
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");
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");
}

``````