From ebf7e33e23f29d39f37589f6fc367fdf01c0958e Mon Sep 17 00:00:00 2001 From: SinusFox <61253950+SinusFox@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:36:43 +0200 Subject: [PATCH] Adding walltime Can be called using: Start: function walltimeStart() { returns Date.now(); Stop: function walltimeStopAndPrint(startpoint: number) { Add startpoint as argument. It prints the elapsed time using customLog() --- src/app/scripts.ts | 116 +++++++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 47 deletions(-) diff --git a/src/app/scripts.ts b/src/app/scripts.ts index 6117819..03a1974 100644 --- a/src/app/scripts.ts +++ b/src/app/scripts.ts @@ -3,30 +3,53 @@ import * as LP from "../pages/parseLP.ts" import * as LPAPI from "../pages/api/optimizeLP.js" import * as GLPKAPI from "../solver/glpk.min.js" +import { start } from "repl"; // custom log so we can append the output dynamically function customLog(message: string) { - console.log(message); // Nachricht weiterhin in der Konsole ausgeben + console.log(message); // Continue to print message inside of box - // Hol das Element mit der ID "out" + // Get Output Box const outputElement = document.getElementById('out'); - - // Wenn das Element existiert, füge die Nachricht hinzu + + // Append message if element exists if (outputElement) { - outputElement.innerHTML += message + "
"; // Nachricht anhängen + outputElement.innerHTML += message + "
"; // Append message } } -function customLogClear() { - document.getElementById('out').innerHTML =""; +function customLogClear() { + document.getElementById('out').innerHTML = ""; +} + +function walltimeStopAndPrint(startpoint: number) { + // calculating elapsed time as timestamp + var duration = Date.now() - startpoint; + + // Calculate seconds and ms + const seconds = Math.floor(duration / 1000); + const milliseconds = (duration % 1000) / 1000; + + // formatting + const durationFormatted = seconds + (milliseconds >= 0 ? "." : ".") + Math.abs(milliseconds).toFixed(3).slice(2); + + // Printing elapsed time + customLog("Elapsed time: " + durationFormatted + " seconds
"); + + // return durationFormatted; +} + +function walltimeStart() { + return Date.now(); } export function calculate_click() { customLogClear(); + const timer = walltimeStart(); customLog("Calculating...
"); - let functions:string|undefined = document.getElementById('funcs').value; - let variables:string|undefined = document.getElementById('vars').value; + let functions: string | undefined = document.getElementById('funcs').value; + let variables: string | undefined = document.getElementById('vars').value; if (functions == undefined || variables == undefined) return; // let funcs:string[] = functions.split(/;/); @@ -53,7 +76,7 @@ export function calculate_click() { // namesVars.push(regexMatch[1]); - + // console.log(regexMatch); // } @@ -78,52 +101,51 @@ export function calculate_click() { // console.log(parseFunction(decider)); - let wholeText:string = functions + "\nGenerals \n"+variables + "End"; + let wholeText: string = functions + "\nGenerals \n" + variables + "End"; customLog("Running optimization with input: \"" + wholeText + "\"
"); run(wholeText); - - - - } - - function run(text:string){ - customLog("Starting problem setup..."); - var lp = GLPKAPI.glp_create_prob(); - GLPKAPI.glp_read_lp_from_string(lp, null, text); - customLog("Problem created.
"); - - customLog("Scaling problem..."); - GLPKAPI.glp_scale_prob(lp, GLPKAPI.GLP_SF_AUTO); - customLog("Scaling complete.
"); - - customLog("Starting simplex optimization..."); - var smcp = new GLPKAPI.SMCP({presolve: GLPKAPI.GLP_ON}); - GLPKAPI.glp_simplex(lp, smcp); - customLog("Simplex optimization complete.
"); - - customLog("Starting integer optimization..."); - var iocp = new GLPKAPI.IOCP({presolve: GLPKAPI.GLP_ON}); - GLPKAPI.glp_intopt(lp, iocp); - customLog("Integer optimization complete.
"); - - // customLog("obj: " + GLPKAPI.glp_mip_obj_val(lp)); - customLog("Final objective value: " + GLPKAPI.glp_mip_obj_val(lp) + "
"); - customLog("Value of each variable:"); - for(var i = 1; i <= GLPKAPI.glp_get_num_cols(lp) - 1; i++){ // "-1" to remove the "End-variable" from logs - customLog(GLPKAPI.glp_get_col_name(lp, i) + " = " + GLPKAPI.glp_mip_col_val(lp, i)); - } + walltimeStopAndPrint(timer); } - // Irgend ein Interface - // document.getElementById('out').innerHTML = funcs; +function run(text: string) { + customLog("Starting problem setup..."); + var lp = GLPKAPI.glp_create_prob(); + GLPKAPI.glp_read_lp_from_string(lp, null, text); + customLog("Problem created.
"); - // output.innerHTML = functions.innerHTML; + customLog("Scaling problem..."); + GLPKAPI.glp_scale_prob(lp, GLPKAPI.GLP_SF_AUTO); + customLog("Scaling complete.
"); - // createProblemMIP(); + customLog("Starting simplex optimization..."); + var smcp = new GLPKAPI.SMCP({ presolve: GLPKAPI.GLP_ON }); + GLPKAPI.glp_simplex(lp, smcp); + customLog("Simplex optimization complete.
"); - // LPAPI.default(); + customLog("Starting integer optimization..."); + var iocp = new GLPKAPI.IOCP({ presolve: GLPKAPI.GLP_ON }); + GLPKAPI.glp_intopt(lp, iocp); + customLog("Integer optimization complete.
"); + + // customLog("obj: " + GLPKAPI.glp_mip_obj_val(lp)); + customLog("Final objective value: " + GLPKAPI.glp_mip_obj_val(lp) + "
"); + customLog("Value of each variable:"); + for (var i = 1; i <= GLPKAPI.glp_get_num_cols(lp) - 1; i++) { // "-1" to remove the "End-variable" from logs + customLog(GLPKAPI.glp_get_col_name(lp, i) + " = " + GLPKAPI.glp_mip_col_val(lp, i)); + } + customLog(""); +} + +// Irgend ein Interface +// document.getElementById('out').innerHTML = funcs; + +// output.innerHTML = functions.innerHTML; + +// createProblemMIP(); + +// LPAPI.default(); export function import_click() {