|
| 1 | +package com.rae.formicapi.math; |
| 2 | + |
| 3 | +import com.rae.formicapi.FormicAPI; |
| 4 | + |
| 5 | +import java.util.function.Function; |
| 6 | + |
| 7 | +import static java.lang.Math.abs; |
| 8 | + |
| 9 | +public class Solvers { |
| 10 | + /** |
| 11 | + * |
| 12 | + * @param function the equation you want to solve |
| 13 | + * @param a first boundaries |
| 14 | + * @param b second boundaries |
| 15 | + * @param epsilon tolerance |
| 16 | + * @return the Solution if there is one or 0. |
| 17 | + */ |
| 18 | + public static float dichotomy(Function<Float, Float> function, float a, float b, float epsilon) { |
| 19 | + try { |
| 20 | + if (function.apply(a) * function.apply(b) > 0) { //Verification of the boundaries |
| 21 | + throw new RuntimeException("Wrong boundaries in dichotomy solver : a=" + a + "f(a)=" + function.apply(a) + "| b=" + b + " f(b)=" + function.apply(b)); |
| 22 | + } else { |
| 23 | + float m = (float) ((a + b) / 2.); |
| 24 | + while (abs(a - b) > epsilon) { |
| 25 | + if (function.apply(m) == 0.0) { |
| 26 | + return m; |
| 27 | + } else if (function.apply(a) * function.apply(m) > 0) { |
| 28 | + a = m; |
| 29 | + } else { |
| 30 | + b = m; |
| 31 | + } |
| 32 | + m = (a + b) / 2; |
| 33 | + } |
| 34 | + return m; |
| 35 | + } |
| 36 | + } catch (RuntimeException e) { |
| 37 | + FormicAPI.LOGGER.error(e.toString()); |
| 38 | + return 0; |
| 39 | + } |
| 40 | + } |
| 41 | + //TODO complete a naive approach first |
| 42 | + |
| 43 | + /** |
| 44 | + * Uses gradient descent to find the minimum of a given function, with adaptive step size. |
| 45 | + * (generated in part with chatgpt) |
| 46 | + * @param function a function that has a minimum |
| 47 | + * @param start starting point |
| 48 | + * @param step initial learning rate |
| 49 | + * @param dx small delta used to estimate the derivative |
| 50 | + * @return the estimated x value at which the function has a minimum, return NaN if there is no solution found |
| 51 | + */ |
| 52 | + public static float gradientDecent(Function<Float, Float> function, float start, float step, float dx) { |
| 53 | + float x = start; |
| 54 | + float learningRate = step; |
| 55 | + int maxIterations = 10000; |
| 56 | + float tolerance = 1e-6f; |
| 57 | + float decay = 0.9f; // How fast the step shrinks when progress slows |
| 58 | + float minStep = 1e-6f; |
| 59 | + |
| 60 | + for (int i = 0; i < maxIterations; i++) { |
| 61 | + float derivative = (function.apply(x + dx) - function.apply(x - dx)) / (2 * dx); |
| 62 | + float newX = x - learningRate * derivative; |
| 63 | + |
| 64 | + float currentValue = function.apply(x); |
| 65 | + float newValue = function.apply(newX); |
| 66 | + |
| 67 | + if (newValue < currentValue) { |
| 68 | + // Improvement, keep going |
| 69 | + x = newX; |
| 70 | + // Optionally increase the step a little |
| 71 | + learningRate *= 1.05f; |
| 72 | + } else { |
| 73 | + // No improvement, reduce step size |
| 74 | + learningRate *= decay; |
| 75 | + if (learningRate < minStep) { |
| 76 | + return x; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (Math.abs(derivative) < tolerance) { |
| 81 | + return x; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return Float.NaN; |
| 86 | + } |
| 87 | +} |
0 commit comments