Skip to content

Commit 56d340d

Browse files
author
nofa
committed
fractional factorial implementation
1 parent f9d5bc0 commit 56d340d

3 files changed

Lines changed: 93 additions & 11 deletions

File tree

src/main/java/com/jdoe/Testing.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class Testing
1212
public static void main( String[] args )
1313
{
1414
int[] arrayOfLevels = {2,3,6};
15-
FactorialDOE.fullFactorial(arrayOfLevels);
16-
FactorialDOE.fullFactorial2Level( arrayOfLevels.length );
15+
// FactorialDOE.fullFactorial(arrayOfLevels);
16+
// FactorialDOE.fullFactorial2Level( arrayOfLevels.length );
17+
FactorialDOE.fractionalFactorial( "a b ab" );
1718
}
1819
}

src/main/java/com/jdoe/algorithms/FactorialDOE.java

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.jdoe.algorithms;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
37
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
48
import org.apache.commons.math3.linear.RealMatrix;
59
import org.apache.logging.log4j.LogManager;
610
import org.apache.logging.log4j.Logger;
711

12+
import com.jdoe.util.FactorialUtility;
13+
814
/*
915
* Copyright (c) 2025 Noor Mustafa
1016
*
@@ -141,19 +147,75 @@ public static RealMatrix fullFactorial2Level( int designFactorCount ) {
141147
}
142148

143149
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+
}
147167
}
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+
}
152188
}
153189
}
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+
}
156216
}
217+
218+
log.info( finalDesignMatrix );
157219
}
158220

159221
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jdoe.util;
2+
3+
public class FactorialUtility {
4+
5+
/**
6+
* Utility method for creating validating the generetor passed
7+
*/
8+
public static void validateGeneretor( String generetor ) {
9+
// validate generetor string
10+
if ( !generetor.matches( "^[A-Za-z +\\-]+$" ) ) {
11+
throw new IllegalArgumentException( "Generator string contains invalid characters. Allowed: letters, space, +, -" );
12+
}
13+
int nFactors = generetor.trim().split("\\s+").length;
14+
if ( generetor.split( " " ).length != nFactors ) {
15+
throw new IllegalArgumentException( "Generator does not match the number of factors" );
16+
}
17+
}
18+
19+
}

0 commit comments

Comments
 (0)