-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompositeDOE.java
More file actions
142 lines (124 loc) · 8.4 KB
/
CompositeDOE.java
File metadata and controls
142 lines (124 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.doe.algorithms;
import org.apache.commons.math3.linear.Array2DRowFieldMatrix;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.jetbrains.annotations.NotNull;
import com.doe.util.GenericDOEUtil;
public class CompositeDOE {
private static final String[] alphaParamLegalExpressions = {"orthogonal", "o", "rotatable", "r"};
private static final String[] faceParamLegalExpressions = {"circumscribed", "ccc", "inscribed", "cci", "faced", "ccf"};
public static RealMatrix centralCompositeDesign(@NotNull int numberOfFactors, @NotNull int[] centerPoints, @NotNull String alpha, @NotNull String face) {
validateCompositeDegisnParameters(numberOfFactors, centerPoints, alpha, face);
RealMatrix factorialDesignMatrix;
RealMatrix starDesignMatrix;
double alphaValue;
// Orthogonal Design
if (alpha.toLowerCase().equals("orthogonal") || alpha.toLowerCase().equals("o")) {
Object[] starResult = StarDOE.star(numberOfFactors, "orthogonal", centerPoints);
starDesignMatrix = new Array2DRowRealMatrix((double[][]) starResult[0]); // Convert array to RealMatrix
alphaValue = (double) starResult[1];
}
// Rotatable Design
else if (alpha.toLowerCase().equals("rotatable") || alpha.toLowerCase().equals("r")) {
Object[] starResult = StarDOE.star(numberOfFactors, "rotatable", new int[]{0, 0});
starDesignMatrix = new Array2DRowRealMatrix((double[][]) starResult[0]); // Convert array to RealMatrix
alphaValue = (double) starResult[1];
} else {
throw new IllegalArgumentException("Invalid alpha parameter: " + alpha);
}
// Inscribed CCD
if (face.toLowerCase().equals("inscribed") || face.toLowerCase().equals("cci")) {
factorialDesignMatrix = FactorialDOE.fullFactorial2Level(numberOfFactors);
factorialDesignMatrix = factorialDesignMatrix.scalarMultiply(1.0 / alphaValue); // Scale down the factorial points
Object[] starResult = StarDOE.star(numberOfFactors, alpha, centerPoints);
starDesignMatrix = new Array2DRowRealMatrix((double[][]) starResult[0]); // Convert array to RealMatrix
alphaValue = (double) starResult[1];
}
// Faced CCD
else if (face.toLowerCase().equals("faced") || face.toLowerCase().equals("ccf")) {
Object[] starResult = StarDOE.star(numberOfFactors, alpha, centerPoints);
starDesignMatrix = new Array2DRowRealMatrix((double[][]) starResult[0]); // Convert array to RealMatrix
alphaValue = 1.0;
// Value of alpha is always 1 in Faced CCD
factorialDesignMatrix = FactorialDOE.fullFactorial2Level(numberOfFactors);
}
// Circumscribed CCD
else if (face.toLowerCase().equals("circumscribed") || face.toLowerCase().equals("ccc")) {
factorialDesignMatrix = FactorialDOE.fullFactorial2Level(numberOfFactors);
} else {
throw new IllegalArgumentException("Invalid face parameter: " + face);
}
RealMatrix centerPointsMatrix1 = RepeatCenterDOE.repeatCenter(numberOfFactors, centerPoints[0]);
RealMatrix centerPointsMatrix2 = RepeatCenterDOE.repeatCenter(numberOfFactors, centerPoints[1]);
factorialDesignMatrix = UnionDOE.matrixUnion(factorialDesignMatrix, centerPointsMatrix1);
starDesignMatrix = UnionDOE.matrixUnion(starDesignMatrix, centerPointsMatrix2);
RealMatrix finalDesignMatrix = UnionDOE.matrixUnion(factorialDesignMatrix, starDesignMatrix);
return finalDesignMatrix;
}
//<-----------------------------Utility Methods---------------------------------->
public static void validateCompositeDegisnParameters(int numberOfFactors, int[] centerPoints, String alpha, String face) {
if (numberOfFactors < 1) {
throw new IllegalArgumentException("number of factors must be greater than 1");
}
if (centerPoints.length != 2) { // Fixed: was == now !=
throw new IllegalArgumentException(String.format("Invalid number of values for \"center\" (expected 2, but got %d)", centerPoints.length));
}
if (validateExpression(alpha, alphaParamLegalExpressions) == Boolean.FALSE) {
throw new IllegalArgumentException(String.format("Invalid value for \"alpha\" (expected one of %s)", java.util.Arrays.toString(alphaParamLegalExpressions)));
}
if (validateExpression(face, faceParamLegalExpressions) == Boolean.FALSE) {
throw new IllegalArgumentException(String.format("Invalid value for \"face\" (expected one of %s)", java.util.Arrays.toString(faceParamLegalExpressions)));
}
}
public static Boolean validateExpression(String expression, String[] legalExpressions) {
Boolean validInput = Boolean.FALSE;
for (String legalExpression : legalExpressions) {
if (legalExpression.equals(expression)) {
validInput = Boolean.TRUE;
break;
}
}
return validInput;
}
}
//<-----------------------------STEPS---------------------------------->
/**
* Detailed Steps to Port Central Composite Design Script to Java
* <p>
* $ 1. Create CompositeDOE Class Structure Create CompositeDOE.java in com.doe.algorithms package Add imports:
* org.apache.commons.math3.linear.* for matrix operations Define method signature: public static RealMatrix centralCompositeDesign(int n,
* int[] center, String alpha, String face) Set default parameters handling for center, alpha, and face
* <p>
* $ 2. Implement Input Validation Logic Add assertion: assert n > 1 : "\"n\" must be an integer greater than 1" Validate alpha parameter
* with: alpha.toLowerCase() and check against allowed values Validate face parameter with: face.toLowerCase() and check against allowed
* values Validate center array length: if (center.length != 2) throw IllegalArgumentException Format error messages to match Python
* implementation
* <p>
* $ 3. Implement Star Point Generation Create StarDOE.java class with star method Calculate alpha based on alpha type: For orthogonal:
* alpha = Math.pow(2 * factorial(n), 0.25) where factorial(n) = n! For rotatable: alpha = Math.pow(2, 0.5) for 2D, Math.pow(3, 0.5) for 3D,
* etc. Generate 2n star points with values [±alpha, 0, ..., 0], [0, ±alpha, ..., 0], etc. Return both star matrix and alpha value as an
* Object array
* <p>
* 4. Enhance FactorialDOE Class Add ff2n method that generates 2-level factorial design Create 2^n × n matrix with values -1 and +1 Use bit
* manipulation: for each row i and column j, set value to ((i >> j) & 1) == 0 ? -1 : 1 Return RealMatrix object
* <p>
* 5. Create UnionDOE Class Implement union method that combines two matrices vertically Use Array2DRowRealMatrix constructor to create new
* matrix with combined rows Copy data from both input matrices to the result matrix Return the concatenated matrix
* <p>
* 6. Create RepeatCenterDOE Class Implement repeatCenter method that generates center points Create repeats × n matrix filled with zeros
* Use new Array2DRowRealMatrix(repeats, n) to initialize matrix Return the center point matrix
* <p>
* 7. Implement Core Algorithm Logic Initialize H1 andH2 matrices based on face type For inscribed (cci): scale factorial points H1 =
* H1.scalarMultiply(1.0/a) For faced (ccf): set alpha to 1.0 For circumscribed (ccc): keep original factorial points Generate center points
* C1 and C2 using repeatCenter Combine matrices: H1 = union(H1, C1), H2 = union(H2, C2), H = union(H1, H2)
* <p>
* 8. Handle Matrix Operations Implement matrix scaling using scalarMultiply() method from Commons Math Ensure all matrix dimensions are
* compatible for union operations Use getSubMatrix() and setSubMatrix() for matrix manipulations Return final RealMatrix object
* <p>
* 9. Create Unit Tests Create CompositeDOETest.java with comprehensive test cases Test all three face types: ccc, cci, ccf Test both alpha
* types: orthogonal, rotatable Validate matrix dimensions: (2^n + 2n + sum(center)) × n Verify center point counts and star point
* distances
* <p>
* 10. Add Documentation and Error Handling Add JavaDoc explaining parameters, return value, and exceptions Implement proper exception
* handling for invalid inputs Include example usage in documentation Follow the same error message format as Python implementation
*/