C# AHP层次分析法:一致性校验

using System;
using System.Collections.Generic;
using System.Text;

namespace AHP
{
    public static class Formulas
    {
        //经验RI数组
        public static double[] RI = new double[15] { 0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.52, 1.54, 1.56, 1.58, 1.59 };

        //对比较矩阵进行归一化,返回特征向量W
        public static double[] normalize(double[][] matrix)
        {
            int row = matrix.Length;
            int column = matrix[0].Length;
           
            double[] Sum_column = new double[column];
            for (int i = 0; i < column; i++)
            {
                Sum_column[i] = 0;
                for (int j = 0; j < row; j++)
                {
                    Sum_column[i] += matrix[j][i];
                }
            }

            //进行归一化,计算特征向量W
            double[] w = new double[row];
            for (int i = 0; i < row; i++)
            {
                w[i] = 0;
                for (int j = 0; j < column; j++)
                {
                    w[i] += matrix[i][j] / Sum_column[j];
                }
                w[i] /= row;
            }

            return w;
        }

        //进行一致性校验
        public static checkResult checkCR(double[][] matrix)
        {
            checkResult res = new checkResult();

            int row = matrix.Length;
            int column = matrix[0].Length;

            res.w = Formulas.normalize(matrix);

            //计算AW
            double[] aw = new double[row];
            for (int i = 0; i < row; i++)
            {
                aw[i] = 0;
                for (int j = 0; j < column; j++)
                {
                    aw[i] += matrix[i][j] * res.w[j];
                }
            }

            res.body = matrix;

            //求和
            double sum = 0;
            for (int i = 0; i < row; i++)
            {
                sum += aw[i] / res.w[i];
            }

            //最大特征根
            res.r = sum / column;

            //一致性指标
            res.CI = (res.r - column) / (column - 1);

            //当前节点的检验系数
            res.CR = res.CI / Formulas.RI[column - 1];

            if (res.CR < 0.1)
            {
                res.success = true;
            }

            return res;
        }

        //将类似1/3格式的字符串转为数字
        public static double CalculationFormula(string formula)
        {
            if (formula.IndexOf('/') > 0)
            {
                string[] kt = formula.Split('/');
                return Double.Parse(kt[0]) / Double.Parse(kt[1]);
            }
            return Double.Parse(formula);
        }

        //代码结束
    }

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值