|
| 1 | +package com.jdoe.algorithms; |
| 2 | + |
| 3 | +import org.apache.commons.math3.linear.Array2DRowRealMatrix; |
| 4 | +import org.apache.commons.math3.linear.RealMatrix; |
| 5 | +import org.apache.logging.log4j.LogManager; |
| 6 | +import org.apache.logging.log4j.Logger; |
| 7 | + |
| 8 | +/* |
| 9 | + * Copyright (c) 2025 Noor Mustafa |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +public class FactorialDOE { |
| 25 | + |
| 26 | + |
| 27 | + private static final Logger log = LogManager.getLogger(FactorialDOE.class); |
| 28 | + |
| 29 | + /** |
| 30 | + * Generates a full factorial design matrix for a set of design factors. |
| 31 | + * <p> |
| 32 | + * A full factorial design enumerates all possible combinations of discrete levels |
| 33 | + * for the provided design factors. Each row in the resulting matrix corresponds |
| 34 | + * to one combination of factor levels, and each column corresponds to a design factor. |
| 35 | + * This method is useful in design of experiments (DOE) where exhaustive sampling |
| 36 | + * of the factor space is required. |
| 37 | + * <p> |
| 38 | + * The input is an integer array where each element represents the number of discrete |
| 39 | + * levels for a corresponding design factor. |
| 40 | + * <p> |
| 41 | + * Example: |
| 42 | + * <pre> |
| 43 | + * int[] factorLevels = {2, 3, 6}; // 3 design factors: 2 levels, 3 levels, 6 levels |
| 44 | + * FactorialDOE.FullFactorial(factorLevels); |
| 45 | + * </pre> |
| 46 | + * <p> |
| 47 | + * Example output (debug log): |
| 48 | + * <pre> |
| 49 | + * Total combinations: 36 |
| 50 | + * Matrix: |
| 51 | + * {{0.0,0.0,0.0}, |
| 52 | + * {1.0,0.0,0.0}, |
| 53 | + * {0.0,1.0,0.0}, |
| 54 | + * {1.0,1.0,0.0}, |
| 55 | + * {0.0,2.0,0.0}, |
| 56 | + * {1.0,2.0,0.0}, |
| 57 | + * ... |
| 58 | + * {1.0,2.0,5.0}} |
| 59 | + * </pre> |
| 60 | + * <p> |
| 61 | + * Notes: |
| 62 | + * - Number of rows = product of all input factor levels. |
| 63 | + * - Number of columns = number of design factors. |
| 64 | + * - Values are zero-indexed, representing the level number for each factor. |
| 65 | + * - This method only generates the combinations; it does not perform optimization. |
| 66 | + * |
| 67 | + * @param designFactorLevelArray an array where each element specifies the number of levels for a design factor |
| 68 | + */ |
| 69 | + public static RealMatrix FullFactorial(int[] designFactorLevelArray) { |
| 70 | + // calculate matrix size by number of combinations |
| 71 | + int length = designFactorLevelArray.length; |
| 72 | + int combinationCount = 1; |
| 73 | + for (int i = 0; i < length; i++) { |
| 74 | + combinationCount *= designFactorLevelArray[i]; |
| 75 | + } |
| 76 | + log.debug("Total Combinations: " + combinationCount); |
| 77 | + // create matrix for storing combinations |
| 78 | + Array2DRowRealMatrix matrixFactory = new Array2DRowRealMatrix(); |
| 79 | + RealMatrix matrix = matrixFactory.createMatrix(combinationCount, length); |
| 80 | + |
| 81 | + // populate matrix |
| 82 | + for (int rowNum = 0; rowNum < combinationCount; rowNum++) { |
| 83 | + int temp = rowNum; |
| 84 | + for (int colNum = 0; colNum < designFactorLevelArray.length; colNum++) { |
| 85 | + matrix.setEntry(rowNum, colNum, temp % designFactorLevelArray[colNum]); |
| 86 | + temp /= designFactorLevelArray[colNum]; |
| 87 | + } |
| 88 | + } |
| 89 | + log.debug(matrix.toString()); |
| 90 | + return matrix; |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments