menu

开发进行时...

crazy coder

Avatar

原创--C#实例编程3

层次分析法Analitic Hierachy Process (AHP)

一、需求分析
问题举例
1. 在海尔、新飞、容声和雪花四个牌号的电冰箱中选购一种。要考虑品牌的信誉、冰箱的功能、价格和耗电量。
2. 在泰山、杭州和承德三处选择一个旅游点。要考虑景点的景色、居住的环境、饮食的特色、交通便利和旅游的费用。
3. 在基础研究、应用研究和数学教育中选择一个领域申报科研课题。要考虑成果的贡献(实用价值、科学意义),可行性(难度、周期和经费)和人才培养。
模型和方法
1. 层次结构模型的构造
步骤一:确定层次结构,将决策的目标、考虑的因素(决策准则)和决策对象按它们之间的相互关系分为最高层、中间层和最低层,绘出层次结构图。
最高层:决策的目的、要解决的问题。
最低层:决策时的备选方案。
中间层:考虑的因素、决策的准则。
对于相邻的两层,称高层为目标层,低层为因素层。
步骤二: 通过相互比较,确定下一层各因素对上一层目标的影响的权重,将定性的判断定量化,即构造因素判断矩阵。
步骤三:由矩阵的特征值确定判别的一致性;由相应的特征向量表示各因素的影响权重,计算权向量。
步骤四: 通过综合计算给出最底层(各方案)对最高层(总目标)影响的权重,权重最大的方案即为实现目标的最由选择。
2. 因素判断矩阵
比较n个因素y=(y1,y2,…,yn)对目标 z 的影响.
采用两两成对比较,用aij表示因素 yi与因素yj对目标z的影响程度之比。
通常用数字 1~ 9及其倒数作为程度比较的标度, 即九级标度法
xi/xj 相当 较重要 重要 很重要 绝对重要
aij 1 3 5 7 9
2, 4, 6, 8 居于上述两个相邻判断之间。
当aij > 1时,对目标 Z来说 xi 比 xj重要, 其数值大小表示重要的程度。
同时必有 aji = 1/ aij 1,对目标 Z来说 xj比 xi 不重要,其数值大小表示不重要的程度。
称矩阵 A = ( aij )为因素判断矩阵。
因为 aij >0 且 aji =1/ aij 故称A = (aij )为正互反矩阵。
例. 选择旅游景点 Z:目标,选择景点 y:因素,决策准则
y1 费用,y2 景色,y3 居住,y4 饮食,y5 交通

3. 一致性与权向量
如果 aij ajk =aik i, j, k=1,2,…,n, 则称正互反矩阵A具有一致性. 这表明对各个因素所作的两两比较是可传递的。
一致性互正反矩阵A=( aij )具有性质:
A的每一行(列)均为任意指定行(列)的正数倍数,因此 rank(A)=1.
A有特征值=n, 其余特征值均为零.
记A的对应特征值=n的特征向量为w=(w1 w2 ,…, wn) 则 aij =wi wj-1
如果在目标z中n个因素y=(y1,y2,…,yn)所占比重分别为w=(w1 w2 ,…, wn),
则 iwi =1, 且因素判断矩阵为 A=(wi wj-1) 。
因此,称一致性正互反矩阵A相应于特征值n的归一化特征向量为因素y=(y1,y2,…,yn)对目标z的权向量
4. 一致性检验与因素排序
定理1: n阶正互反矩阵A是一致性的当且仅当其最大特征值为 n.
定理2: 正互反矩阵具有模最大的正实数特征值1, 其重数为1, 且相应特征向量为正向量.
为刻画n阶正互反矩阵A=( aij )与一致性接近的程度, 定义一致性指标(Consensus index) :
CI=(1-n)/(n-1)
CI = 0, A 有完全的一致性。CI 接近于 0, A 有满意的一致性 。
Saaty又引入平均随机一致性指标RT
n 1 2 3 4 5 6 7 8 9
RI 0 0 0.58 0.90 1.12 1.24 1.32 1.41 1.45
当CR = CI / RI < 0.1 时, 认为A 有满意的一致性。
此时取A 的相应于&#61548;1 的归一化特征向量w=(w1 w2 ,…, wn)为因素y=(y1,y2,…,yn)对目标z的权向量。由w=( w2 ,…, wn)分量wi的大小可以对因素的重要性排序。

择校排名

二、使用的知识要点

1.动态生成控件

三、主程序界面

四、主要程序段

动态生本控件,并加上相应所需要的方法:


		//初始化文本框
		private void Initextbox(int len,string[] str)
		{
			this.groupBox1.Controls.Clear();//清空不用的控件
			TextBox mytextbox;//定义文本框
			int x=this.groupBox1.Location.X+10;
			int y=this.groupBox1.Location.Y+40;
			for(int i = 0; i<len;i++)//生成标签
			{
				Label mylabel = new Label();
				mylabel.Text = str[i].ToString();
				mylabel.Location = new Point(x+i*60,y-40);
				mylabel.AutoSize = true;
				this.groupBox1.Controls.Add(mylabel);
			}
			for(int i=0;i<len;i++)//生成文本框
			{
				for(int j=0;j<len;j++)
				{
					mytextbox = new TextBox();
					mytextbox.Size = new System.Drawing.Size(60,20);
					mytextbox.BackColor = Color.LightGoldenrodYellow;
					mytextbox.Name = "mytextbox"+i+j;
					mytextbox.Leave += new System.EventHandler(this.textBox_mouseover);
					mytextbox.Location = new Point(x,y);
					if(i==j) 
					{
						mytextbox.BackColor = Color.Wheat;
						mytextbox.Text = "1";
						mytextbox.Enabled = false;
					}
					if(i<j)
					{
						mytextbox.BackColor = SystemColors.ActiveBorder;
						mytextbox.Enabled = false;
					}
					this.groupBox1.Controls.Add(mytextbox);
					x+=60;
				}
				x=this.groupBox1.Location.X+10;
				y+=20;
			}
		}


相应的方法:



		//控制文本框
		private void textBox_mouseover(object sender,System.EventArgs e)
		{
			TextBox temptextbox = (TextBox) sender;
			string tempstr = temptextbox.Name.ToString();
			if(temptextbox.Text.Trim() == "") 
			{
			//	MessageBox.Show("没有输入数字");
			//	temptextbox.Focus();
			}
			else
			{
				string i = tempstr.Substring(tempstr.Length-2,1);
				string j =  tempstr.Substring(tempstr.Length-1,1);
				foreach(Control tempC in this.groupBox1.Controls)
				{
					if(tempC is TextBox)
					{
						if(tempC.Name.ToString() == "mytextbox"+j+i)
						{
							if(temptextbox.Text.ToString().Trim().IndexOf("/")>0)
								tempC.Text = temptextbox.Text.ToString().Substring(temptextbox.Text.ToString().Trim().Length-1,1);
							else if(temptextbox.Text.ToString().Trim()=="1")
								tempC.Text = "1";
							else
								tempC.Text = "1/"+temptextbox.Text;
						}
					}
				}
			}
		}//textBox_mouseover


获取第个文本框的值:


		private void getdata(double[,] matrix)
		{
			foreach(Control tempC in this.groupBox1.Controls)
			{
				try
				{
					if(tempC is TextBox)
					{
						string tempstr = tempC.Text.ToString().Trim();
						if(tempstr == "")
						{
							MessageBox.Show("有文本框没有填数据!");
							return;
						}
						int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
						int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
						if(tempstr.IndexOf("/")>0)
						{
							matrix[i,j] = Convert.ToDouble(tempstr.Substring(0,1))/Convert.ToDouble(tempstr.Substring(tempstr.Length-1,1));
						}
						else
							matrix[i,j]=Convert.ToDouble(tempstr);
					}
				}
				catch(Exception err)
				{
					MessageBox.Show(err.ToString());
				}
			}
		}

五、所有程序代码


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace 经济管理模型
{
	/// <summary>
	/// ccfx2 的摘要说明。
	/// </summary>
	public class ccfx2 : System.Windows.Forms.Form
	{
		private static double[] RI = {0,0,0,0.58,0.90,1.12,1.24,1.32,1.41,1.45};
		private Int32 LenA;//准则数
		private System.Int32 LenB;//方案数
		private double[,] zhuzematrix;//准则数方阵
		private double[][,] fanganmatrix;//方案阵
		private string[] zhuze;//准则字符串
		private string[] fangan;//方案字符串
		private int Stepcount=0;//录入矩阵的步骤
		private double[] W;//单序w
		private double[,] TW;//总序w
		private double[] Torder;//总的方案排名
		private double lamda;//单序最大lamda
		private double[] Tlamda;//总序lamda

		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button2;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ccfx2()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <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.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Controls.Add(this.textBox2);
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(0, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(616, 360);
			this.groupBox1.TabIndex = 15;
			this.groupBox1.TabStop = false;
			// 
			// textBox2
			// 
			this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox2.BackColor = System.Drawing.Color.Ivory;
			this.textBox2.Location = new System.Drawing.Point(128, 80);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(472, 20);
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox1.BackColor = System.Drawing.Color.Ivory;
			this.textBox1.Location = new System.Drawing.Point(128, 48);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(472, 20);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "";
			this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 80);
			this.label2.Name = "label2";
			this.label2.TabIndex = 1;
			this.label2.Text = "方案:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 48);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "准则:";
			// 
			// label5
			// 
			this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.label5.AutoSize = true;
			this.label5.ForeColor = System.Drawing.Color.Red;
			this.label5.Location = new System.Drawing.Point(16, 384);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(0, 16);
			this.label5.TabIndex = 16;
			// 
			// button1
			// 
			this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button1.Location = new System.Drawing.Point(456, 384);
			this.button1.Name = "button1";
			this.button1.TabIndex = 17;
			this.button1.Text = "下一步";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button2.Location = new System.Drawing.Point(536, 384);
			this.button2.Name = "button2";
			this.button2.TabIndex = 21;
			this.button2.Text = "从例子获取";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// ccfx2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 421);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.groupBox1);
			this.Name = "ccfx2";
			this.Text = "层次分析法";
			this.Load += new System.EventHandler(this.ccfx2_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void ccfx2_Load(object sender, System.EventArgs e)
		{
		
		}


//初始化变量
		private void InitValue()//
		{
			//录入准则字符串
			this.zhuze = this.textBox1.Text.ToString().Trim().Split(',');
			this.fangan = this.textBox2.Text.ToString().Trim().Split(',');
			this.LenA = zhuze.Length;
			this.LenB = fangan.Length;
			if(this.LenA !=5 || this.LenB != 3) this.button2.Visible = false;
			//==实例化各变量
			this.zhuzematrix = new double[this.LenA,this.LenA];
			this.fanganmatrix = new double[this.LenA][,];
			for(int i = 0;i<LenA;i++)
				this.fanganmatrix[i] = new double[LenB,LenB];
			this.W = new double[LenA];
			this.TW = new double[LenA,LenB];
			this.Torder = new double[LenB];
			this.Tlamda = new double[LenA];
			//==
			this.groupBox1.Controls.Clear();
			this.label5.Text="请输入准则层相对目标的判别矩阵";
		}

		//初始化文本框
		private void Initextbox(int len,string[] str)
		{
			this.groupBox1.Controls.Clear();//清空不用的控件
			TextBox mytextbox;//定义文本框
			int x=this.groupBox1.Location.X+10;
			int y=this.groupBox1.Location.Y+40;
			for(int i = 0; i<len;i++)//生成标签
			{
				Label mylabel = new Label();
				mylabel.Text = str[i].ToString();
				mylabel.Location = new Point(x+i*60,y-40);
				mylabel.AutoSize = true;
				this.groupBox1.Controls.Add(mylabel);
			}
			for(int i=0;i<len;i++)//生成文本框
			{
				for(int j=0;j<len;j++)
				{
					mytextbox = new TextBox();
					mytextbox.Size = new System.Drawing.Size(60,20);
					mytextbox.BackColor = Color.LightGoldenrodYellow;
					mytextbox.Name = "mytextbox"+i+j;
					mytextbox.Leave += new System.EventHandler(this.textBox_mouseover);
					mytextbox.Location = new Point(x,y);
					if(i==j) 
					{
						mytextbox.BackColor = Color.Wheat;
						mytextbox.Text = "1";
						mytextbox.Enabled = false;
					}
					if(i<j)
					{
						mytextbox.BackColor = SystemColors.ActiveBorder;
						mytextbox.Enabled = false;
					}
					this.groupBox1.Controls.Add(mytextbox);
					x+=60;
				}
				x=this.groupBox1.Location.X+10;
				y+=20;
			}
		}
		//控制文本框
		private void textBox_mouseover(object sender,System.EventArgs e)
		{
			TextBox temptextbox = (TextBox) sender;
			string tempstr = temptextbox.Name.ToString();
			if(temptextbox.Text.Trim() == "") 
			{
			//	MessageBox.Show("没有输入数字");
			//	temptextbox.Focus();
			}
			else
			{
				string i = tempstr.Substring(tempstr.Length-2,1);
				string j =  tempstr.Substring(tempstr.Length-1,1);
				foreach(Control tempC in this.groupBox1.Controls)
				{
					if(tempC is TextBox)
					{
						if(tempC.Name.ToString() == "mytextbox"+j+i)
						{
							if(temptextbox.Text.ToString().Trim().IndexOf("/")>0)
								tempC.Text = temptextbox.Text.ToString().Substring(temptextbox.Text.ToString().Trim().Length-1,1);
							else if(temptextbox.Text.ToString().Trim()=="1")
								tempC.Text = "1";
							else
								tempC.Text = "1/"+temptextbox.Text;
						}
					}
				}
			}
		}//textBox_mouseover

		//
		private void mytextboxclear()
		{
			foreach(Control tempC in this.groupBox1.Controls)
			{
				if(tempC is TextBox)
				{
					string tempstr = tempC.Text.ToString().Trim();
					int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
					int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
					if(i==j) tempC.Text = "1";
					else tempC.Text = "";
				}
			}
		}
		

		private void vieworder()
		{
			int i,j;
			int[] temp = new int[this.LenA];
			for(i = 0;i < this.LenA;i++)
				temp[i] = i;
			for(i=0;i<this.LenA;i++)
				for(j=i+1;j<this.LenA;j++)
					if(this.W[temp[i]] < this.W[temp[j]])
					{int t=temp[i];temp[i] = temp[j];temp[j]=t;}
			string str="";
			for(i=0;i<this.LenA;i++)
				//str+=this.zhuze[temp[i]].ToString()+":"+this.W[temp[i]].ToString();
			{
				Label templabel = new Label();
				templabel.Text = this.zhuze[temp[i]].ToString()+":"+this.W[temp[i]].ToString();
				templabel.Location = new Point(this.groupBox1.Location.X+10,this.groupBox1.Location.Y+i*20+40);
				templabel.AutoSize = true;
				this.groupBox1.Controls.Add(templabel);//END 处理单序排序
			}
			//Label templabel = new Label();
			//templabel.Text = str;
			//templabel.Location = new Point(this.groupBox1.Location.X+10,this.groupBox1.Location.Y+40);
			//templabel.AutoSize = true;
			//templabel.Width = 20*LenA;
			//this.groupBox1.Controls.Add(templabel);//END 处理单序排序

			for(j=0;j<LenB;j++)
				for(i=0;i<LenA;i++)
					Torder[j] +=W[i]*TW[i,j]; 
			int[] Ttemp = new int[this.LenB];
			for(i = 0;i < this.LenB;i++)
				Ttemp[i] = i;
			for(i=0;i<this.LenB;i++)
				for(j=i+1;j<this.LenB;j++)
					if(this.Torder[Ttemp[i]] < this.Torder[Ttemp[j]])
					{int t=Ttemp[i];Ttemp[i] = Ttemp[j];Ttemp[j]=t;}
			str="";
			for(i=0;i<this.LenB;i++)
				str+=this.fangan[Ttemp[i]].ToString()+":"+this.Torder[Ttemp[i]].ToString()+"\t";
			Label templabel2 = new Label();
			templabel2.Text = str;
			templabel2.Location = new Point(this.groupBox1.Location.X+10,this.groupBox1.Location.Y+20*LenA+80);
			templabel2.AutoSize = true;
			this.groupBox1.Controls.Add(templabel2);//END 处理总序排序
		}
		private void getdata(double[,] matrix)
		{
			foreach(Control tempC in this.groupBox1.Controls)
			{
				try
				{
					if(tempC is TextBox)
					{
						string tempstr = tempC.Text.ToString().Trim();
						if(tempstr == "")
						{
							MessageBox.Show("有文本框没有填数据!");
							return;
						}
						int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
						int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
						if(tempstr.IndexOf("/")>0)
						{
							matrix[i,j] = Convert.ToDouble(tempstr.Substring(0,1))/Convert.ToDouble(tempstr.Substring(tempstr.Length-1,1));
						}
						else
							matrix[i,j]=Convert.ToDouble(tempstr);
					}
				}
				catch(Exception err)
				{
					MessageBox.Show(err.ToString());
				}
			}
		}
		//例子数据
		private void button2_Click(object sender, System.EventArgs e)
		{
			switch(this.Stepcount)
			{
				case 0:
					this.textBox1.Text = "费用,名气,导师,课题,个人兴趣";
					this.textBox2.Text = "清华,北大,北航";
					break;
				case 1:
					string[,] temparr = {{"1","2","7","5","5"},{"1/2","1","4","3","3"},{"1/7","1/4","1","1/2","1/3"},{"1/5","1/3","2","1","1"},{"1/5","1/3","3","1","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				case 2:
					string[,] temparr1 = {{"1","1/5","1/8"},{"5","1","1/3"},{"8","3","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr1[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				case 3:
					string[,] temparr2 = {{"1","2","5"},{"1/2","1","2"},{"1/5","1/2","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr2[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				case 4:
					string[,] temparr3 = {{"1","1","3"},{"1","1","3"},{"1/3","1/3","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr3[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				case 5:
					string[,] temparr4 = {{"1","3","4"},{"1/3","1","1"},{"1/4","1","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr4[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				case 6:
					string[,] temparr5 = {{"1","1","1/4"},{"1","1","1/4"},{"4","4","1"}};
					foreach(Control tempC in this.groupBox1.Controls)
					{
						try
						{
							if(tempC is TextBox)
							{
								string tempstr = tempC.Text.ToString().Trim();
								int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
								int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
								tempC.Text = temparr5[i,j];
							}
						}
						catch(Exception err)
						{
							MessageBox.Show(err.ToString());
						}
					}
					break;
				default:
					break;
			}
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			if(this.button1.Text=="计算结果")
			{
				this.vieworder();
				return;
			}
			if(this.Stepcount == 0)
			{
				this.InitValue();
				this.Initextbox(this.LenA,this.zhuze);
				this.Stepcount++;
				return;
			}
			if(this.Stepcount == 1)
			{
				foreach(Control tempC in this.groupBox1.Controls)//录入数据
				{
					try
					{
						if(tempC is TextBox)
						{
							string tempstr = tempC.Text.ToString().Trim();
							if(tempstr == "")
							{
								MessageBox.Show("有文本框没有填数据!");
								return;
							}
							int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
							int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
							if(tempstr.IndexOf("/")>0)
							{
								this.zhuzematrix[i,j] = Convert.ToDouble(tempstr.Substring(0,1))/Convert.ToDouble(tempstr.Substring(tempstr.Length-1,1));
							}
							else
								this.zhuzematrix[i,j]=Convert.ToDouble(tempstr);
						}
					}
					catch(Exception err)
					{
						MessageBox.Show(err.ToString());
					}
				}//foreach
				//计算W
				for(int i=0;i<LenA;i++)
					this.W[i]=1;
				for(int j=0;j<LenA;j++)
				{
					for(int i=0;i<LenA;i++)
						this.W[j]*=this.zhuzematrix[j,i];
					//
					//W[j]=Math.Pow(W[j],1/LenA);
					W[j]=Math.Exp(Math.Log(W[j])/LenA);
				}
				double Tt = 0;
				for(int i=0;i<LenA;i++)
					Tt+=W[i];
				for(int i=0;i<LenA;i++)
					W[i]=W[i]/Tt;
				this.lamda = 0;
				for(int j=0;j<LenA;j++)
				{
					for(int i=0;i<LenA;i++)
						this.lamda += this.zhuzematrix[j,i]*W[i]/W[j];
				}
				this.lamda /=LenA;
				//MessageBox.Show(this.lamda.ToString());
				if(((this.lamda-LenA)/(LenA-1)) > RI[LenA]*0.1)
				{
					MessageBox.Show("不符合一致性!");
					return;
				}
				this.Initextbox(this.LenB,this.fangan);
				//this.mytextboxclear();
				this.label5.Text = "请输入方案层相对准则层 "+this.zhuze[Stepcount-1]+" 的判别矩阵";
				this.Stepcount++;
				if(this.Stepcount==this.LenA+2)
				{
					this.button1.Enabled = false;
					this.button2.Visible = false;
				}
				return;
			}//结束对第一步1的特殊处理
			
			foreach(Control tempC in this.groupBox1.Controls)//录入数据
			{
				try
				{
					if(tempC is TextBox)
					{
						string tempstr = tempC.Text.ToString().Trim();
						if(tempstr == "")
						{
							MessageBox.Show("有文本框没有填数据!");
							return;
						}
						int i = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-2,1));
						int j = Convert.ToInt32(tempC.Name.ToString().Substring(tempC.Name.ToString().Length-1,1));
						if(tempstr.IndexOf("/")>0)
						{
							this.fanganmatrix[this.Stepcount-2][i,j] = Convert.ToDouble(tempstr.Substring(0,1))/Convert.ToDouble(tempstr.Substring(tempstr.Length-1,1));
						}
						else
							this.fanganmatrix[this.Stepcount-2][i,j]=Convert.ToDouble(tempstr);
					}
				}
				catch(Exception err)
				{
					MessageBox.Show(err.ToString());
				}
			}//foreach
			//计算W[stepcount]
			int k = this.Stepcount -2;
			if(k+1 == this.LenA)
				this.label5.Text = "计算结果!";
			else
                this.label5.Text = "请输入方案层相对准则层 "+this.zhuze[k+1]+" 的判别矩阵";
			for(int i=0;i<LenB;i++)
				this.TW[k,i]=1;
			for(int j=0;j<LenB;j++)
			{
				for(int i=0;i<LenB;i++)
					this.TW[k,j]*=this.fanganmatrix[k][j,i];
				//TW[k,j]=Math.Pow(TW[k,j],1/LenB);
				TW[k,j] = Math.Exp(Math.Log(TW[k,j])/LenB);
			}
			double Ttt = 0;
			for(int i=0;i<LenB;i++)
				Ttt+=TW[k,i];
			for(int i=0;i<LenB;i++)
				TW[k,i]=TW[k,i]/Ttt;
			this.Tlamda[k] = 0;
			for(int j=0;j<LenB;j++)
			{
				for(int i=0;i<LenB;i++)
					this.Tlamda[k] += this.fanganmatrix[k][j,i]*TW[k,i]/TW[k,j];
			}
			this.Tlamda[k] /=LenB;
			//MessageBox.Show(this.Tlamda[k].ToString());
			if(((this.Tlamda[k]-LenB)/(LenB-1)) > RI[LenB])
			{
				MessageBox.Show("不符合一致性!");
				return;
			}
			//this.Initextbox(this.LenB,this.fangan);
			this.mytextboxclear();
			this.Stepcount++;
			if(this.Stepcount==this.LenA+2)
			{
				this.button1.Text  = "计算结果";
				this.groupBox1.Controls.Clear();
				//this.button2.Visible = false;
			}
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

流水兄,代码可以用 [code] 标签括起来:)

改过来了~~还是这样不错~
多谢谢天魔兄~

你好,最近在学AHP方面的知识,看到你这篇博文写得不错哎,但是上面的一些配图好像都失效了,对了,怎么联系你 啊

评论已关闭