src/controller/solving.js
- import { Model, init, solvers } from "../../assets/vendor/minizinc.mjs";
- export async function initSolvers() {
- await init();
- return solvers();
- }
- export async function solveModel(modelName, modelString, solverId) {
- const model = new Model();
- model.addFile(modelName, modelString);
- const solve = model.solve({
- options: { solver: solverId, statistics: true },
- });
- if (!solve) {
- throw new Error(`Solver '${solverId}' failed to initialize.`);
- }
- let result;
- try {
- result = await solve;
- } catch (e) {
- console.error("Solver error:", e, JSON.stringify(e));
- const detail =
- [e?.message, e?.stderr, e?.output].find(Boolean) ?? JSON.stringify(e);
- throw new Error(
- `Solver failed (code ${e?.code}, type ${e?.type}): ${detail}`,
- { cause: e },
- );
- }
- if (!["SATISFIED", "OPTIMAL_SOLUTION"].includes(result.status)) {
- console.info(`Solver status: ${result.status}`, result);
- }
- const { solution, statistics, status } = result;
- return { solution, statistics, status };
- }