📄 doc.cs
字号:
this.Scale(sf);
//foreach (BNode1 node in myNodeArray)
//{
// node.MakeItEllipse(node.Size.Width, node.Size.Height);
//}
Invalidate();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
//foreach(BNode1 node in myNodeArray)
//MessageBox.Show(((int)System.Math.Round(node.Width).ToString());
//MessageBox.Show(node.Width.ToString());
//this.Scale(0.66F, 0.66F);
//float ratio=0.66;
SizeF sf = new SizeF(0.5f, 0.5f);
this.Scale(sf);
//foreach (BNode1 node in myNodeArray)
//{
// node.MakeItEllipse(node.Size.Width, node.Size.Height);
//}
Invalidate();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
if (NumControlsAdded > 0)
{
// remove the last control
Controls.Remove(LastControl);
NumControlsAdded--;
if (NumControlsAdded > 0)
{
LastControl = Controls[Controls.Count - 1];
if (NumControlsAdded > 1)
{
PreviousControl = Controls[Controls.Count - 2];
}
}
Invalidate();
}
else toolStripButton5.Enabled = false;
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
#region 训练按钮代码
if (this.arrayA==null||this.arrayA.Count==0)
{
MessageBox.Show("没有训练样本");
}
else
{
//this.arrYb = new LineArray();
//for (int i = 0; i < this.Lines.Count; i++)
//{
// this.arrYb.Add(this.Lines[i]);
//}
this.arrXlq = new ArrayList();
foreach(Line l in this.Lines)//清空生成样本值的原来wij,并且随机生成wij
{
l.wij = this.GetRand();
this.arrXlq.Add(l.wij);
}
ToolStripProgressBar tp = (ToolStripProgressBar)this.statusStrip1.Items[7];
tp.Maximum = this.arrayA.Count;
tp.Minimum = 0;
tp.Value = 0;
for (int i = 0; i < this.arrayA.Count; i++)//遍历所有的样本
{
ArrayList arrtem = (ArrayList)this.arrayA[i];//得到当前遍历样本
//这里的当前样本有输入节点的值,也有输出节点的
//得到该样本的所有输入值
ArrayList arrRtem = new ArrayList();
for (int tem = 0; tem < this.arrR; tem++)
{
arrRtem.Add(Convert.ToDouble((string)arrtem[tem]));
}
//以随机生成或学习过的权值为基础生成输出
GetC sc = new GetC(this.myNodeArray, this.Lines, arrRtem);
ArrayList arrCtem = sc.GetShuChu();
#region 得到所有的Derta值以DerTaNode类的格式存储在ArrayList arrDerta中
//得到所有节点的Derta值,这里采用了后验方法,由于原公式没有给出
//Derta_wij和Derta值得计算先后顺序,或者叠带规则,这里先计算每个
//节点的Derta值
ArrayList arrDerta=new ArrayList();//用于暂存各个Derta值,但是为了计算方便,这里值得顺序
//和节点顺序是相反的,第一个值是最后一个节点的derta值
for (int tem2 = 0; tem2 < this.myNodeArray.Count; tem2++)
{
DerTaNode dt = new DerTaNode();
dt.Node = this.myNodeArray[tem2];
dt.Derta = 0.0d;
arrDerta.Add(dt);
}
for (int tem1 = this.myNodeArray.Count - 1; tem1 >= this.arrR; tem1--)
{
if (this.myNodeArray[tem1].outdegree == 0)//输出节点的derta值计算
{
double temd = -Convert.ToDouble(arrtem[arrtem.Count-(this.myNodeArray.Count-tem1)]) - Convert.ToDouble(((BNode_InOut)arrCtem[tem1]).DOut);
((DerTaNode)arrDerta[tem1]).Derta += temd;
}
else if (this.myNodeArray[tem1].outdegree == 1)//子节点为导航节点
{
ArrayList alc = this.GetChild(this.myNodeArray[tem1].name);
int index = this.GetIndexFromName((string)alc[0]);
((DerTaNode)arrDerta[tem1]).Derta -= ((DerTaNode)arrDerta[index]).Derta;
}
else //普通节点
{
ArrayList alc1 = this.GetChild(this.myNodeArray[i].name);
double sumtem = 0.0d;//本节点所有子节点的反馈和
for (int tem4 = 0; tem4 < alc1.Count; tem4++)//遍历所有子节点
{
int intem = this.GetIndexFromName((string)alc1[tem4]);
double Nd = 1.0d;
if (this.myNodeArray[i].style == "Noisy_Or")//Noisy_Or类型节点
{
Nd *= ((DerTaNode)arrDerta[intem]).Derta;
Nd *= this.GetWij(this.myNodeArray[i].name, (string)alc1[tem4]);
//Nd *= -1;
//下面该写alc1[tem4]所有的父节点除了this.myNodeArray[i]之外的乘积了
ArrayList arrP = this.GetParents((string)alc1[tem4]);
for (int tem5 = 0; tem5 < arrP.Count; tem5++)
{
if ((string)arrP[tem5] != (string)alc1[tem4])
{
int temIndex = this.GetIndexFromName((string)arrP[tem5]);
double wkj = this.GetWij((string)arrP[tem5], (string)alc1[tem4]);
double Belk = (((BNode_InOut)arrCtem[temIndex]).DOut);
Nd *= (1 - wkj * Belk);
}
}
}
else //Noisy_And类型节点
{
Nd *= ((DerTaNode)arrDerta[intem]).Derta;
Nd *= this.GetWij(this.myNodeArray[i].name, (string)alc1[tem4]);
ArrayList arrP = this.GetParents((string)alc1[tem4]);
for (int tem5 = 0; tem5 < arrP.Count; tem5++)
{
if ((string)arrP[tem5] != (string)alc1[tem4])
{
int temIndex = this.GetIndexFromName((string)arrP[tem5]);
double wkj = this.GetWij((string)arrP[tem5], (string)alc1[tem4]);
double Belk = (((BNode_InOut)arrCtem[temIndex]).DOut);
Nd *= (1 - wkj * (1-Belk));
}
}
}
sumtem += Nd;
}
((DerTaNode)arrDerta[tem1]).Derta += sumtem;
}
}
#endregion
#region 遍历每一条边,更改权值
for (int tem6 = this.Lines.Count - 1; tem6 >= 0; tem6--)//遍历每一条边用于计算
{
double Derta_cij = 1.0d;
Derta_cij *= this.Eta;
//得到该边的起始节点的输出
int Sindex = this.GetIndexFromName(this.Lines[tem6].sNode);
Derta_cij *= ((BNode_InOut)arrCtem[Sindex]).DOut;//乘以起始节点的输出值
int Dindex = this.GetIndexFromName(this.Lines[tem6].dNode);////得到终止节点的Derta值
Derta_cij *= ((DerTaNode)arrDerta[Dindex]).Derta;//乘以终止节点的Derta值
if (this.myNodeArray[Dindex].style == "no_style")
{
continue;
}
if ((this.myNodeArray[Dindex].style == "Noisy_Or")) //Noisy_Or类型节点
{
//得到终止节点的父节点序列
//Derta_cij *= -1;//注意,公式有错误的部分,就是Derta_cij公式的符号部分
ArrayList arrPt = this.GetParents(this.Lines[tem6].dNode);
for (int tem7 = 0; tem7 < arrPt.Count; tem7++)
{
if ((string)arrPt[tem7] != this.Lines[tem6].sNode)
{
int Kindex = this.GetIndexFromName((string)arrPt[tem7]);
double wkj = this.GetWij((string)arrPt[tem7], this.Lines[tem6].dNode);
Derta_cij *= (1 - wkj * ((BNode_InOut)arrCtem[Kindex]).DOut);
}
}
}
else //Noisy_And类型节点
{
//Derta_cij *= -1;//注意,公式有错误的部分,就是Derta_cij公式的符号部分
//得到终止节点的父节点序列
ArrayList arrPt = this.GetParents(this.Lines[tem6].dNode);
for (int tem7 = 0; tem7 < arrPt.Count; tem7++)
{
if ((string)arrPt[tem7] != this.Lines[tem6].sNode)
{
int Kindex = this.GetIndexFromName((string)arrPt[tem7]);
double wkj = this.GetWij((string)arrPt[tem7], this.Lines[tem6].dNode);
Derta_cij *= (1 - wkj * (1-((BNode_InOut)arrCtem[Kindex]).DOut));
}
}
}
double wij_Lcur = Convert.ToDouble(this.Lines[tem6].wij);
wij_Lcur += Derta_cij;
this.Lines[tem6].wij = wij_Lcur.ToString();
}
#endregion
tp.Value = i + 1;
}
frm_af_xl frm = new frm_af_xl();
frm.arrYb = this.arrYb;
frm.arrXlq = this.arrXlq;
frm.arrXlh = this.Lines;
frm.ShowDialog();
}
#endregion
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
this.save_xml1();
}
private void toolStripButton9_Click(object sender, EventArgs e)
{
#region 打开文件代码
//this.minDegree();
OpenFileDialog opDlg = new OpenFileDialog();
opDlg.Filter = "Belief Network Format(*.xml)|*.xml|All Files(*.*)|*.*";
opDlg.FilterIndex = 1;
opDlg.InitialDirectory = "d:\\";
if (opDlg.ShowDialog() == DialogResult.OK)
{
//windowCount++ ;
//Doc docu = new Doc(opDlg.FileName);
//docu.MdiParent = this;
//NodeArray myNodeArray=new NodeArray(this);
XmlDocument doc = new XmlDocument();
doc.Load(opDlg.FileName);
XmlElement root = doc.DocumentElement;
ArrayList name = new ArrayList();
ArrayList position = new ArrayList();
//int length=0;
Point from = new Point(0, 0);
Point to = new Point(0, 0);
string s = "";
XmlNodeList xmlNodes = root.SelectNodes("/BNetNodes/*");
foreach (XmlNode node in xmlNodes)
{
name.Add(node.Name);
//MessageBox.Show(name.Count.ToString());
xmlNodes = node.SelectNodes("POSITION");
foreach (XmlNode node1 in xmlNodes)
{ //MessageBox.Show(node1.InnerText.ToString());
s = node1.InnerText.ToString();
s = s.Substring(1, s.Length - 2);
string[] obs = s.Split(',');
int[] pos = new Int32[obs.Length];
for (int i = 0; i < obs.Length; i++)
{
pos[i] = Convert.ToInt32(obs[i]);
position.Add(pos[i]);
}
}
//xmlNodes=node.SelectNodes("POSITION");
}
for (int i = 0; i < name.Count; i++)
{
myNodeArray.AddNewNode(Convert.ToInt32(position[2 * i].ToString()), Convert.ToInt32(position[2 * i + 1]));
myNodeArray[i].Text = name[i].ToString();
}
//MessageBox.Show(position.Count.ToString());
int size = name.Count;
bool[][] adjMatrix = new bool[size][];
for (int i = 0; i < size; i++)
adjMatrix[i] = new bool[size];
//initial
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
adjMatrix[i][j] = false;
//gBNet m_net = new gBNet();
//m_net.Build(opDlg.FileName);
//ArrayList m_bnNodes = m_net.Nodes;
//int sumedge=0;
//int to=0;
//for(int i=0;i<nodes.Count;i++)
XmlNodeList xmlNodes1 = root.SelectNodes("/BNetNodes/*");
//int form;
//int to;
foreach (XmlNode node in xmlNodes1)
{
//to=name.IndexOf(node.Name);
xmlNodes1 = node.SelectNodes("Parents/*");
foreach (XmlNode xml_node in xmlNodes1)
adjMatrix[name.IndexOf(xml_node.Name)][name.IndexOf(node.Name)] = true;
}
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
if (adjMatrix[i][j] == true)
{
CurrentLine = new Line();
CurrentLine.StartPoint = myNodeArray[i].from;
CurrentLine.EndPoint = myNodeArray[j].to;
CurrentLine.DrawLine(this);
Invalidate();
Lines.Add(CurrentLine);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -