Skip to content

Commit

Permalink
fix(SNSBD-2): fix logs formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pjazdzyk committed Jul 25, 2023
1 parent c4de317 commit 268ff32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
24 changes: 16 additions & 8 deletions src/main/java/com/synerset/brentsolver/BrentSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class BrentSolver {
// DIAGNOSTICS OUTPUT CONTROL
private boolean showDiagnostics = false;

private static final Logger LOGGER = LoggerFactory.getLogger(BrentSolver.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BrentSolver.class.getSimpleName());

/**
* Initializes solver instance with function output set as 0, with default name.
Expand Down Expand Up @@ -101,28 +101,35 @@ public BrentSolver(String id, DoubleFunction<Double> userFunction, int evalX2Div
* @return actual root value
*/
public final double findRoot() {
LOGGER.info("-----[{}] CALCULATION IN PROGRESS-----", id);
// To check and set value b as being closer to the root
checkSetAndSwapABPoints(a0, b0);
// In case provided by user point "a" or "b" is actually a root
if (Math.abs(f_b) < accuracy)
if (Math.abs(f_b) < accuracy) {
LOGGER.info("CALCULATION COMPLETE. INITIAL VALUE IS A ROOT");
return b;
}
// If solver were stopped
if (!runFlag)
if (!runFlag) {
LOGGER.info("!!CALCULATION STOPPED!!");
return b;
}
// Checking if Brent AB condition is not met to launch automatic AB points evaluation procedure
if (initialABConditionIsNotMet())
if (initialABConditionIsNotMet()) {
evaluateValidCondition();
}
// In case evaluation procedure will output b as a root
if (Math.abs(f_b) < accuracy)
if (Math.abs(f_b) < accuracy) {
return b;
}
// If at this stage proper A-B condition is not achievable - an exception is thrown.
if (initialABConditionIsNotMet()) {
String errorMsg = id + ": EVALUATION PROCEDURE FAILED: f(a) i f(b) must have an opposite signs. Current values:"
+ String.format(" a = %.3f, b = %.3f, f(a)= %.3f, f(b)=%.3f", a, b, f_a, f_b);
LOGGER.error(errorMsg);
throw new BrentSolverException(errorMsg);
}
printSolverDiagnostics("\n" + id + ": BEFORE RUN:\n", "\n");
printSolverDiagnostics(id + ": INITIAL VALUES: ", "");

/*--------BEGINNING OF ITERATIVE LOOP--------*/
while (runFlag) {
Expand Down Expand Up @@ -178,6 +185,7 @@ public final double findRoot() {
}

// b is always closer to the root
LOGGER.info("-----[{}] CALCULATION FINISHED-----", id);
return b;
}

Expand Down Expand Up @@ -391,13 +399,13 @@ public static double secantMethod(double x2, double x1, double f_x2, double f_x1
// DIAGNOSTIC OUTPUT
private void printEvaluationDiagnostics(String titleMsg) {
if (showDiagnostics) {
LOGGER.info(String.format("\n" + titleMsg + " \t EVAL VALUES:" + String.format("a = %.3f, b = %.3f, f(a)= %.3f, f(b)=%.3f", a, b, f_a, f_b)));
LOGGER.info(String.format(titleMsg + " \t EVAL VALUES:" + String.format("a = %.3f, b = %.3f, f(a)= %.3f, f(b)=%.3f", a, b, f_a, f_b)));
}
}

private void printSolverDiagnostics(String titleMsg, String endMsg) {
if (showDiagnostics) {
LOGGER.info(String.format(titleMsg + "s= %.5f, a= %.5f, f(a)= %.5f, b= %.5f, f(b)= %.5f, c= %.5f, f(c)= %.5f \t\t\t" + endMsg + "%n", s, a, f_a, b, f_b, c, f_c));
LOGGER.info(String.format(titleMsg + "s= %.5f, a= %.5f, f(a)= %.5f, b= %.5f, f(b)= %.5f, c= %.5f, f(c)= %.5f \t" + endMsg, s, a, f_a, b, f_b, c, f_c));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
public class BrentSolverException extends RuntimeException {

public BrentSolverException(String msg) {

super(msg);

}

}
}
5 changes: 3 additions & 2 deletions src/test/java/com/synerset/brentsolver/BrentSolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class BrentSolverTest {

@BeforeEach
void setUp() {
this.solver = new BrentSolver("Test-SOLVER");
this.solver_accuracy = solver.getAccuracy();
solver = new BrentSolver("Test-SOLVER");
solver_accuracy = solver.getAccuracy();
solver.setShowDiagnostics(true);
}

@Test
Expand Down

0 comments on commit 268ff32

Please sign in to comment.