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
13 changed files with 5036 additions and 124 deletions
+26
View File
@@ -0,0 +1,26 @@
name: Run Tests
on:
push:
branches: ["main"]
workflow_dispatch:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test:ci
+2
View File
@@ -890,3 +890,5 @@ next-env.d.ts
# Writerside # Writerside
/Writerside /Writerside
package-lock.json
+21
View File
@@ -0,0 +1,21 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import Home from "../src/app/page";
jest.mock('../src/app/scripts', () => ({
customLog: jest.fn(),
customLogClear: jest.fn(),
}));
jest.mock('../src/solver/glpk.min.js', () => ({
LPF_ECOND: 2,
}));
test('render home page', () => {
// render website
render(<Home />);
// check if text is in document
const headingElement = screen.getByText(/OR-Tool/i); // text search in document
expect(headingElement).toBeInTheDocument();
});
+15
View File
@@ -0,0 +1,15 @@
const nextJest = require('next/jest');
const createJestConfig = nextJest({
dir: './',
});
const customJestConfig = {
testEnvironment: 'jest-environment-jsdom',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
};
module.exports = createJestConfig(customJestConfig);
+1
View File
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
+4562 -27
View File
File diff suppressed because it is too large Load Diff
+13 -1
View File
@@ -6,22 +6,34 @@
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"lint": "next lint" "lint": "next lint",
"test": "jest --watchAll",
"test:ci": "jest --ci --coverage"
}, },
"dependencies": { "dependencies": {
"glpk.js": "^4.0.2", "glpk.js": "^4.0.2",
"i18n": "^0.15.1",
"i18next": "^23.15.2",
"i18next-browser-languagedetector": "^8.0.0",
"jest-environment-jsdom": "^29.7.0",
"next": "14.2.11", "next": "14.2.11",
"next-i18next": "^15.3.1",
"react": "^18", "react": "^18",
"react-dom": "^18", "react-dom": "^18",
"react-i18next": "^15.0.2",
"react-popup": "^0.11.2", "react-popup": "^0.11.2",
"reactjs-popup": "^2.0.6" "reactjs-popup": "^2.0.6"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^20", "@types/node": "^20",
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18", "@types/react-dom": "^18",
"eslint": "^8", "eslint": "^8",
"eslint-config-next": "14.2.11", "eslint-config-next": "14.2.11",
"jest": "^29.7.0",
"postcss": "^8", "postcss": "^8",
"tailwindcss": "^3.4.1", "tailwindcss": "^3.4.1",
"typescript": "^5" "typescript": "^5"
+27 -3
View File
@@ -3,8 +3,8 @@
@tailwind utilities; @tailwind utilities;
:root { :root {
--background: #ffffff; --background: #171717;
--foreground: #171717; --foreground: #ffffff;
} }
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
@@ -157,4 +157,28 @@ body {
.popup-overlay { .popup-overlay {
background: rgba(0, 0, 0, 0.5); background: rgba(0, 0, 0, 0.5);
} }
.dropdown-custom {
width: 20%;
background-color: black;
color: white;
border: none;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
.dropdown-custom:hover {
background-color: #444;
}
.dropdown-custom-maxmin {
width: 150px;
background-color: black;
color: white;
border: none;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
+204
View File
@@ -0,0 +1,204 @@
export default function text(lang: string, input: string): string {
// German translation
if (lang === "ger") {
switch (input) {
case "header_title":
return "OR-Tool";
case "header_subtitle":
return "von Spaceholder Programming";
case "boxObjTitle":
return "Ziel";
case "boxObjDesc":
return "Geben Sie Ihr Ziel hier ein. Es ist nur ein Ziel erlaubt. Verwenden Sie eine Zeile dafür (kein 'Enter'!). Erlaubte Symbole sind 0-9, a-z, A-Z und <>=.\nBeispiel:\nx + y\n-786433 x1 + 655361 x2";
case "boxSubjTitle":
return "Nebenbedingungen";
case "boxSubjDesc":
return "Geben Sie Ihre Nebenbedingungen hier ein. Eine pro Zeile (mit der 'Enter'-Taste trennen). Erlaubte Symbole sind 0-9, a-z, A-Z und <>=.\nBeispiel:\n+1 x + 2 y <= 15\n524321 x14 + 524305 x15 <= 4194303.5";
case "boxBoundsTitle":
return "Grenzen";
case "boxBoundsDesc":
return "Geben Sie Ihre Grenzen hier ein. Eine pro Zeile (mit der 'Enter'-Taste trennen). Erlaubte Symbole sind 0-9, a-z, A-Z und <>=.\nBeispiel:\nx >= 0\nx > 0\n0 <= x1 <= 1";
case "boxVarsTitle":
return "Variablen";
case "boxVarsDesc":
return "Listen Sie alle Ihre Variablen auf. Eine pro Zeile (mit der 'Enter'-Taste trennen). Erlaubte Symbole sind a-z, A-Z.\nBeispiel:\nx\ny";
case "boxExportLP":
return "Als LP exportieren";
case "boxOut":
return "Geben Sie ein Problem ein und drücken Sie eine Aktionstaste, um die Ausgabe anzuzeigen...";
case "buttonCalc":
return "Berechnen";
case "etime":
return "Berechnungsdauer";
case "seconds":
return "Sekunden";
case "err_invalidInput":
return "Fehler: Ungültige Eingabe in";
case "err_nullInput":
return "Fehler: NULL- oder undefinierte Eingabe in";
case "err_invalidInput":
return "Fehler: Ungültige Eingabe oder fehlendes Zeichen in";
case "input_checks_successful":
return "Alle Eingabeprüfungen erfolgreich.";
case "input_checks_start":
return "Starte Eingabeprüfungen...";
case "obj_box":
return "Zielfeld";
case "subj_box":
return "Bedingungsfeld";
case "bounds_box":
return "Grenzenfeld";
case "vars_box":
return "Variablenfeld";
case "err_emptyBox":
return "Fehler: Leeres Textfeld.";
case "calculating":
return "Berechne...";
case "maximize":
return "Maximieren";
case "minimize":
return "Minimieren";
case "run_optimization":
return "Optimierung mit Eingaben wird ausgeführt";
case "startProblemSetup":
return "Starte Problemerstellung...";
case "succProblemSetup":
return "Problem erstellt.";
case "startScaling":
return "Skalieren des Problems...";
case "succScaling":
return "Skalierung erfolgreich.";
case "startOptimizationSimplex":
return "Starte Simplex-Optimierung...";
case "succOptimizationSimplex":
return "Simplex-Optimierung abgeschlossen.";
case "startOptimizationInteger":
return "Starte Ganzzahloptimierung...";
case "succOptimizationInteger":
return "Ganzzahloptimierung abgeschlossen.";
case "finalObjValue":
return "Endgültiger Zielfunktionswert";
case "varsValues":
return "Wert jeder Variable";
case "dualValues":
return "Dualwerte der Einschränkungen";
case "downloadPrepFileString":
return "Dateiinhalt wird vorbereitet...";
case "downloadPrepFile":
return "Datei wird vorbereitet...";
case "downloadStart":
return "Download wird gestartet.";
case "downloadPrep":
return "Download wird vorbereitet...";
case "downloadFetchInput":
return "Eingaben werden geladen...";
case "importing":
return "Importiere...";
default:
return input;
}
}
// English translation
if (lang === "eng") {
switch (input) {
case "header_title":
return "OR-Tool";
case "header_subtitle":
return "by Spaceholder Programming";
case "boxObjTitle":
return "Objective";
case "boxObjDesc":
return "Insert your objective here. One objective is allowed. Use one line for it (no \"return\"!) Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\nx + y\n-786433 x1 + 655361 x2";
case "boxSubjTitle":
return "Subject";
case "boxSubjDesc":
return "Insert your subject here. One per line (divide by 'return' button). Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\n+1 x + 2 y <= 15\n524321 x14 + 524305 x15 <= 4194303.5";
case "boxBoundsTitle":
return "Bounds";
case "boxBoundsDesc":
return "Insert your bounds here. One per line (divide by 'return' button). Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\nx >= 0\nx > 0\n0 <= x1 <= 1";
case "boxVarsTitle":
return "Variables";
case "boxVarsDesc":
return "List all your variables. One per line (divide by 'return' button). Allowed symbols are a-z, A-Z.\nExample:\nx\ny";
case "boxExportLP":
return "Export as LP";
case "boxOut":
return "Input a problem and an action button to display output...";
case "buttonCalc":
return "Calculate";
case "etime":
return "Elapsed time";
case "seconds":
return "seconds";
case "err_invalidInput":
return "Error: Invalid input in";
case "err_nullInput":
return "Error: NULL or undefined input in";
case "err_invalidInput":
return "Error: Invalid input or missing character in";
case "input_checks_successful":
return "All input checks successful."
case "input_checks_start":
return "Starting input checks...";
case "obj_box":
return "object box";
case "subj_box":
return "subject box";
case "bounds_box":
return "bounds box";
case "vars_box":
return "variables box";
case "err_emptyBox":
return "Error: Empty text box.";
case "calculating":
return "Calculating...";
case "maximize":
return "Maximize";
case "minimize":
return "Minimize";
case "run_optimization":
return "Running optimization with input";
case "startProblemSetup":
return "Starting problem setup...";
case "succProblemSetup":
return "Problem created.";
case "startScaling":
return "Scaling problem...";
case "succScaling":
return "Scaling successful.";
case "startOptimizationSimplex":
return "Starting simplex optimization...";
case "succOptimizationSimplex":
return "Simplex optimization complete.";
case "startOptimizationInteger":
return "Starting integer optimization...";
case "succOptimizationInteger":
return "Integer optimization complete.";
case "finalObjValue":
return "Final objective value";
case "varsValues":
return "Value of each variable";
case "dualValues":
return "Dual values of constraints";
case "downloadPrepFileString":
return "Preparing file content string...";
case "downloadPrepFile":
return "Preparing file...";
case "downloadStart":
return "Starting download.";
case "downloadPrep":
return "Preparing download...";
case "downloadFetchInput":
return "Fetching input...";
case "importing":
return "Importing...";
default:
return input;
}
}
return "Error: Translation Module - Language Not Known.";
}
+3 -3
View File
@@ -45,7 +45,7 @@ export default function RootLayout({
width={16} width={16}
height={16} height={16}
/> />
Go to our docs Docs
</a> </a>
<a <a
className="flex items-center gap-2 hover:underline hover:underline-offset-4" className="flex items-center gap-2 hover:underline hover:underline-offset-4"
@@ -60,7 +60,7 @@ export default function RootLayout({
width={16} width={16}
height={16} height={16}
/> />
See the source code Source Code
</a> </a>
<a <a
className="flex items-center gap-2 hover:underline hover:underline-offset-4" className="flex items-center gap-2 hover:underline hover:underline-offset-4"
@@ -75,7 +75,7 @@ export default function RootLayout({
width={16} width={16}
height={16} height={16}
/> />
Powered by GLPK GLPK
</a> </a>
</footer> </footer>
</div> </div>
+67 -30
View File
@@ -1,58 +1,95 @@
'use client' 'use client'
import React, { useState } from 'react';
import { Box, Button, Output } from "./modules"; import { Box, Button, Output } from "./modules";
import { calculate_click, downloadLP, import_click } from "./scripts" import { calculate_click, downloadLP, import_click } from "./scripts";
import text from "./lang";
export default function Home() { export default function Home() {
const [language, setLanguage] = useState('eng');
const [maxminOption, setMaxminOption] = useState('maximize'); // Zustand für den MaxMin-Switch
const tr_hTitle = text(language, 'header_title');
const tr_hSubtitle = text(language, 'header_subtitle');
const tr_boxObjTitle = text(language, 'boxObjTitle');
const tr_boxObjDesc = text(language, "boxObjDesc");
const tr_boxSubjTitle = text(language, 'boxSubjTitle');
const tr_boxSubjDesc = text(language, "boxSubjDesc");
const tr_boxBoundsTitle = text(language, 'boxBoundsTitle');
const tr_boxBoundsDesc = text(language, "boxBoundsDesc");
const tr_boxVarsTitle = text(language, 'boxVarsTitle');
const tr_boxVarsDesc = text(language, "boxVarsDesc");
const tr_boxOut = text(language, "boxOut");
const tr_boxExportLP = text(language, "boxExportLP");
const tr_calc_max = text(language, "maximize");
const tr_calc_min = text(language, "minimize");
const tr_calcButton = text(language, "buttonCalc");
const handleLanguageChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setLanguage(event.target.value);
};
const handleMaxMinChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setMaxminOption(event.target.value); // Update den Zustand basierend auf dem Wert des Selects
};
return ( return (
<> <>
<header className="header"> <header className="header">
<div className="title"> <div className="title">
<main className="header_box"> <main className="header_box">
Operations Research Tool {tr_hTitle}
<br></br> <br />
<span className="header_copyright"> <span className="header_copyright">
<i>by Spaceholder Programming</i> <i>{tr_hSubtitle}</i>
</span> </span>
<br />
<select id="language_current" value={language} onChange={handleLanguageChange} className="dropdown-custom">
<option value="ger">Deutsch</option>
<option value="eng">English</option>
</select>
</main> </main>
</div> </div>
</header> </header>
<Box <Box
title={"Objective"} title={tr_boxObjTitle}
placeholder={"Insert your objective here. One objective is allowed. Use one line for it (no \"return\"!) Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\nx + y\n-786433 x1 + 655361 x2"} placeholder={tr_boxObjDesc}
id="objective"/> id="objective"
/>
<Box <Box
title={"Subject"} title={tr_boxSubjTitle}
placeholder={"Insert your subject here. One per line (divide by 'return' button). Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\n+1 x + 2 y <= 15\n524321 x14 + 524305 x15 <= 4194303.5"} placeholder={tr_boxSubjDesc}
id="subject"/> id="subject"
/>
<Box <Box
title={"Bounds"} title={tr_boxBoundsTitle}
placeholder={"Insert your bounds here. One per line (divide by 'return' button). Allowed symbols are 0-9, a-z, A-Z and <>=.\nExample:\nx >= 0\nx > 0\n0 <= x1 <= 1"} placeholder={tr_boxBoundsDesc}
id="bounds"/> id="bounds"
/>
<Box <Box
title={"Variables"} title={tr_boxVarsTitle}
placeholder={"List all your variables. One per line (divide by 'return' button). Allowed symbols are a-z, A-Z.\nExample:\nx\ny"} placeholder={tr_boxVarsDesc}
id="vars" /> id="vars"
/>
<select id="maxminswitch" value={maxminOption} onChange={handleMaxMinChange} className="dropdown-custom-maxmin">
<option value="maximize">{tr_calc_max}</option>
<option value="minimize">{tr_calc_min}</option>
</select>
<Button <Button
title={"Calculate"} title={tr_calcButton}
className={"button_green"} className={"button_green"}
onClickFunc={calculate_click} /> onClickFunc={calculate_click}
{/* <Popup_Button />
title={"Import"}
className={"button"} /> */}
<Button <Button
title={"Export as LP"} title={tr_boxExportLP}
className={"button"} className={"button"}
onClickFunc={downloadLP} /> onClickFunc={downloadLP}
<br></br> />
<br />
<Output <Output
id="out" id="out"
text={"Input a problem and an action button to display output..."}/> text={tr_boxOut}
{/* <Popup_Button />
title="Popup"
className="button"
/> */}
</> </>
); );
} }
+95 -60
View File
@@ -5,26 +5,41 @@ import * as LPAPI from "../api/optimizeLP.js"
import * as GLPKAPI from "../solver/glpk.min.js" import * as GLPKAPI from "../solver/glpk.min.js"
import { start } from "repl"; import { start } from "repl";
import text from "./lang"
// custom log so we can append the output dynamically // custom log so we can append the output dynamically
function customLog(message: string) { export function customLog(input: string) {
console.log(message); // Continue to print message inside of box // get language
const lang = (document.getElementById('language_current') as HTMLSelectElement)?.value;
// Get Output Box // Get Output Box
const outputElement = document.getElementById('out'); const outputElement = document.getElementById('out');
// load text
const message: string = text(lang, input);
console.log(message); // Continue to print message inside of box
// Append message if element exists // Append message if element exists
if (outputElement) { if (outputElement) {
outputElement.innerHTML += message + "<br>"; // Append message outputElement.innerHTML += message + "<br>"; // Append message
} }
} }
function customLogClear() { export function customLogClear() {
const outElement = document.getElementById('out'); const outElement = document.getElementById('out');
if (outElement) { if (outElement) {
outElement.innerHTML = ""; outElement.innerHTML = "";
} }
} }
export function getTranslation(input: string) {
// get language
const lang = (document.getElementById('language_current') as HTMLSelectElement)?.value;
// return translation
return text(lang, input);
}
function walltimeStopAndPrint(startpoint: number) { function walltimeStopAndPrint(startpoint: number) {
// calculating elapsed time as timestamp // calculating elapsed time as timestamp
let duration = Date.now() - startpoint; let duration = Date.now() - startpoint;
@@ -37,7 +52,8 @@ function walltimeStopAndPrint(startpoint: number) {
const durationFormatted = seconds + (milliseconds >= 0 ? "." : ".") + Math.abs(milliseconds).toFixed(3).slice(2); const durationFormatted = seconds + (milliseconds >= 0 ? "." : ".") + Math.abs(milliseconds).toFixed(3).slice(2);
// Printing elapsed time // Printing elapsed time
customLog("Elapsed time: " + durationFormatted + " seconds<br>"); customLog(getTranslation("etime") + ": " + durationFormatted + " " + getTranslation("seconds"));
customLog("");
// return durationFormatted; // return durationFormatted;
} }
@@ -47,11 +63,11 @@ function walltimeStart() {
} }
function isInputValidRegex(obj: string | undefined, subj: string | undefined, bounds: string | undefined, vars: string | undefined): boolean { function isInputValidRegex(obj: string | undefined, subj: string | undefined, bounds: string | undefined, vars: string | undefined): boolean {
customLog("Starting input checks..."); customLog("input_checks_start");
// standard case: input is undefined - invalid // standard case: input is undefined - invalid
if (obj === undefined || obj === null || subj === undefined || subj === null || bounds === undefined || bounds === null || vars === undefined || vars === null) { if (obj === undefined || obj === null || subj === undefined || subj === null || bounds === undefined || bounds === null || vars === undefined || vars === null) {
customLog("Error: Function isInputValidRegex received undefined or null input."); customLog(getTranslation("err_nullInput") + "function isInputValidRegex.");
return false; return false;
} }
@@ -59,54 +75,54 @@ function isInputValidRegex(obj: string | undefined, subj: string | undefined, bo
let regex = /^[ (\n)]*[\+-]? *((\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*)( *[\+-] *((\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*))*[ (\n)]*$/g; let regex = /^[ (\n)]*[\+-]? *((\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*)( *[\+-] *((\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*))*[ (\n)]*$/g;
let isValid = regex.test(obj); let isValid = regex.test(obj);
if (!isValid) { if (!isValid) {
customLog("Error: Invalid or missing character in object box."); customLog(getTranslation("err_invalidInput") + " " + getTranslation("obj_box") + ".");
return false; return false;
} }
// RegEx check for subject // RegEx check for subject
regex = /^([ (\n)]*[\+-]* *(\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*( *[\+-] *(\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*)* *((<=?)|(>=?)|=) *[\+-]? *\d+(.\d+)?[ (\n)]*)+$/g; regex = /^([ (\n)]*[\+-]* *(\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*( *[\+-] *(\d+(.\d+)? )?[a-zA-Z][a-zA-Z0-9]*)* *((<=?)|(>=?)|=) *[\+-]? *\d+(.\d+)?[ (\n)]*)+$/g;
isValid = regex.test(subj); isValid = regex.test(subj);
if (!isValid) { if (!isValid) {
customLog("Error: Invalid or missing character in subject box."); customLog(getTranslation("err_invalidInput") + " " + getTranslation("subj_box") + ".");
return false; return false;
} }
// RegEx check for subject // RegEx check for subject
regex = /[ (\n)]*(([a-zA-Z][a-zA-Z0-9]* *((<=?)|(>=?)|=) *\d(.\d+)?)|((\d(.\d+)?) *<=? *[a-zA-Z][a-zA-Z0-9]* *<= *(\d(.\d+)?)))[ (\n)]*/g; regex = /[ (\n)]*(([a-zA-Z][a-zA-Z0-9]* *((<=?)|(>=?)|=) *\d(.\d+)?)|((\d(.\d+)?) *<=? *[a-zA-Z][a-zA-Z0-9]* *<= *(\d(.\d+)?)))[ (\n)]*/g;
isValid = regex.test(bounds); isValid = regex.test(bounds);
if (!isValid) { if (!isValid) {
customLog("Error: Invalid or missing character in bounds box."); customLog(getTranslation("err_invalidInput") + " " + getTranslation("bounds_box") + ".");
return false; return false;
} }
// RegEx check for variables // RegEx check for variables
regex = /^ *([a-zA-Z][a-zA-Z0-9]*(\n)* *)+$/g; regex = /^ *([a-zA-Z][a-zA-Z0-9]*(\n)* *)+$/g;
isValid = regex.test(vars); isValid = regex.test(vars);
if (!isValid) { if (!isValid) {
customLog("Error: Invalid or missing character in variables box."); customLog(getTranslation("err_invalidInput") + " " + getTranslation("vars_box") + ".");
return false; return false;
} }
customLog("All input checks successful."); customLog("input_checks_successful");
customLog(""); customLog("");
return true; return true;
} }
function isInputFilled(obj: string | undefined, subj: string | undefined, bounds: string | undefined, vars: string | undefined) { function isInputFilled(obj: string | undefined, subj: string | undefined, bounds: string | undefined, vars: string | undefined) {
if (obj == "" || obj == null || obj == undefined) { if (obj == "" || obj == null || obj == undefined) {
customLog("Error: Empty input field."); customLog("err_emptyBox");
return false; return false;
} }
if (subj == "" || subj == null || subj == undefined) { if (subj == "" || subj == null || subj == undefined) {
customLog("Error: Empty input field."); customLog("err_emptyBox");
return false; return false;
} }
if (bounds == "" || bounds == null || bounds == undefined) { if (bounds == "" || bounds == null || bounds == undefined) {
customLog("Error: Empty input field."); customLog("err_emptyBox");
return false; return false;
} }
if (vars == "" || vars == null || vars == undefined) { if (vars == "" || vars == null || vars == undefined) {
customLog("Error: Empty input field."); customLog("err_emptyBox");
return false; return false;
} }
return true; return true;
@@ -115,7 +131,8 @@ function isInputFilled(obj: string | undefined, subj: string | undefined, bounds
export function calculate_click() { export function calculate_click() {
customLogClear(); customLogClear();
const timer = walltimeStart(); const timer = walltimeStart();
customLog("Calculating...<br>"); customLog("calculating");
customLog("");
let objective: string | undefined; let objective: string | undefined;
const objectiveElement = document.getElementById('objective'); const objectiveElement = document.getElementById('objective');
@@ -190,77 +207,95 @@ export function calculate_click() {
// console.log(parseFunction(decider)); // console.log(parseFunction(decider));
// catch error: empty input field(s) // catch error: empty input field(s)
if (!isInputFilled(objective, subject, bounds, variables)) return; if (!isInputFilled(objective, subject, bounds, variables)) return;
// catch error: variables field has invalid characters // catch error: variables field has invalid characters
if (!isInputValidRegex(objective, subject, bounds, variables)) return; if (!isInputValidRegex(objective, subject, bounds, variables)) return;
let wholeText: string = "Maximize\n obj: " + objective // fetch operator
+ "\nSubject To \n" + subject const maxmin = (document.getElementById('maxminswitch') as HTMLSelectElement)?.value;
+ "\nBounds \n" + bounds let operator = "Minimize";
+ "\nGenerals \n" + variables if (maxmin == "maximize") operator = "Maximize";
+ "\nEnd";
let wholeText: string = operator + "\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("<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(getTranslation("run_optimization") + ": \"" + wholeText + "\"");
customLog("");
run(wholeText); run(wholeText);
walltimeStopAndPrint(timer); walltimeStopAndPrint(timer);
} }
function run(text: string) { function run(text: string) {
customLog("Starting problem setup..."); customLog("startProblemSetup");
let lp = GLPKAPI.glp_create_prob(); let lp = GLPKAPI.glp_create_prob();
GLPKAPI.glp_read_lp_from_string(lp, null, text); GLPKAPI.glp_read_lp_from_string(lp, null, text);
customLog("Problem created.<br>"); customLog("succProblemSetup");
customLog("");
customLog("Scaling problem..."); customLog("startScaling");
GLPKAPI.glp_scale_prob(lp, GLPKAPI.GLP_SF_AUTO); GLPKAPI.glp_scale_prob(lp, GLPKAPI.GLP_SF_AUTO);
customLog("Scaling complete.<br>"); customLog("succScaling");
customLog("");
customLog("Starting simplex optimization..."); customLog("startOptimizationSimplex");
let smcp = new GLPKAPI.SMCP({ presolve: GLPKAPI.GLP_ON }); let smcp = new GLPKAPI.SMCP({ presolve: GLPKAPI.GLP_ON });
GLPKAPI.glp_simplex(lp, smcp); GLPKAPI.glp_simplex(lp, smcp);
customLog("Simplex optimization complete.<br>"); customLog("succOptimizationSimplex");
customLog("");
customLog("Starting integer optimization..."); customLog("startOptimizationInteger");
let iocp = new GLPKAPI.IOCP({ presolve: GLPKAPI.GLP_ON }); let iocp = new GLPKAPI.IOCP({ presolve: GLPKAPI.GLP_ON });
GLPKAPI.glp_intopt(lp, iocp); GLPKAPI.glp_intopt(lp, iocp);
customLog("Integer optimization complete.<br>"); customLog("succOptimizationInteger");
customLog("");
// customLog("obj: " + GLPKAPI.glp_mip_obj_val(lp)); // customLog("obj: " + GLPKAPI.glp_mip_obj_val(lp));
customLog("<i>Final objective value: " + GLPKAPI.glp_mip_obj_val(lp) + "</i><br>"); customLog("<i>" + getTranslation("finalObjValue") + ": " + GLPKAPI.glp_mip_obj_val(lp) + "</i>");
customLog("Value of each variable:"); customLog("");
for (let i = 1; i <= GLPKAPI.glp_get_num_cols(lp) - 1; i++) { // "-1" to remove the "End-variable" from logs customLog(getTranslation("varsValues") + ":");
for (let i = 1; i <= GLPKAPI.glp_get_num_cols(lp); i++) {
customLog(GLPKAPI.glp_get_col_name(lp, i) + " = " + GLPKAPI.glp_mip_col_val(lp, i)); customLog(GLPKAPI.glp_get_col_name(lp, i) + " = " + GLPKAPI.glp_mip_col_val(lp, i));
} }
customLog(""); customLog("");
customLog("Dual values of constraints:"); customLog(getTranslation("dualValues") + ":");
for (let j = 1; j <= GLPKAPI.glp_get_num_rows(lp); j++) { for (let j = 1; j <= GLPKAPI.glp_get_num_rows(lp); j++) {
const dualValue = GLPKAPI.glp_get_row_dual(lp, j); // fetch dual 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: any, subject: any, bounds: any) { function downloadLPFormatting(objective: any, subject: any, bounds: any) {
customLog("Preparing file content string...<br>"); customLog(getTranslation("downloadPrepFileString"));
customLog("");
// ensure that all vars are strings // ensure that all vars are strings
const formattedObjective = typeof objective === 'string' ? objective : ''; const formattedObjective = typeof objective === 'string' ? objective : '';
const formattedSubject = typeof subject === 'string' ? subject : ''; const formattedSubject = typeof subject === 'string' ? subject : '';
const formattedBounds = typeof bounds === 'string' ? bounds : ''; const formattedBounds = typeof bounds === 'string' ? bounds : '';
// fetch operator
const maxmin = (document.getElementById('maxminswitch') as HTMLSelectElement)?.value;
let operator = "Minimize";
if (maxmin == "maximize") operator = "Maximize";
// Header mit Problemname // Header mit Problemname
const header = "\\ Your problem\n"; const header = "\\ Your problem\n";
// format objective // format objective
const objectiveFunction = `Maximize\n obj: ${formattedObjective}\n`;
const objectiveFunction = operator + `\n obj: ${formattedObjective}\n`;
// turn each subject into a single line // 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`; const constraints = `Subject To\n${formattedSubject.split("\n").filter(line => line.trim() !== "").map(line => ` ${line}`).join("\n")}\n`;
@@ -274,23 +309,23 @@ function downloadLPFormatting(objective: any, subject: any, bounds: any) {
return lpFormat; return lpFormat;
} }
function downloadProblemDownload(content: string) { function downloadProblemDownload(content: string) {
customLog("Preparing file...<br>") customLog("downloadPrepFile");
customLog("");
const blob = new Blob([content], { type: 'text/plain' }); const blob = new Blob([content], { type: 'text/plain' });
const link = document.createElement('a'); const link = document.createElement('a');
link.href = URL.createObjectURL(blob); link.href = URL.createObjectURL(blob);
link.download = 'problem.txt'; // file name link.download = 'problem.txt'; // file name
link.click(); // starting download link.click(); // starting download
customLog("Starting download.") customLog("downloadStart");
} }
export function downloadLP() { export function downloadLP() {
customLogClear(); customLogClear();
customLog("Preparing download...<br>"); customLog("downloadPrep");
customLog("Fetching input...<br>") customLog("");
customLog("downloadFetchInput");
customLog("");
let objective: string | undefined; let objective: string | undefined;
const objectiveElement = document.getElementById('objective'); const objectiveElement = document.getElementById('objective');
@@ -335,7 +370,7 @@ export function downloadLP() {
export function import_click() { export function import_click() {
console.log("Importing..."); console.log("importing");
} }