adding unit test for rendering site and fixing LP export issue #45

Closed
SinusFox wants to merge 50 commits from adding-unit-tests-and-language-switching into main
3 changed files with 108 additions and 0 deletions
Showing only changes of commit 0806c8917d - Show all commits
+54
View File
@@ -0,0 +1,54 @@
import { NextApiRequest, NextApiResponse } from 'next';
import GLPK from 'glpk.js';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
const { objective, constraints } = req.body;
try {
const glpk = await GLPK();
const options = {
msglev: glpk.GLP_MSG_ALL,
presol: true,
cb: {
call: (progress: any) => console.log(progress),
each: 1
}
};
const problem = {
name: 'LP',
objective: {
direction: objective.direction === 'max' ? glpk.GLP_MAX : glpk.GLP_MIN,
name: 'obj',
vars: objective.vars
},
subjectTo: constraints.map((c: any) => ({
name: c.name,
vars: c.vars,
bnds: {
type: glpk.GLP_UP,
ub: c.bnds.ub,
lb: c.bnds.lb
}
}))
};
const result = glpk.solve(problem, options);
const lpFormat = `\n# Problem Definition\n
name: ${problem.name}\n
# Objective Function\n
${problem.objective.direction === glpk.GLP_MAX ? 'max' : 'min'} ${problem.objective.vars.map((v: any) => `${v.coef} ${v.name}`).join(' + ')}\n
# Constraints\n
${problem.subjectTo.map((c: any) => `${c.vars.map((v: any) => `${v.coef} ${v.name}`).join(' + ')} ${c.bnds.ub ? `<= ${c.bnds.ub}` : ''}`).join('\n')}\n`;
res.status(200).json({ result, lpFormat });
} catch (error) {
console.error('Error processing optimization:', error);
res.status(500).json({ message: 'Error processing optimization', error: error.message });
}
} else {
res.status(405).json({ message: 'Only POST method is allowed' });
}
}
+44
View File
@@ -0,0 +1,44 @@
import { NextApiRequest, NextApiResponse } from 'next';
import GLPK from 'glpk.js';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
const { objective, constraints, bounds } = req.body;
if (!objective || !constraints) {
return res.status(400).json({ message: 'Invalid input data. Ensure that "objective" and "constraints" are provided correctly.' });
}
try {
const glpk = await GLPK();
const options = { msglev: glpk.GLP_MSG_ALL, presol: true };
const problem = {
name: 'MIP',
objective: {
direction: objective.direction === 'max' ? glpk.GLP_MAX : glpk.GLP_MIN,
name: 'obj',
vars: objective.vars.map(v => ({ name: v.name, coef: v.coef }))
},
subjectTo: constraints.map(c => ({
name: c.name,
vars: c.vars.map(v => ({ name: v.name, coef: v.coef })),
bnds: {
type: glpk.GLP_UP,
lb: c.bnds.lb,
ub: c.bnds.ub
}
})),
binaries: objective.vars.map(v => v.name),
generals: objective.vars.map(v => v.name)
};
const result = glpk.solve(problem, options);
res.status(200).json({ result });
} catch (error) {
res.status(500).json({ message: 'Error processing optimization', error: error.message });
}
} else {
res.status(405).json({ message: 'Only POST method is allowed' });
}
}
+10
View File
@@ -0,0 +1,10 @@
import type { NextApiRequest, NextApiResponse } from 'next';
export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
res.status(200).json({ message: 'API is working!' });
} else {
res.status(405).json({ message: 'Method not allowed' });
}
}