Skip to content

Commit

Permalink
modified: CeresHelper.js
Browse files Browse the repository at this point in the history
	modified:   dist/ceres.js
	new file:   docs/images/logo.jpeg
	modified:   docs/index.html
	modified:   docs/js/prism.js
	modified:   docs/themes/prism.css
  • Loading branch information
miner committed Jan 16, 2024
1 parent f9bbeba commit b515d30
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 99 deletions.
46 changes: 29 additions & 17 deletions CeresHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,23 @@ export class Ceres {
throw new Error(`Invalid mathematical expression: ${mathExpression}`);
}

mathExpression = mathExpression.replace('^', '**');
mathExpression = mathExpression.replaceAll('^', '**');

// Translation to Javascript functions
mathExpression = mathExpression
.replace(/sin/g, 'Math.sin')
.replace(/cos/g, 'Math.cos')
.replace(/tan/g, 'Math.tan')
.replace(/cot/g, 'Math.cot')
.replace(/sec/g, 'Math.sec')
.replace(/csc/g, 'Math.csc')
.replace(/log/g, 'Math.log10')
.replace(/ln/g, 'Math.log')
.replace(/sqrt/g, 'Math.sqrt')
.replace(/exp/g, 'Math.exp')
.replace(/abs/g, 'Math.abs')
.replace(/Pi/g, 'Math.PI')
.replace(/E/g, 'Math.E');
.replaceAll(/sin/g, 'Math.sin')
.replaceAll(/cos/g, 'Math.cos')
.replaceAll(/tan/g, 'Math.tan')
.replaceAll(/cot/g, 'Math.cot')
.replaceAll(/sec/g, 'Math.sec')
.replaceAll(/csc/g, 'Math.csc')
.replaceAll(/log/g, 'Math.log10')
.replaceAll(/ln/g, 'Math.log')
.replaceAll(/sqrt/g, 'Math.sqrt')
.replaceAll(/exp/g, 'Math.exp')
.replaceAll(/abs/g, 'Math.abs')
.replaceAll(/Pi/g, 'Math.PI')
.replaceAll(/E/g, 'Math.E');

return mathExpression;
}
Expand All @@ -167,7 +167,7 @@ export class Ceres {

// sanitize the input to prevent injection attacks
jsonFunctions = jsonFunctions.map(Ceres.sanitizeInput);
//console.log(jsonSystem.functions)
//console.log(jsonFunctions)

jsonFunctions.forEach(jsonFunction => this.addFunction(new Function('x', `return ${jsonFunction}`)));

Expand All @@ -180,16 +180,28 @@ export class Ceres {
this.addUpperbound(index, variable.upperbound);
}
});

return jsonFunctions
}

generateInitialGuess(variablesMapping) {
return Object.keys(variablesMapping).map(varName => variablesMapping[varName].guess);
}

async run(jsonSystem, max_numb_iterations = 2000, parameter_tolerance = 1e-10, function_tolerance = 1e-16, gradient_tolerance = 1e-16, max_solver_time_in_seconds = 100, initial_trust_region_radius = 1e4, max_trust_region_radius = 1e16, max_num_consecutive_invalid_steps = 5) {
this.setSystemFromJson(jsonSystem);
async run(jsonSystem) {
function isNumber(n) { return !isNaN(parseFloat(n)) && !isNaN(n - 0) }
let jsonFunctions = this.setSystemFromJson(jsonSystem);
let initial_guess = this.generateInitialGuess(jsonSystem.variables);
let max_numb_iterations = (isNumber(jsonSystem.max_numb_iterations)) ? jsonSystem.max_numb_iterations : 2000;
let parameter_tolerance = (isNumber(jsonSystem.parameter_tolerance)) ? jsonSystem.parameter_tolerance : 1e-10;
let function_tolerance = (isNumber(jsonSystem.function_tolerance)) ? jsonSystem.function_tolerance : 1e-16;
let gradient_tolerance = (isNumber(jsonSystem.gradient_tolerance)) ? jsonSystem.gradient_tolerance : 1e-16;
let max_solver_time_in_seconds = (isNumber(jsonSystem.max_solver_time_in_seconds)) ? jsonSystem.max_solver_time_in_seconds : 100;
let initial_trust_region_radius = (isNumber(jsonSystem.initial_trust_region_radius)) ? jsonSystem.initial_trust_region_radius : 1e4;
let max_trust_region_radius = (isNumber(jsonSystem.max_trust_region_radius)) ? jsonSystem.max_trust_region_radius : 1e16;
let max_num_consecutive_invalid_steps = (isNumber(jsonSystem.max_num_consecutive_invalid_steps)) ? jsonSystem.max_num_consecutive_invalid_steps : 5;
const results = await this.solve(initial_guess, max_numb_iterations, parameter_tolerance, function_tolerance, gradient_tolerance, max_solver_time_in_seconds, initial_trust_region_radius, max_trust_region_radius, max_num_consecutive_invalid_steps);
results.report = jsonFunctions.map(i => 'Function: ' + i + ' = 0\n')+"\n\n"+results.report
return results;
}
}
Expand Down
46 changes: 29 additions & 17 deletions dist/ceres.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added docs/images/logo.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 35 additions & 63 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Ceres.js</title>
<link rel="icon" type="image/x-icon" href="./images/logo.jpeg">
<style>
body {
font-family: Arial, sans-serif; color: #333; margin: 0; padding: 0;}
Expand All @@ -28,7 +29,7 @@
</head>
<body>
<header>
<img src="images/logo.png" alt="Ceres.js Logo"> <!--Link to your logo-->
<img src="images/logo.jpeg" alt="Ceres.js Logo"> <!--Link to your logo-->
<h1>Ceres.js</h1>
<h2>Numerical Optimization Library in JavaScript</h2>
</header>
Expand All @@ -55,41 +56,9 @@ <h2>About</h2>
<h2>Examples</h2>
<h3>User Defined Function</h3>
<p>This is an example of the solution of the user defined function using Ceres.js</p>
<pre><code class="language-js">
async function ceresSolveArrowFunction() {
const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.min.js');

let f = function (x, a, b, c){
return a*x[0]*x[0] + b*x[0] + c
}

let solver = new Ceres()

let a = -1
let b = 0
let c = 9
solver.addFunction((x) => f(x, a, b, c)) //Add the first equation to the solver.
var x_guess = [1] //Guess the initial values of the solution.
let s = await solver.solve(x_guess) //Solve the equation
var x = s.x //assign the calculated solution array to the variable x
document.getElementById("arrow_demo").value = s.report + "\n\n" //Print solver report
solver.reset() // erases the loaded functions and resets the solver.

a = -2
b = 1
c = 9
solver.addFunction((x) => f(x, a, b, c)) //Add the first equation to the solver.
x_guess = [1] //Guess the initial values of the solution.
s = await solver.solve(x_guess) //Solve the equation
x = s.x //assign the calculated solution array to the variable x
document.getElementById("arrow_demo").value += s.report //Print solver report

solver.remove() //required to free the memory in C++
}
ceresSolveArrowFunction()
</code></pre>
<pre><code id="arrow_demo_code" class="language-js"></code></pre>
<textarea id="arrow_demo" rows="40" cols="110"></textarea>
<script>
<script id="arrow_demo_code_source">
async function ceresSolveArrowFunction() {
const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.min.js');

Expand Down Expand Up @@ -122,12 +91,14 @@ <h3>User Defined Function</h3>
}
ceresSolveArrowFunction()
</script>
<script>document.getElementById("arrow_demo_code").innerHTML = document.getElementById("arrow_demo_code_source").innerHTML</script>

<h3>JSON Syntax Demo Function</h3>
<p>This is an example using the JSON syntax with Ceres.js</p>
<pre><code id="json_demo_code" class="language-js"></code></pre>
<div><button type="button" onclick="ceresSolveJSON()">Solve</button> </div>
<textarea id="json_demo" rows="40" cols="110"></textarea>
<script>
<script id="json_demo_code_source">

let jsonSystem = {
"variables": {
Expand All @@ -154,11 +125,13 @@ <h3>JSON Syntax Demo Function</h3>
}

</script>
<script>document.getElementById("json_demo_code").innerHTML = document.getElementById("json_demo_code_source").innerHTML</script>

<h3>Powell Function</h3>
<p>This is an example of the solution of the powell function using Ceres.js</p>
<pre><code id="powell_demo_code" class="language-js"></code></pre>
<textarea id="powell_demo" rows="40" cols="110"></textarea>
<script>
<script id="powell_demo_code_source">
async function ceresSolvePowell() {
const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.min.js');

Expand Down Expand Up @@ -196,10 +169,11 @@ <h3>Powell Function</h3>
}
ceresSolvePowell();
</script>
<script>document.getElementById("powell_demo_code").innerHTML = document.getElementById("powell_demo_code_source").innerHTML</script>

<h3>Quadratic Function</h3>
<p>This is an example of the solution of the quadratic function using <a href="https://github.com/Pterodactylus/ceres.js">Ceres.js</a></p>

<pre><code id="quadratic_demo_code" class="language-js"></code></pre>
<div id="calculator" style="width: 600px; height: 400px;"></div>
<script>
var elt = document.getElementById('calculator');
Expand All @@ -208,7 +182,7 @@ <h3>Quadratic Function</h3>
calculator.setExpression({ id: 'exp2', latex: "\\sqrt{5}*x-y^2 = 0" });
</script>
<p><textarea id="quadratic_demo" rows="40" cols="110"></textarea></p>
<script>
<script id="quadratic_demo_code_source">
async function ceresSolveQuadratic() {
const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.min.js');

Expand Down Expand Up @@ -237,41 +211,39 @@ <h3>Quadratic Function</h3>
}
ceresSolveQuadratic()
</script>
<script>document.getElementById("quadratic_demo_code").innerHTML = document.getElementById("quadratic_demo_code_source").innerHTML</script>

<h3>Rosebrock Function</h3>
<p>This is an example of the solution of the Rosenbrock function using Ceres.js</p>
<pre><code id="rosebrock_demo_code" class="language-js"></code></pre>
<textarea id="rosebrock_demo" rows="40" cols="110"></textarea>
<script>
<script id="rosebrock_demo_code_source">
async function ceresSolveRosebrock() {
const {Ceres} = await import('https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.min.js');

var fn1 = function (x){
return (1.0 - x[0]) * (1.0 - x[0]) + 100.0 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]);
}
const {Ceres} = await import('../dist/ceres.js');

var c1 = function(x){
//console.log("Min Loop xxx")
//console.log(x+" ")
return null
let rosebrockSystem = {
"variables": {
"x": {
"guess": -2,
},
"y": {
"guess": 1.0,
},
},
"functions": [
"(1-x)^2 + 100*(y-x^2)^2", //The solution is x=1, y=1
],
"max_numb_iterations": 10000, //Increase the default number of iterations
"parameter_tolerance": 0.0000001, //In this case we want Ceres to act as a function minimizer.
}

let solver = new Ceres()
solver.addFunction(fn1) //Add the first equation to the solver.
/solver.addCallback(c1) //Add the callback to the solver.
//solver.addLowerbound(0,1.6) //Add a lower bound to the x[0] variable
//solver.addUpperbound(1,1.7) //Add a upper bound to the x[1] variable


var x_guess = [-2, 1.0] //Guess the initial values of the solution.
let s = await solver.solve(x_guess) //Solve the equation
console.log(s)
//var x = s.x //assign the calculated solution array to the variable x
document.getElementById("rosebrock_demo").value = s.report //Print solver report
solver.remove() //required to free the memory in C++

let results = await solver.run(rosebrockSystem);
document.getElementById("rosebrock_demo").value = results.report //Print solver report
}
ceresSolveRosebrock()
</script>
<script>document.getElementById("rosebrock_demo_code").innerHTML = document.getElementById("rosebrock_demo_code_source").innerHTML</script>
</section>

<section id="docs" class="container">
Expand Down
Loading

0 comments on commit b515d30

Please sign in to comment.