/// <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); }
public DecisionMatrix(Level level, int elementCount, string name) : base(elementCount, level.FactorCount, name) { //如果传入的是顶层,则抛出异常 if (level.LevelCount == 1) throw new CustomeExcetpion("不可以是顶层元素"); this.level = level; }
public Level(LevelDataModel dataModel) { if (!dataModel.Check()) { throw new CustomeExcetpion("数据不符合要求!"); } _parent = dataModel.Parent; _factors = dataModel.Factors; _relationMatrix = dataModel.RelationMatrix; _judgeMatrices = dataModel.JudgeMatrices; }
//将判断矩阵设置到对应的层次 public static void SetJudgeMatrices(this IList<JudgeMatrixPair> judgeMatrixPairs, Level level, bool isApproved) { Factor currentFactor; foreach (var judgeMatrixPair in judgeMatrixPairs) { currentFactor = judgeMatrixPair.AffectedFactor; if (level.JudgeMatrices.ContainsKey(currentFactor)) { level.JudgeMatrices[currentFactor] = isApproved ? judgeMatrixPair.ApprovedGen : judgeMatrixPair.NormalGen; } } }
public Level(Level parent, IList<Factor> factors, Matrix relationMatrix, Dictionary<Factor, JudgeMatrix> judgeMatrices) { //如果parent为null,说明是顶层元素,只需要设置factors即可 if (parent == null) { _factors = factors; } //否则就需要社会自factors,relationMatrix,judgeMatrices else { //todo:设置之前必须检查是否符合要求啊 _parent = parent; _factors = factors; _relationMatrix = relationMatrix; _judgeMatrices = judgeMatrices; } }
public static void AhpModelSample() { //创建第一层 Level topLevel = new Level(null, new List<Factor> { new Factor("Z") }, null, null); //实例化一个AhpModel,并设置第一层对象 AhpModel ahpModel = new AhpModel(topLevel); //插入一个层次,直到用户表示不再增减 while (true) { //默认插入第二层 InsertLevel(ahpModel); //然后询问是否继续插入其他层次 var ifAddLevel = DataHelper.ReadBool("是否插入一个层次"); if (!ifAddLevel) break; } //打印层次结构模型中的相关信息 ahpModel.DisplayModelInfo(); //选择用以生成决策矩阵的层次 //读入的数要-1 int selectLevel = DataHelper.ReadValus<int>(string.Format("请从第1层到第{0}层中选择一层来创建决策矩阵", ahpModel.Levels.Count))[0] - 1; //选择待评价的元素 int elementCount = DataHelper.ReadValus<int>("请问一共有多少待评价元素?")[0]; //建立空的决策矩阵 DecisionMatrix decisionMatrix = new DecisionMatrix(ahpModel.Levels[selectLevel], elementCount, "决策矩阵"); //读入决策矩阵数据 decisionMatrix.InsertMatrix(DataHelper.ConsoleArrayInput); //打印决策矩阵 decisionMatrix.DisplayMatrix(DataHelper.ConsoloOutput); //使用改进的标准化方法处理决策矩阵 Console.WriteLine("使用改进的归一化法对决策矩阵进行标准化法"); ManipulateDecisionMatrix(decisionMatrix, Standardizer.ApprovedNormalize); //使用常规归一化法处理决策矩阵 Console.WriteLine("使用常规的归一化法对决策矩阵进行标准化"); ManipulateDecisionMatrix(decisionMatrix, Standardizer.Normalize); }
public LevelDisplayForm(Level level) : this() { _level = level; Init(); }
/// <summary> /// 压入一个新的层次 /// </summary> /// <param name="newLevel">新的层次结构模型</param> public void PushLevel(Level newLevel) { _levels.Add(newLevel); }
public AhpModel(Level topLevel) { _levels = new List<Level>(); _levels.Add(topLevel); }
/// <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 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); }
//为传入的AhpModel加入一个层次 private static Level InsertLevel(AhpModel model) { //创建一个Level所必须的数据 LevelDataModel dataModel = new LevelDataModel(); //循环输入因素 while (true) { foreach (var factor in GetFactors()) { dataModel.Factors.Add(factor); } var ifAddFactor = DataHelper.ReadBool("是否继续添加因素"); if (!ifAddFactor) break; } //读入并设置指标的方向 while (true) { //首先读入一定数量的数据 var tags = DataHelper.ReadValus<string>("请输入指标各个指标的方向,+ 代表正向,- 代表逆向", dataModel.Factors.Count); //表示是否获得真确的 bool readSuccess = true; //依次设置Factor的正向还是逆向 for (int i = 0; i < tags.Count; i++) { //如果是正向,就继续,因为默认是正向 if (tags[i] == "+") continue; //如果是逆向,就设置 if (tags[i] == "-") { dataModel.Factors[i].Direction = FactorDirection.Negative; } //如果到这一步,说明有 +/- 以外的字符,则表示读入的数据错误,报错,并跳出循环,再重新读 else { readSuccess = false; break; } } if (readSuccess) break; Console.WriteLine("输入的数据中包含无法识别的字符,请重新输入"); } //输入关系矩阵 //设置当前model中的最后一个层次为新插入层次的Parent dataModel.Parent = model.GetLastLevel(); Matrix relationMatrix = new Matrix(dataModel.Factors.Count, dataModel.Parent.Factors.Count); Console.WriteLine("请输入关系矩阵"); relationMatrix.InsertMatrix(DataHelper.ConsoleArrayInput); //relationMatrix.InsertMatrix(DataHelper.ConsoloInput); dataModel.RelationMatrix = relationMatrix; //设置判断矩阵 Dictionary<Factor, JudgeMatrix> judgeMatrices = new Dictionary<Factor, JudgeMatrix>(); //此处可以选择使用那种方式输入判断矩阵 var judges = GetJudgeMatrices(relationMatrix, dataModel.Parent.Factors, dataModel.Factors); //var judges = GetJudgeMatrices(relationMatrix); for (int i = 0; i < relationMatrix.Y; i++) { judgeMatrices.Add(model.GetLastLevel().Factors[i], judges[i]); } dataModel.JudgeMatrices = judgeMatrices; //创建一个新的层次,并加入层次结构模型中 Level newLevel = new Level(dataModel); model.PushLevel(newLevel); return model.GetLastLevel(); }
//因为不提供对外修改的接口,所以只能通过构造函数输入所有的数据 /// <summary> /// 构造函数 /// </summary> /// <param name="parent">本层次的上一层次,必须指定,如果是顶层则为null</param> private Level(Level parent) { _parent = parent; }