📄 cure算法的源程序.txt
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
namespace DataMiningRepPoint
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class DataMiningRepPointFrom : System.Windows.Forms.Form
{
private ArrayList sourceData;
private ArrayList resultData;
System.Drawing.Bitmap MyBitMap;
private int MyBitMapWidth;
private int MyBitMapHeight;
private System.Xml.XmlDocument xDoc;
private System.Windows.Forms.Button buttonLeadInData;
private System.Windows.Forms.TreeView treeViewDivisive;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button buttonGo;
private System.Windows.Forms.TextBox textBoxClassNum;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DataMiningRepPointFrom()
{
InitializeComponent();
sourceData = new ArrayList();
resultData = new ArrayList();
sourceData.Add(new PointTwo(1,2));
sourceData.Add(new PointTwo(1,2));
sourceData.Add(new PointTwo(3,2));
sourceData.Add(new PointTwo(3,8));
sourceData.Add(new PointTwo(4,2));
sourceData.Add(new PointTwo(2,4));
sourceData.Add(new PointTwo(5,2));
sourceData.Add(new PointTwo(6,4));
xDoc = new XmlDocument();
computeBitMapSize();
ShowPointInPicBox(sourceData,70);
pictureBox1.Image = MyBitMap;
}
/// <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.label2 = new System.Windows.Forms.Label();
this.textBoxClassNum = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonGo
//
this.buttonGo.Location = new System.Drawing.Point(464, 200);
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, 4);
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, 64);
this.treeViewDivisive.Name = "treeViewDivisive";
this.treeViewDivisive.SelectedImageIndex = -1;
this.treeViewDivisive.Size = new System.Drawing.Size(200, 296);
this.treeViewDivisive.TabIndex = 4;
//
// label2
//
this.label2.Location = new System.Drawing.Point(464, 48);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "期望的聚类数:";
//
// textBoxClassNum
//
this.textBoxClassNum.Location = new System.Drawing.Point(464, 88);
this.textBoxClassNum.Name = "textBoxClassNum";
this.textBoxClassNum.TabIndex = 6;
this.textBoxClassNum.Text = "";
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(216, 64);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(224, 296);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 7;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(74, 18);
this.label1.TabIndex = 8;
this.label1.Text = "树形显示";
//
// label3
//
this.label3.Location = new System.Drawing.Point(220, 40);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(78, 18);
this.label3.TabIndex = 8;
this.label3.Text = "图形显示";
//
// DataMiningRepPointFrom
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 382);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.treeViewDivisive);
this.Controls.Add(this.buttonGo);
this.Controls.Add(this.buttonLeadInData);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBoxClassNum);
this.Controls.Add(this.label3);
this.Name = "DataMiningRepPointFrom";
this.Text = "凝聚的层次聚类";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DataMiningRepPointFrom());
}
private void computeBitMapSize()
{
MyBitMapWidth = 0;
MyBitMapHeight = 0;
foreach(PointTwo pT in sourceData)
{
if(pT.x*10 > MyBitMapWidth)
{
MyBitMapWidth = (int)(pT.x*10);
}
if(pT.y*10 > MyBitMapHeight)
{
MyBitMapHeight = (int)(pT.y*10);
}
}
MyBitMap = new Bitmap(MyBitMapWidth+11,MyBitMapHeight+11);
}
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 PointTwo(double.Parse(xmlNode.Attributes.Item(0).Value),double.Parse(xmlNode.Attributes.Item(1).Value)));
}
}
}
computeBitMapSize();
ShowPointInPicBox(sourceData,70);
pictureBox1.Image = MyBitMap;
}
catch(Exception ex)
{
MessageBox.Show("数据错误!","Error");
}
}
private void ShowPointInPicBox(ArrayList displayData,int color_Index)
{
foreach(PointTwo pT in displayData)
{
if(color_Index%11==0)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Black);
}
else if(color_Index%11==1)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Red);
}
else if(color_Index%11==2)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Indigo);
}
else if(color_Index%11==3)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Blue);
}
else if(color_Index%11==4)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Brown);
}
else if(color_Index%11==5)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Gray);
}
else if(color_Index%11==6)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.DarkCyan);
}
else if(color_Index%11==7)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Gold);
}
else if(color_Index%11==8)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.Chocolate);
}
else if(color_Index%11==9)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.LightPink);
}
else if(color_Index%11==10)
{
MyBitMap.SetPixel((int)(pT.x*10),(int)(pT.y*10),System.Drawing.Color.DeepSkyBlue);
}
}
}
private bool InitializeXmlDocument(string fileName)
{
xDoc = new XmlDocument();
try
{
xDoc.Load(fileName);
return true;
}
catch(Exception ex)
{
MessageBox.Show("文件路径名称不正确");
return false;
}
}
// private void InitializeDataArray(ref ArrayList data_In)
// {
// for(int index=0;index<data_In.Count;index++)
// {
// ((PointTwo)data_In[index]).type = index;
// ((PointTwo)data_In[index]).locked = false;
// }
// }
private string ChangeArrayToString(ClusterDS C_In)
{
string result_Str;
result_Str="";
foreach(PointTwo node in C_In.pointArray)
{
result_Str=result_Str+"("+node.x+","+node.y+") ";
}
return result_Str;
}
private void buttonGo_Click(object sender, System.EventArgs e)
{
int classNum = 3;
if(textBoxClassNum.Text.Trim() != "")
{
try
{
classNum = int.Parse(textBoxClassNum.Text);
}
catch(Exception ex)
{
MessageBox.Show("Input Error");
return;
}
}
cluster(classNum);
displayReInTree();
int i=0;
foreach(ClusterDS CDS in resultData)
{
i=i+1;
ShowPointInPicBox(CDS.pointArray,i);
}
pictureBox1.Image = MyBitMap;
}
private void cluster(int Hope_K)
{
resultData.Clear();
foreach(PointTwo pT in sourceData)
{
ArrayList tempA = new ArrayList();
tempA.Add(pT);
resultData.Add(new ClusterDS(tempA));
}
while(resultData.Count > Hope_K)
{
foreach(ClusterDS CDC_One in resultData)
{
double min_Length=10000;
for(int i=0;i<resultData.Count;i++)
{
if(((ClusterDS)resultData[i]).Equals(CDC_One))
{
continue;
}
if(compute_Dist(CDC_One,(ClusterDS)resultData[i]) < min_Length)
{
min_Length = compute_Dist(CDC_One,(ClusterDS)resultData[i]);
CDC_One.nearestIndex = i;
CDC_One.nearestLength = min_Length;
}
}
}
int mindist_Index = 0;
for(int i=0;i<resultData.Count;i++)
{
if( ((ClusterDS)resultData[i]).nearestLength < ((ClusterDS)resultData[mindist_Index]).nearestLength )
{
mindist_Index = i;
}
}
int temp_Index = ((ClusterDS)resultData[mindist_Index]).nearestIndex;
Merge(((ClusterDS)resultData[mindist_Index]),((ClusterDS)resultData[temp_Index]));
resultData.RemoveAt(temp_Index);
}
}
private void Merge(ClusterDS C_One,ClusterDS C_Two)
{
foreach(PointTwo pT in C_Two.pointArray)
{
C_One.Add(pT);
}
C_One.computeMean();
C_One.computePointRegion();
C_One.GetNewRepPoint();
}
private double compute_Dist(ClusterDS C_One, ClusterDS C_Two)
{
double C_Length;
C_Length=10000;
foreach(PointTwo pT_One in C_One.RepPointArray)
{
foreach(PointTwo pT_Two in C_Two.RepPointArray)
{
if(C_One.computeLength(pT_One,pT_Two) < C_Length)
{
C_Length=C_One.computeLength(pT_One,pT_Two);
}
}
}
return C_Length;
}
private void displayReInTree()
{
this.treeViewDivisive.Nodes.Clear();
foreach(ClusterDS CDS in this.resultData)
{
this.treeViewDivisive.Nodes.Add(ChangeArrayToString(CDS));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -