【发布时间】:2020-07-11 09:48:28
【问题描述】:
我创建了一个计算马尔可夫链的程序,但我遇到了一些问题,任何人都可以帮助我,为什么在应用程序打印出向用户询问行和列的值直到第 3 行它停止并且没有询问用户值 P。它直接打印“Markov Chain Invalid Row not Equal to 1”并结束。任何人都可以帮我解决这个问题,拜托。提前谢谢你。
public class assgmatrix {
static void printArray(double arrayfreq[]) {
for(double i:arrayfreq) {
System.out.println(i);
}
}
static void printArrayM(double arrayfreq[][]) {
System.out.println();
System.out.println(Arrays.deepToString(arrayfreq).replace("]","\n").replace(",","\t|").replaceAll("[\\[\\]]"," "));
System.out.println();
}
static boolean checkMark(double m[][]) {
for(int i=0; i<m.length; i++) {
double sum=0;
for(int j=0; j<m[i].length; j++)
sum+=m[i][j];
if(sum !=1)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Matrix 3 x 3 ");
System.out.println();
System.out.println(" Enter The Value for Row 1 Colum 1 :");
double oneM=input.nextDouble();
System.out.println(" Enter The Value for Row 1 Colum 2 :");
double twoM=input.nextDouble();
System.out.println(" Enter The Value for Row 1 Colum 3 :");
double threeM=input.nextDouble();
System.out.println();
System.out.println(" Enter The Value for Row 2 Colum 1 :");
double fourM=input.nextDouble();
System.out.println(" Enter The Value for Row 2 Colum 2 :");
double fiveM=input.nextDouble();
System.out.println(" Enter The Value for Row 2 Colum 3 :");
double sixM=input.nextDouble();
System.out.println();
System.out.println(" Enter The Value for Row 3 Colum 1 :");
double sevenM=input.nextDouble();
System.out.println(" Enter The Value for Row 3 Colum 2 :");
double eightM=input.nextDouble();
System.out.println(" Enter The Value for Row 3 Colum 3 :");
double nineM=input.nextDouble();
System.out.println();
double m[][]= { { oneM, twoM, threeM, fourM, fiveM, sixM, sevenM, eightM, nineM} };
double firstinput=0.0;
double secondinput=0.0;
double thirdinput=0.0;
double checkSum=0.0;
if(checkMark(m)) {
System.out.println(" Valid Markox Matrix as Sum for each Row is Equal to 1 ");
System.out.println(" Enter First P Value in % :");
firstinput=input.nextDouble();
firstinput=firstinput/100;
System.out.println(" Enter Second P Value in % :");
secondinput=input.nextDouble();
secondinput=secondinput/100;
System.out.println(" Enter Third P Value in % :");
thirdinput=input.nextDouble();
thirdinput=thirdinput/100;
checkSum=firstinput+secondinput+thirdinput;
if(checkSum==1) {
System.out.println("Transition Of Matrix Is : ");
printArrayM(m);
System.out.println("initial Probibility Vector that had been Entered is :");
System.out.println(" "+firstinput+","+secondinput+","+thirdinput);
System.out.println();
System.out.println(" ");
double firstRow=(firstinput*m[0][0])+(secondinput*m[1][0])+(thirdinput*m[2][0]);
double secondRow=(firstinput*m[0][1])+(secondinput*m[1][1])+(thirdinput*m[2][1]);
double thirdRow=(firstinput*m[0][2])+(secondinput*m[1][2])+(thirdinput*m[2][2]);
double sum = firstRow+secondRow+thirdRow;
String s1=String.format("%.3f", firstRow);
String s2=String.format("%.3f", secondRow);
String s3=String.format("%.3f", thirdRow);
System.out.println(" The % that been Entered for First Row :"+firstinput+"First Column :"+s1);
System.out.println(" The % that been Entered for Second Row :"+secondinput+"First Column :"+s2);
System.out.println(" The % that been Entered for Third Row :"+thirdinput+"First Column :"+s3);
System.out.println();
System.out.println("U(3) is :"+s1+" "+s2+" "+s3);
System.out.println("Sum of U(3) is :" +sum);
}
else
{
System.out.println("P Value is Not Equal to 1 ");
}
}
else
System.out.println("Markov Chain Invalid Row not Equal to 1");
}
}
【问题讨论】:
-
我使用以下值测试了应用程序:[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.2] 并且没有发生错误。你用什么值进行测试?问题可能出在比较
if (sum! = 1) {return false; }时,其中double的值与1 进行了比较,并假设double 不会有舍入误差。为了准确计算,最好使用BigDecimal而不是double。 -
输入的每一行必须是 =1 我输入了 0.2 0.2 0.6, 0.4 0.2 0.2 和 0.6 0.1 0.3 它直接跳转到马尔可夫链无效行不等于 1
-
0.2 0.2 0.6、0.4 0.4 0.2 和 0.6 0.1 0.3*
标签: java markov-chains