private void Generate_Btn_Click(object sender, EventArgs e) //生成最终全局信任值
{
int m, n, CalTime = 0; ;
double TrustTmp;
do
{
for (m = 0; m < PeersCount; m++)
{
OldGlobalTrust[m] = GlobalTrust[m];
((Peers)PeersList[m]).PeersTrustAdjust();
}
for (m = 0; m < PeersCount; m++) //迭代计算、矩阵相乘
{
TrustTmp = 0;
for (n = 0; n < PeersCount; n++)
{
double add1 = ((Peers)PeersList[n]).GetTrustValue(m);
double add2 = Convert.ToDouble(OldGlobalTrust[n]);
TrustTmp += add1 * add2;
}
GlobalTrust[m] = Math.Round(TrustTmp,3);
}
CalTime++;
} while (!IfInterationOver()&&CalTime<50);
if (!OverFlag)
{
MessageBox.Show("Interation is over");
}
else
{
MessageBox.Show("Running overtime!");
}
}
private bool IfInterationOver() //判断迭代是否结束,收敛条件是两次计算信任值差值的绝对值小于0.001
{
for (int i = 0; i < PeersCount; i++)
{
if (Math.Abs(Convert.ToDouble(GlobalTrust) - Convert.ToDouble(OldGlobalTrust)) > 0.001)
OverFlag = false;
}
return OverFlag;
}
}
}