Skip to content

Commit e2c03f4

Browse files
full factorial impl
1 parent 4d02d18 commit e2c03f4

7 files changed

Lines changed: 173 additions & 17 deletions

File tree

pom.xml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@
2121
<version>3.8.1</version>
2222
<scope>test</scope>
2323
</dependency>
24+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
25+
<dependency>
26+
<groupId>org.apache.commons</groupId>
27+
<artifactId>commons-math3</artifactId>
28+
<version>3.6.1</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.logging.log4j</groupId>
32+
<artifactId>log4j-core</artifactId>
33+
<version>2.20.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.logging.log4j</groupId>
37+
<artifactId>log4j-api</artifactId>
38+
<version>2.20.0</version>
39+
</dependency>
2440
</dependencies>
2541

2642
<build>
@@ -32,11 +48,30 @@
3248
<configuration>
3349
<archive>
3450
<manifest>
35-
<mainClass>com.jdoe.App</mainClass>
51+
<mainClass>com.jdoe.Testing</mainClass>
3652
</manifest>
3753
</archive>
3854
</configuration>
3955
</plugin>
56+
<!-- for fat jar's-->
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-shade-plugin</artifactId>
60+
<version>3.6.1</version>
61+
<executions>
62+
<execution>
63+
<phase>package</phase>
64+
<goals>
65+
<goal>shade</goal>
66+
</goals>
67+
<configuration>
68+
<transformers>
69+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
70+
</transformers>
71+
</configuration>
72+
</execution>
73+
</executions>
74+
</plugin>
4075
</plugins>
4176
</build>
4277
</project>

src/main/java/com/jdoe/App.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jdoe;
2+
3+
public class DesignFactory {
4+
5+
public static void fullFactorialFactory() {
6+
/*
7+
will return an instance of the fullfactorial algo object to make its methods accessible
8+
methods can be static but that is for topic for later
9+
*/
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.jdoe;
2+
3+
import com.jdoe.algorithms.FactorialDOE;
4+
5+
/**
6+
* Hello world!
7+
*
8+
*/
9+
public class Testing
10+
{
11+
//! this main class is used for testing purposes only
12+
public static void main( String[] args )
13+
{
14+
int[] arrayOfLevels = {2,3,6};
15+
FactorialDOE.FullFactorial(arrayOfLevels);
16+
}
17+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

src/main/resources/log4j2.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="DEBUG">
10+
<AppenderRef ref="Console"/>
11+
</Root>
12+
</Loggers>
13+
</Configuration>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
/**
88
* Unit test for simple App.
99
*/
10-
public class AppTest
10+
public class TestingTest
1111
extends TestCase
1212
{
1313
/**
1414
* Create the test case
1515
*
1616
* @param testName name of the test case
1717
*/
18-
public AppTest( String testName )
18+
public TestingTest(String testName )
1919
{
2020
super( testName );
2121
}
@@ -25,7 +25,7 @@ public AppTest( String testName )
2525
*/
2626
public static Test suite()
2727
{
28-
return new TestSuite( AppTest.class );
28+
return new TestSuite( TestingTest.class );
2929
}
3030

3131
/**

0 commit comments

Comments
 (0)