Mathematical expression solver with Genetic Algorithm (GA)
Testrun
JavaScript ES6
// Import the MathExpressionSolver; // Initialize our valuesconst THE_POPULATION_SIZE = 1000;const THE_OBJECTIVE_FUNCTION = 9261; // the result of the formulaconst THE_TOLERANCE = 05;const THE_CHANCE_OF_MUTATION = 50;const THE_ITERATIONS = 1000;var THE_EXPRESSION = 'x^2+y^2+z^2'; // the formula to solve: looking for x, y, z // Create the GA objectvar mes = THE_EXPRESSION THE_OBJECTIVE_FUNCTION THE_POPULATION_SIZE THE_CHANCE_OF_MUTATION; console; // Evolve the solutionvar theBest result;for var i = 0; i < THE_ITERATIONS; i++ mes; theBest = mes; result = mes; if result >= THE_OBJECTIVE_FUNCTION - THE_TOLERANCE && result <= THE_OBJECTIVE_FUNCTION + THE_TOLERANCE break; // Print the resultconsole;console;console;console;console;console;console;console;