99
1010public class CompositeDOE {
1111
12- private static final String [] alphaParamLegalExpressions = { "orthogonal" , "o" , "rotatable" , "r" };
12+ private static final String [] alphaParamLegalExpressions = {"orthogonal" , "o" , "rotatable" , "r" };
1313
14- private static final String [] faceParamLegalExpressions = { "circumscribed" , "ccc" , "inscribed" , "cci" , "faced" , "ccf" };
14+ private static final String [] faceParamLegalExpressions = {"circumscribed" , "ccc" , "inscribed" , "cci" , "faced" , "ccf" };
1515
16- public static void centralCompositeDesign ( @ NotNull int numberOfFactors , @ NotNull int [] centerPoints , @ NotNull String alpha ,
17- @ NotNull String face ) {
16+ public static RealMatrix centralCompositeDesign (@ NotNull int numberOfFactors , @ NotNull int [] centerPoints , @ NotNull String alpha , @ NotNull String face ) {
17+ validateCompositeDegisnParameters ( numberOfFactors , centerPoints , alpha , face );
1818
19- validateCompositeDegisnParameters ( numberOfFactors , centerPoints , alpha , face );
19+ RealMatrix factorialDesignMatrix ;
20+ RealMatrix starDesignMatrix ;
21+ double alphaValue ;
2022
21- RealMatrix fullFactorial2LevelMatrix = FactorialDOE .fullFactorial2Level ( numberOfFactors );
23+ // Orthogonal Design
24+ if (alpha .toLowerCase ().equals ("orthogonal" ) || alpha .toLowerCase ().equals ("o" )) {
25+ Object [] starResult = StarDOE .star (numberOfFactors , "orthogonal" , centerPoints );
26+ starDesignMatrix = new Array2DRowRealMatrix ((double [][]) starResult [0 ]); // Convert array to RealMatrix
27+ alphaValue = (double ) starResult [1 ];
28+ }
29+ // Rotatable Design
30+ else if (alpha .toLowerCase ().equals ("rotatable" ) || alpha .toLowerCase ().equals ("r" )) {
31+ Object [] starResult = StarDOE .star (numberOfFactors , "rotatable" , new int []{0 , 0 });
32+ starDesignMatrix = new Array2DRowRealMatrix ((double [][]) starResult [0 ]); // Convert array to RealMatrix
33+ alphaValue = (double ) starResult [1 ];
34+ } else {
35+ throw new IllegalArgumentException ("Invalid alpha parameter: " + alpha );
36+ }
37+
38+ // Inscribed CCD
39+ if (face .toLowerCase ().equals ("inscribed" ) || face .toLowerCase ().equals ("cci" )) {
40+ factorialDesignMatrix = FactorialDOE .fullFactorial2Level (numberOfFactors );
41+ factorialDesignMatrix = factorialDesignMatrix .scalarMultiply (1.0 / alphaValue ); // Scale down the factorial points
42+ Object [] starResult = StarDOE .star (numberOfFactors , alpha , centerPoints );
43+ starDesignMatrix = new Array2DRowRealMatrix ((double [][]) starResult [0 ]); // Convert array to RealMatrix
44+ alphaValue = (double ) starResult [1 ];
45+ }
46+ // Faced CCD
47+ else if (face .toLowerCase ().equals ("faced" ) || face .toLowerCase ().equals ("ccf" )) {
48+ Object [] starResult = StarDOE .star (numberOfFactors , alpha , centerPoints );
49+ starDesignMatrix = new Array2DRowRealMatrix ((double [][]) starResult [0 ]); // Convert array to RealMatrix
50+ alphaValue = 1.0 ;
51+ // Value of alpha is always 1 in Faced CCD
52+ factorialDesignMatrix = FactorialDOE .fullFactorial2Level (numberOfFactors );
53+ }
54+ // Circumscribed CCD
55+ else if (face .toLowerCase ().equals ("circumscribed" ) || face .toLowerCase ().equals ("ccc" )) {
56+ factorialDesignMatrix = FactorialDOE .fullFactorial2Level (numberOfFactors );
57+ } else {
58+ throw new IllegalArgumentException ("Invalid face parameter: " + face );
59+ }
2260
61+ RealMatrix centerPointsMatrix1 = RepeatCenterDOE .repeatCenter (numberOfFactors , centerPoints [0 ]);
62+ RealMatrix centerPointsMatrix2 = RepeatCenterDOE .repeatCenter (numberOfFactors , centerPoints [1 ]);
63+
64+ factorialDesignMatrix = UnionDOE .matrixUnion (factorialDesignMatrix , centerPointsMatrix1 );
65+ starDesignMatrix = UnionDOE .matrixUnion (starDesignMatrix , centerPointsMatrix2 );
66+ RealMatrix finalDesignMatrix = UnionDOE .matrixUnion (factorialDesignMatrix , starDesignMatrix );
67+
68+ return finalDesignMatrix ;
2369 }
2470
2571 //<-----------------------------Utility Methods---------------------------------->
2672
27- public static void validateCompositeDegisnParameters ( int numberOfFactors , int [] centerPoints , String alpha , String face ) {
28- if ( numberOfFactors < 1 ) {
29- throw new IllegalArgumentException ( "number of factors must be greater then 1" );
73+ public static void validateCompositeDegisnParameters (int numberOfFactors , int [] centerPoints , String alpha , String face ) {
74+ if (numberOfFactors < 1 ) {
75+ throw new IllegalArgumentException ("number of factors must be greater than 1" );
3076 }
31- if ( centerPoints .length == 2 ) {
32- throw new IllegalArgumentException (
33- String .format ( "Invalid number of values for \" center\" (expected 2, but got %d )" , centerPoints .length ) );
77+ if (centerPoints .length != 2 ) { // Fixed: was == now !=
78+ throw new IllegalArgumentException (String .format ("Invalid number of values for \" center\" (expected 2, but got %d)" , centerPoints .length ));
3479 }
35- if ( validateExpression ( alpha , alphaParamLegalExpressions ) == Boolean .FALSE ) {
36- throw new IllegalArgumentException (
37- String .format ( "Invalid value for \" alpha\" (expected format %d )" , alphaParamLegalExpressions .toString () ) );
80+ if (validateExpression (alpha , alphaParamLegalExpressions ) == Boolean .FALSE ) {
81+ throw new IllegalArgumentException (String .format ("Invalid value for \" alpha\" (expected one of %s)" , java .util .Arrays .toString (alphaParamLegalExpressions )));
3882 }
39- if ( validateExpression ( alpha , faceParamLegalExpressions ) == Boolean .FALSE ) {
40- throw new IllegalArgumentException (
41- String .format ( "Invalid value for \" face\" (expected format %d )" , faceParamLegalExpressions .toString () ) );
83+ if (validateExpression (face , faceParamLegalExpressions ) == Boolean .FALSE ) {
84+ throw new IllegalArgumentException (String .format ("Invalid value for \" face\" (expected one of %s)" , java .util .Arrays .toString (faceParamLegalExpressions )));
4285 }
4386 }
4487
45- public static Boolean validateExpression ( String expression , String [] legalExpressions ) {
88+ public static Boolean validateExpression (String expression , String [] legalExpressions ) {
4689 Boolean validInput = Boolean .FALSE ;
47- for ( String legalExpression : legalExpressions ) {
48- if ( legalExpression .equals ( expression ) ) {
90+ for (String legalExpression : legalExpressions ) {
91+ if (legalExpression .equals (expression ) ) {
4992 validInput = Boolean .TRUE ;
5093 break ;
5194 }
5295 }
5396 return validInput ;
5497 }
5598
56- public static Boolean matrixUnion ( RealMatrix firstMatrix , RealMatrix secondMatrix ) {
57- double [][] finalMatrixRowModel = new double [ firstMatrix .getRowDimension () + secondMatrix .getRowDimension () ][];
58- for ( int i = 0 ; i < firstMatrix .getRowDimension (); i ++ ) {
59- finalMatrixRowModel [ i ] = firstMatrix .getRow ( i );
60- }
61- for ( int i = 0 ; i < secondMatrix .getRowDimension (); i ++ ) {
62- finalMatrixRowModel [ i ] = secondMatrix .getRow ( i );
63- }
64- RealMatrix finalMatrix = new Array2DRowRealMatrix (finalMatrixRowModel );
65- GenericDOEUtil .matrixLogger ( finalMatrix , "matrixUnion" );
66- }
67-
6899}
69100
70101//<-----------------------------STEPS---------------------------------->
71102
72103/**
73104 * Detailed Steps to Port Central Composite Design Script to Java
74- *
105+ * <p>
75106 * $ 1. Create CompositeDOE Class Structure Create CompositeDOE.java in com.jdoe.algorithms package Add imports:
76107 * org.apache.commons.math3.linear.* for matrix operations Define method signature: public static RealMatrix centralCompositeDesign(int n,
77108 * int[] center, String alpha, String face) Set default parameters handling for center, alpha, and face
78- *
109+ * <p>
79110 * $ 2. Implement Input Validation Logic Add assertion: assert n > 1 : "\"n\" must be an integer greater than 1" Validate alpha parameter
80111 * with: alpha.toLowerCase() and check against allowed values Validate face parameter with: face.toLowerCase() and check against allowed
81112 * values Validate center array length: if (center.length != 2) throw IllegalArgumentException Format error messages to match Python
82113 * implementation
83- *
114+ * <p>
84115 * $ 3. Implement Star Point Generation Create StarDOE.java class with star method Calculate alpha based on alpha type: For orthogonal:
85116 * 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,
86117 * etc. Generate 2n star points with values [±alpha, 0, ..., 0], [0, ±alpha, ..., 0], etc. Return both star matrix and alpha value as an
87118 * Object array
88- *
119+ * <p>
89120 * 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
90121 * manipulation: for each row i and column j, set value to ((i >> j) & 1) == 0 ? -1 : 1 Return RealMatrix object
91- *
122+ * <p>
92123 * 5. Create UnionDOE Class Implement union method that combines two matrices vertically Use Array2DRowRealMatrix constructor to create new
93124 * matrix with combined rows Copy data from both input matrices to the result matrix Return the concatenated matrix
94- *
125+ * <p>
95126 * 6. Create RepeatCenterDOE Class Implement repeatCenter method that generates center points Create repeats × n matrix filled with zeros
96127 * Use new Array2DRowRealMatrix(repeats, n) to initialize matrix Return the center point matrix
97- *
128+ * <p>
98129 * 7. Implement Core Algorithm Logic Initialize H1 andH2 matrices based on face type For inscribed (cci): scale factorial points H1 =
99130 * H1.scalarMultiply(1.0/a) For faced (ccf): set alpha to 1.0 For circumscribed (ccc): keep original factorial points Generate center points
100131 * C1 and C2 using repeatCenter Combine matrices: H1 = union(H1, C1), H2 = union(H2, C2), H = union(H1, H2)
101- *
132+ * <p>
102133 * 8. Handle Matrix Operations Implement matrix scaling using scalarMultiply() method from Commons Math Ensure all matrix dimensions are
103134 * compatible for union operations Use getSubMatrix() and setSubMatrix() for matrix manipulations Return final RealMatrix object
104- *
135+ * <p>
105136 * 9. Create Unit Tests Create CompositeDOETest.java with comprehensive test cases Test all three face types: ccc, cci, ccf Test both alpha
106137 * types: orthogonal, rotatable Validate matrix dimensions: (2^n + 2n + sum(center)) × n Verify center point counts and star point
107138 * distances
108- *
139+ * <p>
109140 * 10. Add Documentation and Error Handling Add JavaDoc explaining parameters, return value, and exceptions Implement proper exception
110141 * handling for invalid inputs Include example usage in documentation Follow the same error message format as Python implementation
111142 */
0 commit comments