基于组件软件可信度量
课程:软件质量分析
作业
可编写下面的java程序:
package org.example;
import java.util.Arrays;
public class ComponentBasedMeasurementModel {
public static void main(String[] args) {
double[][] keyComponentJudgmentMatrix = {
{1.0, 2.0, 1.0 / 2.0, 2.0, 1.0 / 4.0},
{1.0 / 2.0, 1.0, 2.0, 3.0, 1.0 / 2.0},
{2.0, 1.0 / 2.0, 1.0, 1.0, 1.0 / 2.0},
{1.0 / 2.0, 1.0 / 3.0, 1.0, 1.0, 1.0 / 2.0},
{4.0, 2.0, 2.0, 2.0, 1.0}
};
double[][] non_keyComponentJudgmentMatrix = {
{1.0, 3.0, 2.0, 1.0 / 2.0, 2.0, 1.0, 3.0, 3.0},
{1.0 / 3.0, 1.0, 2.0, 1.0, 2.0, 2.0, 2.0, 2.0},
{1.0 / 2.0, 1.0 / 2.0, 1.0, 1.0 / 2.0, 1.0, 1.0 / 2.0, 3.0, 3.0},
{2.0, 1.0, 2.0, 1.0, 3.0, 1.0 / 2.0, 3.0, 3.0},
{1.0 / 2.0, 1.0 / 2.0, 1.0, 1.0 / 3.0, 1.0, 1.0, 2.0, 2.0},
{1.0, 1.0 / 2.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0},
{1.0 / 3.0, 1.0 / 2.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 2.0, 1.0 / 2.0, 1.0, 2.0},
{1.0 / 3.0, 1.0 / 2.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 2.0, 1.0 / 2.0, 1.0 / 2.0, 1.0}
};
double[][] componentWeights = {
{0.7, 0.3},
{0.6, 0.4},
{0.55, 0.45},
};
double[] keyComponentTrustworthiness = {8.430, 8.530, 6.042, 9.094, 8.289};
double[] non_keyComponentTrustworthiness = {6.192, 8.020, 7.984, 8.713, 9.211, 7.777, 7.897, 8.075};
double[] keyComponentWeightVector = CalculatingOptimalWeightVector.finalOptimalWeightVector(keyComponentJudgmentMatrix);
double[] non_keyComponentWeightVector = CalculatingOptimalWeightVector.finalOptimalWeightVector(non_keyComponentJudgmentMatrix);
for (double[] componentWeight : componentWeights) {
double T = calculateSystemTrustworthinessMeasurementValue(componentWeight, keyComponentWeightVector, non_keyComponentWeightVector, keyComponentTrustworthiness, non_keyComponentTrustworthiness);
System.out.println("关键组件组与非关键组件组的权重取" + Arrays.toString(componentWeight) + "的情况下,游戏服务器系统的可信度量值T为:" + T + ";可信等级Level为:" + getLevel(T, keyComponentTrustworthiness));
}
}
public static double calculateSystemTrustworthinessMeasurementValue(double[] componentWeight, double[] keyComponentWeightVector, double[] non_keyComponentWeightVector, double[] keyComponentTrustworthiness, double[] non_keyComponentTrustworthiness) {
double keyComponent = 1.0;
double non_keyComponent = 1.0;
for (int i = 0; i < keyComponentWeightVector.length; i++) {
keyComponent *= Math.pow(keyComponentTrustworthiness[i], keyComponentWeightVector[i]);
}
for (int i = 0; i < non_keyComponentWeightVector.length; i++) {
non_keyComponent *= Math.pow(non_keyComponentTrustworthiness[i], non_keyComponentWeightVector[i]);
}
return keyComponent * componentWeight[0] + non_keyComponent * componentWeight[1];
}
public static int getLevel(double T, double[] keyComponents) {
if (T >= 9.5 && levelDivide(5, keyComponents)) {
return 5;
} else if (T >= 8.5 && levelDivide(4, keyComponents)) {
return 4;
} else if (T >= 7.0 && levelDivide(3, keyComponents)) {
return 3;
} else if (T >= 4.5 && levelDivide(2, keyComponents)) {
return 2;
} else {
return 1;
}
}
public static boolean levelDivide(int level ,double[] keyComponents) {
int count = 0;
boolean flag = true;
if (level == 1) {
return true;
} else if (level == 2) {
for (double attribute : keyComponents) {
if (attribute < 4.5) {
count++;
}
}
return count <= keyComponents.length - Math.ceil(keyComponents.length * 2.0 / 3.0);
} else if (level == 3) {
for (double attribute : keyComponents) {
if (attribute < 7.0) {
count++;
}
if (attribute < 4.5) {
flag = false;
}
}
return count <= keyComponents.length - Math.ceil(keyComponents.length * 2.0 / 3.0) && flag;
} else if (level == 4) {
for (double attribute : keyComponents) {
if (attribute < 8.5) {
count++;
}
if (attribute < 7.0) {
flag = false;
}
}
return count <= keyComponents.length - Math.ceil(keyComponents.length * 2.0 / 3.0) && flag;
} else {
for (double attribute : keyComponents) {
if (attribute < 9.5) {
count++;
}
if (attribute < 8.5) {
flag = false;
}
}
return count <= keyComponents.length - Math.ceil(keyComponents.length * 2.0 / 3.0) && flag;
}
}
}
package org.example;
import Jama.EigenvalueDecomposition;
import Jama.Matrix;
import java.util.Random;
public class CalculatingOptimalWeightVector {
// public static void main(String[] args) {
// double[][] data = {
// {1.0, 1.0/2.0, 3.0, 2.0, 1.0/2.0},
// {2.0, 1.0, 2.0, 3.0, 2.0},
// {1.0/3.0, 1.0/2.0, 1.0, 2.0, 1.0/3.0},
// {1.0/2.0, 1.0/3.0, 1.0/2.0, 1.0, 2.0},
// {2.0, 1.0/2.0, 3.0, 1.0/2.0, 1.0}
// };
//
// double[] EV_weight = EV_OptimalWeightVector(data);
// double[] LLSM_weight = LLSM_OptimalWeightVector(data);
// double[] CSM_weight = CSM_OptimalWeightVector(data);
// System.out.println("EV_weight: " + java.util.Arrays.toString(EV_weight));
// System.out.println("LLSM_weight: " + java.util.Arrays.toString(LLSM_weight));
// System.out.println("CSM_weight: " + java.util.Arrays.toString(CSM_weight));
//
// double EV_accuracy = strengthEvaluateCriteria(data, EV_weight);
// double LLSM_accuracy = strengthEvaluateCriteria(data, LLSM_weight);
// double CSM_accuracy = strengthEvaluateCriteria(data, CSM_weight);
// System.out.println("EV_accuracy: " + EV_accuracy);
// System.out.println("LLSM_accuracy: " + LLSM_accuracy);
// System.out.println("CSM_accuracy: " + CSM_accuracy);
//
// double result = Math.min(EV_accuracy, Math.min(LLSM_accuracy, CSM_accuracy));
// String resultStr = result == EV_accuracy ? "EV" : result == LLSM_accuracy ? "LLSM" : "CSM";
// System.out.println("最优的权重向量是: " + resultStr);
// }
public static double[] finalOptimalWeightVector(double[][] data) {
double[] EV_weight = EV_OptimalWeightVector(data);
double[] LLSM_weight = LLSM_OptimalWeightVector(data);
double[] CSM_weight = CSM_OptimalWeightVector(data);
System.out.println("EV_weight: " + java.util.Arrays.toString(EV_weight));
System.out.println("LLSM_weight: " + java.util.Arrays.toString(LLSM_weight));
System.out.println("CSM_weight: " + java.util.Arrays.toString(CSM_weight));
double EV_accuracy = strengthEvaluateCriteria(data, EV_weight);
double LLSM_accuracy = strengthEvaluateCriteria(data, LLSM_weight);
double CSM_accuracy = strengthEvaluateCriteria(data, CSM_weight);
System.out.println("EV_accuracy: " + EV_accuracy);
System.out.println("LLSM_accuracy: " + LLSM_accuracy);
System.out.println("CSM_accuracy: " + CSM_accuracy);
double result = Math.min(EV_accuracy, Math.min(LLSM_accuracy, CSM_accuracy));
String resultStr = result == EV_accuracy ? "EV" : result == LLSM_accuracy ? "LLSM" : "CSM";
double[] resultWeightVector = result == EV_accuracy ? EV_weight : result == LLSM_accuracy ? LLSM_weight : CSM_weight;
System.out.println("最优的权重向量是: " + resultStr);
return resultWeightVector;
}
public static double[] EV_OptimalWeightVector(double[][] data) {
// 直接使用输入的 double 数组构造 Matrix 对象
Matrix A = new Matrix(data);
// 计算特征值和特征向量
EigenvalueDecomposition eig = A.eig();
Matrix D = eig.getD(); // 特征值矩阵
Matrix V = eig.getV(); // 特征向量矩阵
// 找到最大特征值及其对应的特征向量
double maxEigenvalue = Double.NEGATIVE_INFINITY;
int maxEigenvalueIndex = -1;
for (int i = 0; i < D.getRowDimension(); i++) {
if (D.get(i, i) > maxEigenvalue) {
maxEigenvalue = D.get(i, i);
maxEigenvalueIndex = i;
}
}
// 最大特征值对应的特征向量
Matrix eigenvector = V.getMatrix(0, V.getRowDimension() - 1, maxEigenvalueIndex, maxEigenvalueIndex);
// 获取特征向量的数组表示
double[][] eigenvectorArray = eigenvector.getArray();
double[] eigenvectorVector = new double[eigenvectorArray.length];
for (int i = 0; i < eigenvectorArray.length; i++) {
eigenvectorVector[i] = eigenvectorArray[i][0];
}
// 归一化处理
double sumOfAbsoluteValues = 0;
for (double value : eigenvectorVector) {
sumOfAbsoluteValues += Math.abs(value);
}
double[] normalizedVector = new double[eigenvectorVector.length];
for (int i = 0; i < eigenvectorVector.length; i++) {
normalizedVector[i] = Math.abs(eigenvectorVector[i] / sumOfAbsoluteValues);
}
return normalizedVector;
}
public static double[] LLSM_OptimalWeightVector(double[][] data) {
double[] A = new double[data.length];
for (int i = 0; i < data.length; i++) {
double product = 1.0;
for (int j = 0; j < data[i].length; j++) {
product *= data[i][j];
}
A[i] = Math.pow(product, 1.0 / data[i].length);
}
double sum = 0;
for (double v : A) {
sum += v;
}
double[] w = new double[A.length];
for (int i = 0; i < A.length; i++) {
w[i] = A[i] / sum;
}
return w;
}
public static double[] CSM_OptimalWeightVector(double[][] data) {
double epsilon = 1e-10; // 迭代精度
int maxIterations = 1000; // 最大迭代次数限制
double[] w = new double[data.length];
Random random = new Random();
double sum = 0;
for (int i = 0; i < data.length; i++) {
w[i] = random.nextDouble(); // 随机初始化解
sum += w[i];
}
for (int i = 0; i < data.length; i++) {
w[i] /= sum; // 归一化
}
int iteration = 0;
while (iteration < maxIterations) {
// System.out.println("Iteration " + iteration + ": w = " + java.util.Arrays.toString(w));
int m = -1;
double maxVal = -1.0;
for (int i = 0; i < data.length; i++) {
double val = 0;
for (int j = 0; j < data[i].length; j++) {
val += (1 + data[j][i] * data[j][i]) * (w[i] / w[j]) - (1 + data[i][i] * data[i][i]) * (w[j] / w[i]);
}
val = Math.abs(val);
if (maxVal == -1 || val > maxVal) {
maxVal = val;
m = i;
}
}
if (maxVal <= epsilon) {
break;
}
double up = 0;
double bottom = 0;
for (int j = 0; j < data.length; j++) {
if (j != m) {
up += (1 + data[m][j] * data[m][j]) * (w[j] / w[m]);
bottom += (1 + data[j][m] * data[j][m]) * (w[m] / w[j]);
}
}
double T = Math.pow(up / bottom, 1.0 / 2);
double[] X = w.clone();
X[m] *= T;
sum = 0;
for (int i = 0; i < data.length; i++) {
sum += X[i];
}
for (int i = 0; i < data.length; i++) {
w[i] = X[i] / sum;
}
iteration++;
}
if (iteration >= maxIterations) {
// System.out.println("达到最大迭代次数限制,可能未收敛");
}
return w;
}
public static double strengthEvaluateCriteria(double[][] data, double[] weight) {
double result = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
result += Math.abs(data[i][j] - weight[i]/weight[j]);
}
}
return result;
}
}
运行结果
EV_weight: [0.17276899543845872, 0.20021897469030842, 0.16090764808578253, 0.10824147110711889, 0.35786291067833154]
LLSM_weight: [0.16020622338782942, 0.1995738491931742, 0.16020622338782942, 0.11195645349939282, 0.3680572505317743]
CSM_weight: [0.15419976038001187, 0.22068013571004072, 0.21423159815422463, 0.15921403290363184, 0.251674472852091]
EV_accuracy: 11.411122021707987
LLSM_accuracy: 11.376383332147004
CSM_accuracy: 14.367286549368558
最优的权重向量是: LLSM
EV_weight: [0.19053688357561377, 0.15544138299287066, 0.1024342806572344, 0.1895940790972254, 0.09457869275270668, 0.1542833022115746, 0.06135727698762504, 0.05177410172514951]
LLSM_weight: [0.18791561772034962, 0.1534324593715871, 0.10621331697844344, 0.18791561772034962, 0.09948864114022907, 0.14801301469802347, 0.06356758120738136, 0.05345375116363638]
CSM_weight: [0.19858023599711344, 0.15035415634188057, 0.12626501083363526, 0.18906511184074648, 0.08133444881799352, 0.12542805023497544, 0.06420014182674112, 0.06477284410691417]
EV_accuracy: 22.505995725156936
LLSM_accuracy: 21.95620770909283
CSM_accuracy: 23.07993133107597
最优的权重向量是: LLSM
关键组件组与非关键组件组的权重取[0.7, 0.3]的情况下,游戏服务器系统的可信度量值T为:7.967173859639105;可信等级Level为:3
关键组件组与非关键组件组的权重取[0.6, 0.4]的情况下,游戏服务器系统的可信度量值T为:7.946533396594098;可信等级Level为:3
关键组件组与非关键组件组的权重取[0.55, 0.45]的情况下,游戏服务器系统的可信度量值T为:7.936213165071594;可信等级Level为:3