From 72e610f814d3658f52e906924a854cdf8eec3b57 Mon Sep 17 00:00:00 2001 From: SinusFox <61253950+SinusFox@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:29:19 +0200 Subject: [PATCH] fixing further errors --- src/app/scripts.ts | 2 +- src/pages/api/optimizeLP.ts | 12 ++++++--- src/pages/api/optimizeMIP.ts | 48 ++++++++++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/app/scripts.ts b/src/app/scripts.ts index 8f30b86..899be06 100644 --- a/src/app/scripts.ts +++ b/src/app/scripts.ts @@ -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...
"); // ensure that all vars are strings diff --git a/src/pages/api/optimizeLP.ts b/src/pages/api/optimizeLP.ts index 330ba08..d14af69 100644 --- a/src/pages/api/optimizeLP.ts +++ b/src/pages/api/optimizeLP.ts @@ -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' }); diff --git a/src/pages/api/optimizeMIP.ts b/src/pages/api/optimizeMIP.ts index 3ff00b4..38b6da6 100644 --- a/src/pages/api/optimizeMIP.ts +++ b/src/pages/api/optimizeMIP.ts @@ -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' });