|
| 1 | +package com.jdoe.algorithms; |
| 2 | + |
| 3 | +import org.apache.commons.math3.linear.Array2DRowRealMatrix; |
| 4 | +import org.apache.commons.math3.linear.RealMatrix; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class DoehlertDOE { |
| 9 | + |
| 10 | + /** |
| 11 | + * Generate a Doehlert design matrix for a given number of factors using a shell approach. |
| 12 | + * |
| 13 | + * @param numFactors Number of factors (variables) |
| 14 | + * @param numCenterPoints Number of center (replicate) points |
| 15 | + * @return Design matrix of shape (N, numFactors), where N = k^2 + k + C |
| 16 | + */ |
| 17 | + public static RealMatrix doehlertShellDesign(int numFactors, int numCenterPoints) { |
| 18 | + if (numFactors < 1) { |
| 19 | + throw new IllegalArgumentException("Number of factors must be at least 1."); |
| 20 | + } |
| 21 | + |
| 22 | + List<double[]> designPoints = new ArrayList<>(); |
| 23 | + |
| 24 | + // Add center points |
| 25 | + for (int i = 0; i < numCenterPoints; i++) { |
| 26 | + double[] centerPoint = new double[numFactors]; |
| 27 | + for (int j = 0; j < numFactors; j++) { |
| 28 | + centerPoint[j] = 0.0; |
| 29 | + } |
| 30 | + designPoints.add(centerPoint); |
| 31 | + } |
| 32 | + |
| 33 | + // Add shells progressively |
| 34 | + for (int shellIndex = 1; shellIndex <= numFactors; shellIndex++) { |
| 35 | + int numShellPoints = shellIndex + 1; |
| 36 | + double radius = Math.sqrt(shellIndex / (double) numFactors); |
| 37 | + |
| 38 | + for (int pointIdx = 0; pointIdx < numShellPoints; pointIdx++) { |
| 39 | + double[] point = new double[numFactors]; |
| 40 | + double angle = (2.0 * Math.PI * pointIdx) / numShellPoints; |
| 41 | + |
| 42 | + if (numFactors > 0) point[0] = radius * Math.cos(angle); |
| 43 | + if (numFactors > 1) point[1] = radius * Math.sin(angle); |
| 44 | + |
| 45 | + designPoints.add(point); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Convert list of points to matrix |
| 50 | + double[][] result = new double[designPoints.size()][numFactors]; |
| 51 | + for (int i = 0; i < designPoints.size(); i++) { |
| 52 | + result[i] = designPoints.get(i); |
| 53 | + } |
| 54 | + |
| 55 | + return new Array2DRowRealMatrix(result); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Overloaded method with default center points (1) |
| 60 | + */ |
| 61 | + public static RealMatrix doehlertShellDesign(int numFactors) { |
| 62 | + return doehlertShellDesign(numFactors, 1); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Generate a Doehlert design matrix using a simplex-based approach. |
| 67 | + * |
| 68 | + * @param numFactors Number of factors included in the design |
| 69 | + * @return Design matrix with experiments at different coded levels |
| 70 | + */ |
| 71 | + public static RealMatrix doehlertSimplexDesign(int numFactors) { |
| 72 | + double[][] simplexMatrix = new double[numFactors + 1][numFactors]; |
| 73 | + |
| 74 | + for (int i = 1; i <= numFactors; i++) { |
| 75 | + for (int j = 1; j <= i; j++) { |
| 76 | + if (j == i) { |
| 77 | + simplexMatrix[i][j - 1] = Math.sqrt(i + 1) / Math.sqrt(2 * i); |
| 78 | + } else { |
| 79 | + int denom = 2 * (i - (j - 1)) * (i - j); |
| 80 | + simplexMatrix[i][i - j] = 1 / Math.sqrt(denom); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + List<double[]> extraPoints = new ArrayList<>(); |
| 86 | + for (int i = 0; i <= numFactors; i++) { |
| 87 | + List<Integer> rangeList = new ArrayList<>(); |
| 88 | + for (int k = 1; k < i; k++) rangeList.add(k); |
| 89 | + for (int k = i + 1; k <= numFactors; k++) rangeList.add(k); |
| 90 | + |
| 91 | + for (int j : rangeList) { |
| 92 | + double[] point = new double[numFactors]; |
| 93 | + for (int k = 0; k < numFactors; k++) { |
| 94 | + point[k] = simplexMatrix[i][k] - simplexMatrix[j][k]; |
| 95 | + } |
| 96 | + extraPoints.add(point); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Combine matrices |
| 101 | + int totalRows = (numFactors + 1) + extraPoints.size(); |
| 102 | + double[][] fullMatrix = new double[totalRows][numFactors]; |
| 103 | + |
| 104 | + // Copy simplex matrix |
| 105 | + for (int i = 0; i < simplexMatrix.length; i++) { |
| 106 | + fullMatrix[i] = simplexMatrix[i]; |
| 107 | + } |
| 108 | + |
| 109 | + // Add extra points |
| 110 | + for (int i = 0; i < extraPoints.size(); i++) { |
| 111 | + fullMatrix[simplexMatrix.length + i] = extraPoints.get(i); |
| 112 | + } |
| 113 | + |
| 114 | + return new Array2DRowRealMatrix(fullMatrix); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments