The Optimization API
The optimization API exposes low-level functionalities to construct and solve optimization problems. Learn how to create an optimization problem.
variable
A variable represents a variable in an optimization problem, i.e. a number that the optimizer can change during optimization. variable takes a val: number as its initial value.
problem
problem takes a description of an optimization problem defined as a list of constraints and/or an objective function. After setting up a problem, call start() before running the optimizer. start() returns:
- A
runfunction with an optionaluntilstopping condition. Ifuntilis not provided, the optimizer will run until it converges on a solution. - A
convergedboolean flag for checking if the optimizer converged.
Examples:
Minimize such that , with an initial condition of .
ts
import { variable, pow, sub, problem } from "@penrose/core";
const x = variable(10);
const problem = await problem({ constraints: [pow(sub(x, 5), 2)] });
const { vals } = problem.start({}).run({});
console.log(problem.converged); // true
console.log(x); // a value closer to 5All the arithmetic functions
INFO
Coming soon!