Design fixes #50
+1
-1
@@ -199,7 +199,7 @@ function run(text: string) {
|
||||
customLog("");
|
||||
}
|
||||
|
||||
function downloadLPFormatting(objective: string, subject: any, bounds: any) {
|
||||
function downloadLPFormatting(objective: any, subject: any, bounds: any) {
|
||||
customLog("Preparing file content string...<br>");
|
||||
|
||||
// ensure that all vars are strings
|
||||
|
||||
@@ -44,9 +44,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
${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 });
|
||||
} catch (error: unknown) {
|
||||
// Type Assertion to access properties of the error object
|
||||
if (error instanceof Error) {
|
||||
console.error('Error processing optimization:', error);
|
||||
res.status(500).json({ message: 'Error processing optimization', error: error.message });
|
||||
} else {
|
||||
console.error('Unknown error:', error);
|
||||
res.status(500).json({ message: 'Error processing optimization', error: 'Unknown error' });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res.status(405).json({ message: 'Only POST method is allowed' });
|
||||
|
||||
@@ -1,9 +1,34 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import GLPK from 'glpk.js';
|
||||
|
||||
interface Variable {
|
||||
name: string;
|
||||
coef: number;
|
||||
}
|
||||
|
||||
interface Constraint {
|
||||
name: string;
|
||||
vars: Variable[];
|
||||
bnds: {
|
||||
lb: number;
|
||||
ub: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface Objective {
|
||||
direction: 'max' | 'min';
|
||||
vars: Variable[];
|
||||
}
|
||||
|
||||
interface RequestBody {
|
||||
objective: Objective;
|
||||
constraints: Constraint[];
|
||||
bounds?: any;
|
||||
}
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === 'POST') {
|
||||
const { objective, constraints, bounds } = req.body;
|
||||
const { objective, constraints, bounds }: RequestBody = req.body;
|
||||
|
||||
if (!objective || !constraints) {
|
||||
return res.status(400).json({ message: 'Invalid input data. Ensure that "objective" and "constraints" are provided correctly.' });
|
||||
@@ -18,25 +43,32 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
objective: {
|
||||
direction: objective.direction === 'max' ? glpk.GLP_MAX : glpk.GLP_MIN,
|
||||
name: 'obj',
|
||||
vars: objective.vars.map(v => ({ name: v.name, coef: v.coef }))
|
||||
vars: objective.vars.map((v: Variable) => ({ name: v.name, coef: v.coef }))
|
||||
},
|
||||
subjectTo: constraints.map(c => ({
|
||||
subjectTo: constraints.map((c: Constraint) => ({
|
||||
name: c.name,
|
||||
vars: c.vars.map(v => ({ name: v.name, coef: v.coef })),
|
||||
vars: c.vars.map((v: Variable) => ({ 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)
|
||||
binaries: objective.vars.map((v: Variable) => v.name),
|
||||
generals: objective.vars.map((v: Variable) => 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 });
|
||||
} catch (error: unknown) {
|
||||
// Type Assertion to access properties of the error object
|
||||
if (error instanceof Error) {
|
||||
console.error('Error processing optimization:', error);
|
||||
res.status(500).json({ message: 'Error processing optimization', error: error.message });
|
||||
} else {
|
||||
console.error('Unknown error:', error);
|
||||
res.status(500).json({ message: 'Error processing optimization', error: 'Unknown error' });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res.status(405).json({ message: 'Only POST method is allowed' });
|
||||
|
||||
Reference in New Issue
Block a user