|
1 | 1 | package com.jdoe.algorithms; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
3 | 7 | import org.apache.commons.math3.linear.Array2DRowRealMatrix; |
4 | 8 | import org.apache.commons.math3.linear.RealMatrix; |
5 | 9 | import org.apache.logging.log4j.LogManager; |
6 | 10 | import org.apache.logging.log4j.Logger; |
7 | 11 |
|
| 12 | +import com.jdoe.util.FactorialUtility; |
| 13 | + |
8 | 14 | /* |
9 | 15 | * Copyright (c) 2025 Noor Mustafa |
10 | 16 | * |
@@ -141,19 +147,75 @@ public static RealMatrix fullFactorial2Level( int designFactorCount ) { |
141 | 147 | } |
142 | 148 |
|
143 | 149 | public static void fractionalFactorial( String generetor ) { |
144 | | - // validate generetor string |
145 | | - if ( !generetor.matches( "^[A-Za-z +\\-]+$" ) ) { |
146 | | - throw new IllegalArgumentException( "Generator string contains invalid characters. Allowed: letters, space, +, -" ); |
| 150 | + Array2DRowRealMatrix matrixFactory = new Array2DRowRealMatrix(); |
| 151 | + // validation |
| 152 | + FactorialUtility.validateGeneretor( generetor ); |
| 153 | + // parsing generetor |
| 154 | + List< String > factorList = Arrays.asList( generetor.trim().toLowerCase().split( " " ) ); |
| 155 | + List< Integer > combinationDesignFactorsIndexList = new ArrayList<>(); |
| 156 | + List< Integer > mainDesignFactorIndexList = new ArrayList<>(); |
| 157 | + for ( String factor : factorList ) { |
| 158 | + if ( factor.equalsIgnoreCase( " " ) ) { |
| 159 | + factorList.remove( factor ); |
| 160 | + } |
| 161 | + List< String > factorSubList = Arrays.asList( factor.split( "" ) ); |
| 162 | + if ( factorSubList.size() == 1 ) { |
| 163 | + mainDesignFactorIndexList.add( factorList.indexOf( factor ) ); |
| 164 | + } else if ( factorSubList.size() > 1 ) { |
| 165 | + combinationDesignFactorsIndexList.add( factorList.indexOf( factor ) ); |
| 166 | + } |
147 | 167 | } |
148 | | - int factorCount = 0; |
149 | | - for ( int i = 0; i < generetor.length() - 1; i++ ) { |
150 | | - if ( generetor.contains( " " ) ) { |
151 | | - factorCount += 1; |
| 168 | + // main design factor 2 level full factorial matrix |
| 169 | + int[] fullFactorialParam = new int[ mainDesignFactorIndexList.size() ]; |
| 170 | + List< RealMatrix > mainDesignFactor2LFFMatrixList = new ArrayList<>(); |
| 171 | + for ( int i = 0; mainDesignFactorIndexList.size() > i; i++ ) { |
| 172 | + mainDesignFactor2LFFMatrixList.add( fullFactorial2Level( 1 ) ); |
| 173 | + fullFactorialParam[ i ] = 2; // fixed level size |
| 174 | + } |
| 175 | + // creating the final result matrix |
| 176 | + Integer rowNum = fullFactorial( fullFactorialParam ).getRowDimension(); |
| 177 | + Integer colNum = factorList.size(); |
| 178 | + RealMatrix finalDesignMatrix = matrixFactory.createMatrix( rowNum, colNum ); |
| 179 | + |
| 180 | + // computing and populating matrix for main design factor factorial first |
| 181 | + for ( Integer mainDesignFactorIndexValue :mainDesignFactorIndexList) { |
| 182 | + for ( int i = 0; mainDesignFactor2LFFMatrixList.size() > i; i++ ) { |
| 183 | + RealMatrix currentMainDesignFactorMatrix = mainDesignFactor2LFFMatrixList.get( i ); |
| 184 | + for ( int j = 0; currentMainDesignFactorMatrix.getRowDimension() >= j; j++ ) { |
| 185 | + double currentMainDesignFactorMatrixValue = currentMainDesignFactorMatrix.getRow( j )[ 0 ]; |
| 186 | + finalDesignMatrix.setEntry( j, mainDesignFactorIndexValue, currentMainDesignFactorMatrixValue ); |
| 187 | + } |
152 | 188 | } |
153 | 189 | } |
154 | | - if ( generetor.split( " " ).length != factorCount ) { |
155 | | - throw new IllegalArgumentException( "Generator does not match the number of factors" ); |
| 190 | + // computing combination design factor and populate the final design matrix with it |
| 191 | + //? worry about the "-" and "+" cases in the genereter later first populate with product |
| 192 | + for ( Integer combinationDesignFactorIndexValue : combinationDesignFactorsIndexList ) { |
| 193 | + String[] factorCombinationSplit = factorList.get( combinationDesignFactorIndexValue ).split( "" ); |
| 194 | + /////////////// matching mainDesingnFactor /////////////////////////////// |
| 195 | + List< Integer > matchingMainFactorFinalMatrixIndexList = new ArrayList<>(); |
| 196 | + for ( int c = 0; factorCombinationSplit.length > c; c++ ) { |
| 197 | + for ( int m = 0; mainDesignFactorIndexList.size() > m; m++ ) { |
| 198 | + String currDesignFactorName = factorList.get( m ); |
| 199 | + if ( factorCombinationSplit[ c ].equalsIgnoreCase( currDesignFactorName ) ) { |
| 200 | + matchingMainFactorFinalMatrixIndexList.add( m ); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + ////////////////////////////////////////////////////////////// |
| 205 | + double mainFactorsProductValue = 1.0; |
| 206 | + for ( int c = 0; matchingMainFactorFinalMatrixIndexList.size() > c; c++ ) { |
| 207 | + for ( int r = 0; finalDesignMatrix.getRowDimension() > r; r++ ) { |
| 208 | + double mainFactorValue = finalDesignMatrix.getEntry( r, c ); |
| 209 | + mainFactorsProductValue *= mainFactorValue; |
| 210 | + } |
| 211 | + for ( int r = 0; finalDesignMatrix.getRowDimension() > r; r++ ) { |
| 212 | + // setting the combination factor value in the final matrix |
| 213 | + finalDesignMatrix.setEntry( r, combinationDesignFactorIndexValue, mainFactorsProductValue ); |
| 214 | + } |
| 215 | + } |
156 | 216 | } |
| 217 | + |
| 218 | + log.info( finalDesignMatrix ); |
157 | 219 | } |
158 | 220 |
|
159 | 221 | } |
0 commit comments