//将表格中的数据读取到判断矩阵中 private JudgeMatrix ReadDataToMatrix() { int n = _judgeMatrixPair.NormalGen.X; JudgeMatrix temp = new JudgeMatrix(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double d = double.Parse(dataGrid.Rows[i].Cells[j].Value.ToString()); temp[i, j] = d; } } return temp; }
public JudgeMatrixDisplayForm(JudgeMatrix judgeMatrix) : this() { _judgeMatrix = judgeMatrix; Init(); }
/// <summary> /// 初始化决策模型 /// </summary> private void InitModel() { //创建第一层 Factor factor = new Factor("Z"); Level toplevel = new Level(null, new List<Factor>() { factor }, null, null); //设置初始化的模型 _ahpModel = new AhpModel(toplevel); //构造第二层 //第二层因素 IList<Factor> factors2 = new List<Factor> { new Factor("A1"), new Factor("A2"), new Factor("A3"), new Factor("A4"), }; //第二层关系矩阵 Matrix level2Relation = new Matrix(4, 1); level2Relation.InsertDataFromList(new List<double> { 1, 1, 1, 1 }); //第二层判断矩阵,初始化为空 JudgeMatrix level2JudugeMatrix1 = new JudgeMatrix(4); //加入到判断矩阵序列 Dictionary<Factor, JudgeMatrix> level2Judges = new Dictionary<Factor, JudgeMatrix>(); level2Judges.Add(toplevel.Factors[0], level2JudugeMatrix1); Level level2 = new Level(toplevel, factors2, level2Relation, level2Judges); //加入到模型中 _ahpModel.PushLevel(level2); //构造第三层次 IList<Factor> factors3 = new List<Factor> { new Factor("B1"){Direction = FactorDirection.Negative}, new Factor("B2"), new Factor("B3"), new Factor("B4"), new Factor("B5"), new Factor("B6"), new Factor("B7"), new Factor("B8"){Direction = FactorDirection.Negative}, new Factor("B9"), new Factor("B10"), new Factor("B11"), new Factor("B12"), new Factor("B13"), new Factor("B14"), }; //第三层关系矩阵 Matrix level3Relation = new Matrix(14, 4); level3Relation.InsertDataFromList(new List<double> { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 }); //第三层判断矩阵 JudgeMatrix level3JudugeMatrix1 = new JudgeMatrix(3); JudgeMatrix level3JudugeMatrix2 = new JudgeMatrix(4); JudgeMatrix level3JudugeMatrix3 = new JudgeMatrix(3); JudgeMatrix level3JudugeMatrix4 = new JudgeMatrix(4); //加入到判断矩阵序列 Dictionary<Factor, JudgeMatrix> level3Judges = new Dictionary<Factor, JudgeMatrix>(); level3Judges.Add(level2.Factors[0], level3JudugeMatrix1); level3Judges.Add(level2.Factors[1], level3JudugeMatrix2); level3Judges.Add(level2.Factors[2], level3JudugeMatrix3); level3Judges.Add(level2.Factors[3], level3JudugeMatrix4); Level level3 = new Level(level2, factors3, level3Relation, level3Judges); //加入到模型中 _ahpModel.PushLevel(level3); }
public static void JudgeMatrixTest() { JudgeMatrix jm1 = new JudgeMatrix(5, "测试矩阵"); jm1.InsertDataFromList( new List<double> { 1, 0.5, 4, 3, 3, 2, 1, 7, 5, 5, 1/4.0, 1/7.0, 1, 1/2.0, 1/3.0, 1/3.0, 1/5.0, 2, 1, 1, 1/3.0, 1/5.0, 3, 1, 1 }); jm1.DisplayMatrix(DataHelper.ConsoloOutput); double eiginValue; var vect1 = jm1.SingleFactorWightVect(out eiginValue); vect1.DisplayMatrix(DataHelper.ConsoloOutput); Console.WriteLine("测试矩阵的\n特征值={3},\nCI={0},\nRI={1},\nCR={2}", jm1.CI, jm1.RI, jm1.CR, eiginValue); }
public static void AhpModelTest() { //构造第一层 IList<Factor> factors1 = new List<Factor>() { new Factor("Z") }; Level level1 = new Level(null, factors1, null, null); AhpModel ahpModel = new AhpModel(level1); //构造第二层 //第二层因素 IList<Factor> factors2 = new List<Factor> { new Factor("A1"), new Factor("A2"), new Factor("A3"), new Factor("A4"), new Factor("A") }; //第二层关系矩阵 Matrix level2Relation = new Matrix(5, 1); level2Relation.InsertDataFromList(new List<double> { 1, 1, 1, 1, 1 }); //第二层判断矩阵 JudgeMatrix level2JudugeMatrix1 = new JudgeMatrix(5); level2JudugeMatrix1.InsertDataFromList(new List<double> { 1, 1/2.0, 4, 3, 3, 2, 1, 7, 5, 5, 1/4, 1/7, 1, 1/2.0, 1/3.0, 1/3.0, 1/5.0, 2, 1, 1, 1/3.0, 1/5.0, 3, 1, 1 }); //加入到判断矩阵序列 Dictionary<Factor, JudgeMatrix> level2Judges = new Dictionary<Factor, JudgeMatrix>(); level2Judges.Add(level1.Factors[0], level2JudugeMatrix1); Level level2 = new Level(level1, factors2, level2Relation, level2Judges); //加入到模型中 ahpModel.PushLevel(level2); //构造第三层次 IList<Factor> factors3 = new List<Factor> { new Factor("B1"), new Factor("B2"), new Factor("B3"), }; //第三层关系矩阵 Matrix level3Relation = new Matrix(3, 5); level3Relation.InsertDataFromList(new List<double> { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); //第三层判断矩阵 JudgeMatrix level3JudugeMatrix1 = new JudgeMatrix(3); level3JudugeMatrix1.InsertDataFromList(new List<double> { 1, 2, 5, 1/2.0, 1, 2, 1/5.0, 1/2.0, 1 }); JudgeMatrix level3JudugeMatrix2 = new JudgeMatrix(3); level3JudugeMatrix2.InsertDataFromList(new List<double> { 1, 1/3.0, 1/8.0, 3, 1, 1/3.0, 8, 3, 1 }); JudgeMatrix level3JudugeMatrix3 = new JudgeMatrix(3); level3JudugeMatrix3.InsertDataFromList(new List<double> { 1, 1, 3, 1, 1, 3, 1/3.0, 1/3.0, 1 }); JudgeMatrix level3JudugeMatrix4 = new JudgeMatrix(3); level3JudugeMatrix4.InsertDataFromList(new List<double> { 1, 3, 4, 1/3.0, 1, 1, 1/4.0, 1, 1 }); JudgeMatrix level3JudugeMatrix5 = new JudgeMatrix(3); level3JudugeMatrix5.InsertDataFromList(new List<double> { 1, 1, 1/4.0, 1, 1, 1/4.0, 4, 4, 1 }); //加入到判断矩阵序列 Dictionary<Factor, JudgeMatrix> level3Judges = new Dictionary<Factor, JudgeMatrix>(); level3Judges.Add(level2.Factors[0], level3JudugeMatrix1); level3Judges.Add(level2.Factors[1], level3JudugeMatrix2); level3Judges.Add(level2.Factors[2], level3JudugeMatrix3); level3Judges.Add(level2.Factors[3], level3JudugeMatrix4); level3Judges.Add(level2.Factors[4], level3JudugeMatrix5); Level level3 = new Level(level2, factors3, level3Relation, level3Judges); //加入到模型中 ahpModel.PushLevel(level3); //测试数据 ahpModel.GetLevelInfo(3).GetTotalWeightVect().DisplayMatrix(DataHelper.ConsoloOutput); }
//使用改进的方法构造判断矩阵 public static IList<JudgeMatrix> GetJudgeMatrices(Matrix relationMatrix, IList<Factor> parentFactors, IList<Factor> currentFacors) { IList<JudgeMatrix> judgeMatrices = new List<JudgeMatrix>(); //按照上一层中的各个因素逐个处理 for (int j = 0; j < relationMatrix.Y; j++) { //获得上层某因素影响的下层因素 IList<Factor> affectedFactors = new List<Factor>(); for (int i = 0; i < relationMatrix.X; i++) { if (Math.Abs(relationMatrix[i, j] - 0) > 0.00001) affectedFactors.Add(currentFacors[i]); } //构造一个空的判断矩阵, int affectedCount = affectedFactors.Count; JudgeMatrix judgeMatrix = new JudgeMatrix(affectedCount); #region 然后使用改进的构造方法构造判断矩阵 //打印提示信息 Console.WriteLine("请输入因素{0}影响的{1}个因素的重要性排序,\n同时给出最重要与最不重要的因素比率", parentFactors[j].Name, affectedCount); Console.Write("被影响的因素包括:"); for (int i = 0; i < affectedCount; i++) { Console.Write("{0} {1},", i, affectedFactors[i].Name); } Console.WriteLine(); //各个专家给出的排序的表 IList<IList<int>> rateTable = new List<IList<int>>(); IList<int> pList = new List<int>(); //表示第几个专家给出的排序 int rateCount = 0; //循环读取专家的建议排序,然后放入rateTable中 while (true) { rateCount++; //获取排序 var readRate = DataHelper.ReadValus<int>(string.Format("请输入第{0}个专家的排序,从0开始!", rateCount), affectedCount); rateTable.Add(readRate); //获取p int p = DataHelper.ReadValus<int>(string.Format("请输入第{0}个专家的的最重要与最不重要的因素比率", rateCount), 1)[0]; pList.Add(p); //是否继续添加,如果不添加了,就跳出 if (!DataHelper.ReadBool("是否要继续添加重要性排序?")) break; } //计算根据rateTable和pList构造判断矩阵 double pAvg = pList.Average(); IList<double> aList = new List<double>(); //对被影响的因素逐个处理,生成各个被影响因素的平均赋权值 for (int i = 0; i < affectedCount; i++) { int sumIndex = 0, avgIndex = 0, pow = 0; for (int k = 0; k < rateCount; k++) { sumIndex += (rateTable[k].IndexOf(i) + 1); } avgIndex = sumIndex / rateCount; pow = affectedCount - avgIndex + 1; aList.Add(pow); } //对判断矩阵逐个处理插入值 for (int m = 0; m < affectedCount; ++m) { for (int n = 0; n < affectedCount; ++n) { if (aList[m] >= aList[n]) { var numerator = aList[m] - aList[n]; var denominator = aList.Max() - aList.Min(); judgeMatrix[m, n] = numerator / denominator * (pAvg - 1) + 1; } else { var numerator = aList[n] - aList[m]; var denominator = aList.Max() - aList.Min(); judgeMatrix[m, n] = 1 / (numerator / denominator * (pAvg - 1) + 1); } } } #endregion judgeMatrices.Add(judgeMatrix); } return judgeMatrices; }
//根据关系矩阵构造判断矩阵 public static IList<JudgeMatrix> GetJudgeMatrices(Matrix relationMatrix) { IList<JudgeMatrix> judgeMatrices = new List<JudgeMatrix>(); for (int j = 0; j < relationMatrix.Y; j++) { var affectcount = 0; for (int i = 0; i < relationMatrix.X; i++) { if (Math.Abs(relationMatrix[i, j] - 0) > 0.00001) affectcount++; } JudgeMatrix judgeMatrix = new JudgeMatrix(affectcount); //可以选择判断矩阵的构造方式 judgeMatrix.InsertMatrix(DataHelper.ConsoleArrayInput); judgeMatrices.Add(judgeMatrix); } return judgeMatrices; }
/// <summary> /// 根据关系矩阵生成空的判断矩阵,每一次关系矩阵修改,都必须调用这个函数 /// </summary> /// <param name="relationMatrix">关系矩阵</param> private void UpdateJudgeMatrices() { //Todo:考虑实现一种不清空关系矩阵的方法 for (int i = 0; i < _relationMatrix.Y; i++) { //查找上一层第i个因素影响的本层因素数量 int affectcount = 0; for (int j = 0; j < _relationMatrix.X; j++) { affectcount++; } //如果影响的因素数量不为不为0,则增加一个判断矩阵 if (affectcount != 0) { JudgeMatrix jm = new JudgeMatrix(affectcount); JudgeMatrices.Add(Parent.Factors[i], jm); } } }
/// <summary> /// 设置本层次相对于上一层次中某个因素的的判断矩阵 /// </summary> /// <param name="parentFactor"></param> /// <param name="judgeMatrix"></param> private void SetJudgeMatrix(Factor parentFactor, JudgeMatrix judgeMatrix) { //检查parentFactor是否为上一层次总的因素 if (!Parent.Factors.Contains(parentFactor)) { throw new CustomeExcetpion("上一层不包含指定的因素"); } //检查传入的判断矩阵是否符合要求 judgeMatrix.CheckJudgeMatrix(); //设置 JudgeMatrices.Add(parentFactor, judgeMatrix); }
//通过表格中的数据计算判断矩阵 private JudgeMatrix CalJudgeMatrix() { JudgeMatrix judgeMatrix = new JudgeMatrix(FactorCount); //各个专家给出的排序的表 IList<IList<int>> rateTable = new List<IList<int>>(); IList<int> pList = new List<int>(); //表示专家给出的排序个数 //要排除最后一行空行 int rateCount = _dataTable.Rows.Count; //逐行处理,将数据填充到以上两个List中 for (int i = 0; i < rateCount; i++) { //获取当前行的所有数据项 var items = _dataTable.Rows[i].ItemArray; //用来保存排序数据项 IList<int> tempList = new List<int>(); for (int j = 0; j < FactorCount; j++) { tempList.Add((int)items[j]); } rateTable.Add(tempList); //填充p pList.Add((int)items[FactorCount]); } //计算根据rateTable和pList构造判断矩阵 double pAvg = pList.Average(); IList<double> aList = new List<double>(); //对被影响的因素逐个处理,生成各个被影响因素的平均赋权值 for (int i = 0; i < FactorCount; i++) { int sumIndex = 0, avgIndex = 0, pow = 0; for (int k = 0; k < rateCount; k++) { sumIndex += (rateTable[k].IndexOf(i) + 1); } avgIndex = sumIndex / rateCount; pow = FactorCount - avgIndex + 1; aList.Add(pow); } //对判断矩阵逐个处理插入值 for (int m = 0; m < FactorCount; ++m) { for (int n = 0; n < FactorCount; ++n) { if (aList[m] >= aList[n]) { var numerator = aList[m] - aList[n]; var denominator = aList.Max() - aList.Min(); judgeMatrix[m, n] = numerator / denominator * (pAvg - 1) + 1; } else { var numerator = aList[n] - aList[m]; var denominator = aList.Max() - aList.Min(); judgeMatrix[m, n] = 1 / (numerator / denominator * (pAvg - 1) + 1); } } } return judgeMatrix; }