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

Merged
SinusFox merged 56 commits from adding-unit-tests-and-language-switching into main 2024-10-11 12:48:16 +00:00
5 changed files with 172 additions and 64 deletions
Showing only changes of commit 8c39997a5a - Show all commits
+6 -45
View File
@@ -1,4 +1,5 @@
import { MouseEventHandler } from "react"; import { MouseEventHandler } from "react";
import React from 'react';
import Popup from "reactjs-popup"; import Popup from "reactjs-popup";
export function Box({title, placeholder, id}: export function Box({title, placeholder, id}:
@@ -35,53 +36,13 @@ export function Button({title, className, onClickFunc}:
); );
} }
export function Popup_Button({title, className}: export function Output({ id, text }: { id: string; text: string }) {
{title:string; className:string|undefined;}) { return (
return(
<Popup
trigger={<button
className={className}>
{title}
</button>}
position="right center"
modal
nested>
{close => (
<div className="popup_bg">
<button onClick={close}>
&times;
</button>
<div className="header"> {title} </div>
<div className="content">
This is a popup example.
</div>
<div className="actions">
<button className="button" onClick={close}>
Cancel</button>
</div>
</div>
)}
</Popup>
);
}
export function Output({id, text}:
{id:string; text:string}) {
return(
<div className="main_div"> <div className="main_div">
<div className="body_title" <div className="body_title">Output</div>
>
Output
</div>
<div className="text"> <div className="text">
<p <p className="output_box" id={id}>
className="output_box" {text}
id={id}
value={text}>
</p> </p>
</div> </div>
</div> </div>
+17 -8
View File
@@ -1,7 +1,7 @@
'use client' 'use client'
import { Box, Button, Output, Popup_Button } from "./modules.tsx"; import { Box, Button, Output } from "./modules";
import { calculate_click, import_click, export_click } from "./scripts.ts" import { calculate_click, downloadLP, import_click } from "./scripts"
export default function Home() { export default function Home() {
return ( return (
@@ -18,9 +18,17 @@ export default function Home() {
</div> </div>
</header> </header>
<Box <Box
title={"Functions"} title={"Objective"}
placeholder={"Your Functions here"} placeholder={"Objective"}
id="funcs"/> id="objective"/>
<Box
title={"Subject"}
placeholder={"Subject"}
id="subject"/>
<Box
title={"Bounds"}
placeholder={"Bounds"}
id="bounds"/>
<Box <Box
title={"Variables"} title={"Variables"}
placeholder={"Your Variables here"} placeholder={"Your Variables here"}
@@ -32,9 +40,10 @@ export default function Home() {
{/* <Popup_Button {/* <Popup_Button
title={"Import"} title={"Import"}
className={"button"} /> */} className={"button"} /> */}
<Popup_Button <Button
title={"Export"} title={"Export as LP"}
className={"button"} /> className={"button"}
onClickFunc={downloadLP} />
<br></br> <br></br>
<Output <Output
id="out" id="out"
+140 -11
View File
@@ -1,5 +1,5 @@
import * as MIP from "../pages/parseMIP.ts" import * as MIP from "../pages/parseMIP"
import * as LP from "../pages/parseLP.ts" import * as LP from "../pages/parseLP"
import * as LPAPI from "../pages/api/optimizeLP.js" import * as LPAPI from "../pages/api/optimizeLP.js"
import * as GLPKAPI from "../solver/glpk.min.js" import * as GLPKAPI from "../solver/glpk.min.js"
@@ -19,7 +19,10 @@ function customLog(message: string) {
} }
function customLogClear() { function customLogClear() {
document.getElementById('out').innerHTML = ""; const outElement = document.getElementById('out');
if (outElement) {
outElement.innerHTML = "";
}
} }
function walltimeStopAndPrint(startpoint: number) { function walltimeStopAndPrint(startpoint: number) {
@@ -43,15 +46,55 @@ function walltimeStart() {
return Date.now(); return Date.now();
} }
function isInputFilled(obj: string | undefined, subj: string | undefined, bounds: string | undefined, vars: string | undefined) {
if (obj == "" || obj == null || obj == undefined) {
customLog("Error: Empty input field.");
return false;
}
if (subj == "" || subj == null || subj == undefined) {
customLog("Error: Empty input field.");
return false;
}
if (bounds == "" || bounds == null || bounds == undefined) {
customLog("Error: Empty input field.");
return false;
}
if (vars == "" || vars == null || vars == undefined) {
customLog("Error: Empty input field.");
return false;
}
return true;
}
export function calculate_click() { export function calculate_click() {
customLogClear(); customLogClear();
const timer = walltimeStart(); const timer = walltimeStart();
customLog("Calculating...<br>"); customLog("Calculating...<br>");
let functions: string | undefined = document.getElementById('funcs').value; let objective: string | undefined;
let variables: string | undefined = document.getElementById('vars').value; const objectiveElement = document.getElementById('objective');
if (objectiveElement !== null) {
objective = (objectiveElement as HTMLInputElement).value;
}
let subject: string | undefined;
const subjectElement = document.getElementById('subject');
if (subjectElement !== null) {
subject = (subjectElement as HTMLInputElement).value;
}
let bounds: string | undefined;
const boundsElement = document.getElementById('bounds');
if (boundsElement !== null) {
bounds = (boundsElement as HTMLInputElement).value;
}
let variables: string | undefined;
const varsElement = document.getElementById('vars');
if (varsElement !== null) {
variables = (varsElement as HTMLInputElement).value;
}
if (functions == undefined || variables == undefined) return;
// let funcs:string[] = functions.split(/;/); // let funcs:string[] = functions.split(/;/);
// let vars:string[] = variables.split(/;/); // let vars:string[] = variables.split(/;/);
@@ -101,7 +144,17 @@ export function calculate_click() {
// console.log(parseFunction(decider)); // console.log(parseFunction(decider));
let wholeText: string = functions + "\nGenerals \n" + variables + "End";
// catch error: empty input field(s)
if (!isInputFilled(objective, subject, bounds, variables)) return;
let wholeText: string = "Maximize\n obj: " + objective
+ "\nSubject To \n" + subject
+ "\nBounds \n" + bounds
+ "\nGenerals \n" + variables
+ "\nEnd";
// customLog("<br><br>DEBUGGING<br><br>\nfunctions:<br>" + functions + "<br><br>variables:<br>" + variables + "<br><br>DEBUGGING END<br>");
customLog("Running optimization with input: \"" + wholeText + "\"<br>"); customLog("Running optimization with input: \"" + wholeText + "\"<br>");
run(wholeText); run(wholeText);
@@ -139,13 +192,89 @@ function run(text: string) {
customLog("Dual values of constraints:"); customLog("Dual values of constraints:");
for (var j = 1; j <= GLPKAPI.glp_get_num_rows(lp); j++) { for (var j = 1; j <= GLPKAPI.glp_get_num_rows(lp); j++) {
const dualValue = GLPKAPI.glp_get_row_dual(lp, j); // Abrufen des dualen Wertes const dualValue = GLPKAPI.glp_get_row_dual(lp, j); // fetch dual
const constraintName = GLPKAPI.glp_get_row_name(lp, j); const constraintName = GLPKAPI.glp_get_row_name(lp, j);
customLog(constraintName + " dual = " + dualValue); customLog(constraintName + " dual = " + dualValue);
} }
customLog(""); customLog("");
} }
function downloadLPFormatting(objective: string, subject: any, bounds: any) {
customLog("Preparing file content string...<br>");
// ensure that all vars are strings
const formattedObjective = typeof objective === 'string' ? objective : '';
const formattedSubject = typeof subject === 'string' ? subject : '';
const formattedBounds = typeof bounds === 'string' ? bounds : '';
// Header mit Problemname
const header = "\\ Your problem\n";
// format objective
const objectiveFunction = `Maximize\n obj: ${formattedObjective}\n`;
// turn each subject into a single line
const constraints = `Subject To\n${formattedSubject.split("\n").filter(line => line.trim() !== "").map(line => ` ${line}`).join("\n")}\n`;
// format bounds
const boundsFormatted = `Bounds\n${formattedBounds.split("\n").filter(line => line.trim() !== "").map(line => ` ${line}`).join("\n")}\n`;
// summarizing everything into one var
const lpFormat = `${header}${objectiveFunction}${constraints}${boundsFormatted}End\n`;
return lpFormat;
}
function downloadProblemDownload(content: string) {
customLog("Preparing file...<br>")
const blob = new Blob([content], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'problem.txt'; // file name
link.click(); // starting download
customLog("Starting download.")
}
export function downloadLP() {
customLogClear();
customLog("Preparing download...<br>");
customLog("Fetching input...<br>")
let objective: string | undefined;
const objectiveElement = document.getElementById('objective');
if (objectiveElement !== null) {
objective = (objectiveElement as HTMLInputElement).value;
}
let subject: string | undefined;
const subjectElement = document.getElementById('subject');
if (subjectElement !== null) {
subject = (subjectElement as HTMLInputElement).value;
}
let bounds: string | undefined;
const boundsElement = document.getElementById('bounds');
if (boundsElement !== null) {
bounds = (boundsElement as HTMLInputElement).value;
}
let variables: string | undefined;
const varsElement = document.getElementById('vars');
if (varsElement !== null) {
variables = (varsElement as HTMLInputElement).value;
}
// catch error: empty input field(s)
if (!isInputFilled(objective, subject, bounds, variables)) return;
const exportString: string = downloadLPFormatting(objective, subject, bounds);
downloadProblemDownload(exportString);
}
// Irgend ein Interface // Irgend ein Interface
// document.getElementById('out').innerHTML = funcs; // document.getElementById('out').innerHTML = funcs;
@@ -161,10 +290,10 @@ export function import_click() {
} }
export function export_click() { // export function export_click() {
console.log("Exporting..."); // console.log("Exporting...");
} // }
// function parseFunction(toParse: string) { // function parseFunction(toParse: string) {
// var regex = toParse.match(/([a-zA-Z][a-zA-Z0-9]*):/); // var regex = toParse.match(/([a-zA-Z][a-zA-Z0-9]*):/);
+4
View File
@@ -33,4 +33,8 @@ function createProblemLP(
}; };
return problem; return problem;
}
export function parseLP(input: string) {
console.log("Parsing LP file:", input);
} }
+5
View File
@@ -74,4 +74,9 @@ function createProblemMIP(
}; };
return problem; return problem;
}
export function parseLP(input: string) {
console.log("Parsing MIP file:", input);
} }