📄 分裂的聚类算法的源程序.txt
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
namespace DataMiningWork
{
/// <summary>
/// DataMiningForm 的摘要说明。
/// </summary>
public class DataMiningForm : System.Windows.Forms.Form
{
private ArrayList sourceData;
private System.Xml.XmlDocument xDoc;
private System.Windows.Forms.Button buttonLeadInData;
private System.Windows.Forms.TreeView treeViewDivisive;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button buttonGo;
private System.Windows.Forms.TextBox textBoxLength;
private System.Windows.Forms.TextBox textBoxStep;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DataMiningForm()
{
InitializeComponent();
sourceData = new ArrayList();
sourceData.Add(new node_Two(1,2));
sourceData.Add(new node_Two(3,4));
sourceData.Add(new node_Two(3,2));
sourceData.Add(new node_Two(3,8));
sourceData.Add(new node_Two(4,2));
sourceData.Add(new node_Two(2,4));
sourceData.Add(new node_Two(5,2));
sourceData.Add(new node_Two(6,4));
xDoc = new XmlDocument();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.buttonGo = new System.Windows.Forms.Button();
this.buttonLeadInData = new System.Windows.Forms.Button();
this.treeViewDivisive = new System.Windows.Forms.TreeView();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBoxLength = new System.Windows.Forms.TextBox();
this.textBoxStep = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonGo
//
this.buttonGo.Location = new System.Drawing.Point(440, 232);
this.buttonGo.Name = "buttonGo";
this.buttonGo.Size = new System.Drawing.Size(104, 23);
this.buttonGo.TabIndex = 0;
this.buttonGo.Text = "分裂的层次聚类";
this.buttonGo.Click += new System.EventHandler(this.buttonGo_Click);
//
// buttonLeadInData
//
this.buttonLeadInData.Location = new System.Drawing.Point(8, 8);
this.buttonLeadInData.Name = "buttonLeadInData";
this.buttonLeadInData.Size = new System.Drawing.Size(104, 23);
this.buttonLeadInData.TabIndex = 0;
this.buttonLeadInData.Text = "从文件导入数据";
this.buttonLeadInData.Click += new System.EventHandler(this.buttonLeadInData_Click);
//
// treeViewDivisive
//
this.treeViewDivisive.ImageIndex = -1;
this.treeViewDivisive.Location = new System.Drawing.Point(8, 48);
this.treeViewDivisive.Name = "treeViewDivisive";
this.treeViewDivisive.SelectedImageIndex = -1;
this.treeViewDivisive.Size = new System.Drawing.Size(392, 312);
this.treeViewDivisive.TabIndex = 4;
//
// label1
//
this.label1.Location = new System.Drawing.Point(408, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 23);
this.label1.TabIndex = 5;
this.label1.Text = "初始簇间最大距离:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(408, 112);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "步进值:";
//
// textBoxLength
//
this.textBoxLength.Location = new System.Drawing.Point(464, 80);
this.textBoxLength.Name = "textBoxLength";
this.textBoxLength.TabIndex = 6;
this.textBoxLength.Text = "";
//
// textBoxStep
//
this.textBoxStep.Location = new System.Drawing.Point(464, 144);
this.textBoxStep.Name = "textBoxStep";
this.textBoxStep.TabIndex = 6;
this.textBoxStep.Text = "";
//
// DataMiningForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 382);
this.Controls.Add(this.textBoxLength);
this.Controls.Add(this.label1);
this.Controls.Add(this.treeViewDivisive);
this.Controls.Add(this.buttonGo);
this.Controls.Add(this.buttonLeadInData);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBoxStep);
this.Name = "DataMiningForm";
this.Text = "凝聚的层次聚类";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DataMiningForm());
}
private void buttonLeadInData_Click(object sender, System.EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = @"C:\";
openFileDialog.ShowReadOnly = true;
openFileDialog.Filter = "XML files (*.xml)|*.xml|所有文件 (*.*)|*.*" ;
openFileDialog.FilterIndex = 2 ;
openFileDialog.RestoreDirectory = true ;
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
if(InitializeXmlDocument(openFileDialog.FileName))
{
sourceData.Clear();
foreach(XmlNode xmlNode in xDoc.DocumentElement.ChildNodes)
{
sourceData.Add(new node_Two(double.Parse(xmlNode.Attributes.Item(0).Value),double.Parse(xmlNode.Attributes.Item(1).Value)));
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("数据错误!","Error");
}
}
private bool InitializeXmlDocument(string fileName)
{
xDoc = new XmlDocument();
try
{
xDoc.Load(fileName);
return true;
}
catch(Exception ex)
{
MessageBox.Show("文件路径名称不正确");
return false;
}
}
private void SearchFile()
{
}
private ArrayList OneTimeAGNES_Fun(ArrayList data_In,double d_Max)
{
ArrayList result_Array = new ArrayList();
ArrayList inResult_Array = new ArrayList();
InitializeDataArray(ref data_In);
((node_Two)data_In[0]).locked = true;
foreach(node_Two node_1 in data_In)
{
foreach(node_Two node_2 in data_In)
{
if(node_2.locked == true)
{
continue;
}
bool Add = true;
foreach(node_Two node_3 in data_In)
{
if(node_3.type == node_1.type)
{
double length;
length = Math.Sqrt(Math.Pow((node_3.x-node_2.x),2)+Math.Pow((node_3.y-node_2.y),2));
if(length > d_Max)
{
Add = false;
}
}
}
if(Add)
{
node_2.type = node_1.type;
node_1.locked = true;
node_2.locked = true;
}
}
}
int type=0;
while(type < data_In.Count)
{
inResult_Array = new ArrayList();
foreach(node_Two node_1 in data_In)
{
if(node_1.type == type)
inResult_Array.Add(node_1);
}
if(inResult_Array.Count > 0)
{
result_Array.Add(inResult_Array);
}
type++;
}
return result_Array;
}
private void InitializeDataArray(ref ArrayList data_In)
{
for(int index=0;index<data_In.Count;index++)
{
((node_Two)data_In[index]).type = index;
((node_Two)data_In[index]).locked = false;
}
}
private string ChangeArrayToString(ArrayList data_In)
{
string result_Str;
result_Str="";
foreach(node_Two node in data_In)
{
result_Str=result_Str+"("+node.x+","+node.y+") ";
}
return result_Str;
}
private void AGNES_Fun(ArrayList data_In,TreeNode treeNode_In,double length,double step)
{
foreach(ArrayList AL in data_In)
{
treeNode_In.Nodes.Add("簇间距离为:"+length.ToString()+" "+ChangeArrayToString(AL));
if(AL.Count > 1)
{
double length_Temp=length;
if(length_Temp<0)
length_Temp=-1;
AGNES_Fun(OneTimeAGNES_Fun(AL,length_Temp),treeNode_In.Nodes[treeNode_In.Nodes.Count-1],length-step,step);
}
}
}
private void buttonGo_Click(object sender, System.EventArgs e)
{
ArrayList sourceData1 = new ArrayList();
sourceData1.Add(sourceData);
double length;
double step;
if(textBoxLength.Text.Trim() == "")
{
textBoxLength.Text = "3";
}
if(textBoxStep.Text.Trim() == "")
{
textBoxStep.Text = "0.5";
}
try
{
length = double.Parse(textBoxLength.Text);
step = double.Parse(textBoxStep.Text);
}
catch(Exception ex)
{
MessageBox.Show("最大距离以及步进值都应该为数字!","Erroe");
return;
}
try
{
treeViewDivisive.Nodes.Clear();
treeViewDivisive.Nodes.Add("Start");
AGNES_Fun(sourceData1,treeViewDivisive.Nodes[0],length,step);
}
catch(Exception ex)
{
MessageBox.Show("发生错误!","Error");
return;
}
}
// private void buttonOutput_Click(object sender, System.EventArgs e)
// {
// saveXmlFile();
// }
// private void saveXmlFile()
// {
// if( MessageBox.Show("确定保存吗?","确认",MessageBoxButtons.OKCancel) == DialogResult.OK )
// {
// SaveFileDialog saveFileDialog = new SaveFileDialog();
// saveFileDialog.InitialDirectory = @"C:\";
// saveFileDialog.Filter = "XML files (*.xml)|*.xml|所有文件 (*.*)|*.*" ;
// saveFileDialog.FilterIndex = 2 ;
// saveFileDialog.RestoreDirectory = true ;
//
// if(saveFileDialog.ShowDialog() == DialogResult.OK)
// {
// this.treeViewDivisive.x(saveFileDialog.FileName);
// }
// }
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -