From a3a67035d8631aae0d5eb05cb4742c7c86f961bf Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Sun, 3 Nov 2019 09:57:14 -0500 Subject: [PATCH] run it my dudes --- .classpath | 1 + ampl/ampl_model.dat | 20 ++++++++++ ampl/ampl_solve.run | 7 ++++ data/flows.csv | 6 +++ node/run.js | 31 +++++++++++++-- src/evac/AmplGenerator.java | 44 +++++++++++++++++++++ src/evac/AmplPrepMain.java | 78 +++++++++++++++++++++++++++++++++++++ src/evac/KmlGenerator.java | 2 +- src/evac/Main.java | 26 +++++++++++++ 9 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 ampl/ampl_model.dat create mode 100644 ampl/ampl_solve.run create mode 100644 data/flows.csv create mode 100644 src/evac/AmplGenerator.java create mode 100644 src/evac/AmplPrepMain.java diff --git a/.classpath b/.classpath index 33c97a3..7021515 100644 --- a/.classpath +++ b/.classpath @@ -6,5 +6,6 @@ + diff --git a/ampl/ampl_model.dat b/ampl/ampl_model.dat new file mode 100644 index 0000000..453f7d1 --- /dev/null +++ b/ampl/ampl_model.dat @@ -0,0 +1,20 @@ +set EVAC := Troy Brunswick ; +set DEST := Albany Guilderland EastGreenbush Schenectady ; +param: EDGES : edgecapacity := + Troy Brunswick 250.000000 + Troy Guilderland 200.000000 + Troy Albany 500.000000 + Troy EastGreenbush 100.000000 + Brunswick EastGreenbush 100.000000 + Guilderland Schenectady 100.000000 +; +param population := + Troy 700 + Brunswick 300 +; +param capacity := + Albany 3000 + Guilderland 300 + EastGreenbush 500 + Schenectady 400 +; diff --git a/ampl/ampl_solve.run b/ampl/ampl_solve.run new file mode 100644 index 0000000..b06b7fc --- /dev/null +++ b/ampl/ampl_solve.run @@ -0,0 +1,7 @@ +model model.mdl; + +data ampl_model.dat; + +solve; + +display Flow; diff --git a/data/flows.csv b/data/flows.csv new file mode 100644 index 0000000..163595e --- /dev/null +++ b/data/flows.csv @@ -0,0 +1,6 @@ +Brunswick,EastGreenbush,300 +Guilderland,Schenectady,0 +Troy,Albany,200 +Troy,Brunswick,0 +Troy,EastGreenbush,200 +Troy,Guilderland,300 \ No newline at end of file diff --git a/node/run.js b/node/run.js index 535c299..2da34a1 100644 --- a/node/run.js +++ b/node/run.js @@ -1,4 +1,5 @@ const child_process = require('child_process'); +const fs = require('fs'); async function run_process(command, args, cwd) { let process = child_process.spawn(command, args, { cwd: cwd }); @@ -16,13 +17,16 @@ async function run_process(command, args, cwd) { return new Promise((resolve, reject) => { process.on('close', (code, signal) => { + if (code != 0) { + reject('exit code: ' + code); + } console.log('code', code, 'signal', signal); console.log('stdout'); console.log(stdout); console.log('stderr'); console.log(stderr); - resolve(); + resolve(stdout); }); }); } @@ -33,12 +37,33 @@ async function run() { // launch java (read input data) cwd = "/home/elipzer/eclipse-workspace/EvacuationPlanner"; - await run_process("java", [ "-classpath", "/home/elipzer/eclipse-workspace/EvacuationPlanner/bin", "evac.KmlTest" ], cwd); + try { + await run_process("java", [ + "-classpath", + "/home/elipzer/eclipse-workspace/EvacuationPlanner/bin" + + ":/home/elipzer/eclipse-workspace/EvacuationPlanner/include/commons-csv-1.7/commons-csv-1.7.jar", + "evac.AmplPrepMain" ], cwd); + } catch (e) { + console.error(e); + } // launch ampl (calculate flows) cwd = "/home/elipzer/eclipse-workspace/EvacuationPlanner/ampl"; - await run_process("ampl", [ "solve.run" ], cwd); + try { + let output = await run_process("ampl", [ "ampl_solve.run" ], cwd); + let lines = output.split('\n'); + let semicolon = lines.indexOf(';'); + let flows = lines.slice(3, semicolon); + let flowsCsv = flows.map(line => { + return line.replace(/\s+/g, ','); + }).join('\n'); + fs.writeFileSync('../data/flows.csv', flowsCsv); + console.log('output (flows.csv):'); + console.log(flowsCsv); + } catch (e) { + console.error(e); + } // launch java (generate kml) diff --git a/src/evac/AmplGenerator.java b/src/evac/AmplGenerator.java new file mode 100644 index 0000000..ff75a34 --- /dev/null +++ b/src/evac/AmplGenerator.java @@ -0,0 +1,44 @@ +package evac; + +import java.util.List; + +public class AmplGenerator { + public static String createData(InputModel model, List evac, List dest) { + StringBuilder builder = new StringBuilder(); + + builder.append("set EVAC := "); + for (Node n : evac) { + builder.append(n.name); + builder.append(" "); + } + builder.append(";\n"); + + builder.append("set DEST := "); + for (Node n : dest) { + builder.append(n.name); + builder.append(" "); + } + builder.append(";\n"); + + builder.append("param: EDGES : edgecapacity :=\n"); + for (Edge edge : model.edges) { + builder.append(String.format(" %s %s %f\n", edge.src.name, edge.dst.name, edge.capacity)); + } + builder.append(";\n"); + + builder.append("param population :=\n"); + for (Node n : evac) { + builder.append(String.format(" %s %d\n", n.name, n.population)); + } + builder.append(";\n"); + + builder.append("param capacity :=\n"); + for (Node n : dest) { + builder.append(String.format(" %s %d\n", n.name, n.capacity - n.population)); + } + builder.append(";\n"); + + + return builder.toString(); + } +} diff --git a/src/evac/AmplPrepMain.java b/src/evac/AmplPrepMain.java new file mode 100644 index 0000000..9606cc0 --- /dev/null +++ b/src/evac/AmplPrepMain.java @@ -0,0 +1,78 @@ +package evac; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class AmplPrepMain { + public static void main(String[] args) { + // Hardcoded for testing + String arg1 = "models/troy_model/main.csv"; + String arg2 = "models/troy_model/nodes.csv"; + String arg3 = "models/troy_model/edges.csv"; + String arg4 = "models/troy_model/hurricanes.csv"; + + InputModel inputModel = new InputModel(); + + Translator translator = new Translator(inputModel, arg1, arg2, arg3, arg4); + + if (!translator.translateMain()) { + System.out.println("Error while translating main"); + return; + } + System.out.println("Finished translating main"); + + if (!translator.translateNodes()) { + System.out.println("Error while translating nodes"); + return; + } + System.out.println("Finished translating nodes"); + + if (!translator.translateEdges()) { + System.out.println("Error while translating edges"); + return; + } + System.out.println("Finished translating edges"); + + if (!translator.translateHurricanes()) { + System.out.println("Error while translating hurricanes"); + } + System.out.println("Finished translating hurricanes"); + + List evac = new ArrayList<>(); + evac.add(inputModel.nodes.get("Troy")); // troy + evac.add(inputModel.nodes.get("Brunswick")); // brunswick + + List dest = new ArrayList<>(); + dest.add(inputModel.nodes.get("Albany")); + dest.add(inputModel.nodes.get("Guilderland")); + dest.add(inputModel.nodes.get("EastGreenbush")); + dest.add(inputModel.nodes.get("Schenectady")); + + System.out.println("evac"); + for (Node node : evac) { + System.out.println(" " + node.name); + } + + System.out.println("dest"); + for (Node node : dest) { + System.out.println(" " + node.name); + } + + String ampl = AmplGenerator.createData(inputModel, evac, dest); + + File amplDataFile = new File("./ampl/ampl_model.dat"); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(amplDataFile))) { + writer.write(ampl); + } catch (IOException e) { + e.printStackTrace(); + } + + System.out.println("data output:"); + System.out.println(ampl); + + } +} diff --git a/src/evac/KmlGenerator.java b/src/evac/KmlGenerator.java index ff04cb7..ea85788 100644 --- a/src/evac/KmlGenerator.java +++ b/src/evac/KmlGenerator.java @@ -123,7 +123,7 @@ public class KmlGenerator { builder.append(String.format(" Direction%f\u00b0 @ %f mph\n", hurricaneData.velocity.inclination, hurricaneData.velocity.speed)); builder.append(String.format(" Wind Speed%f mph\n", hurricaneData.wind)); //builder.append(String.format(" Wind Gusts%f mph\n", hurricaneData.gusts)); - builder.append(String.format(" Category%d\n", hurricaneData.category)); + builder.append(String.format(" Category%f\n", hurricaneData.category)); builder.append(" \n"); builder.append(" ]]>\n"); builder.append(" \n"); diff --git a/src/evac/Main.java b/src/evac/Main.java index 362fc02..e729529 100755 --- a/src/evac/Main.java +++ b/src/evac/Main.java @@ -1,6 +1,8 @@ package evac; import java.time.Instant; +import java.util.ArrayList; +import java.util.List; public class Main { @@ -48,6 +50,30 @@ public class Main { } System.out.println("Finished translating hurricanes"); + List evac = new ArrayList<>(); + evac.add(inputModel.nodes.get("Troy")); // troy + evac.add(inputModel.nodes.get("Brunswick")); // brunswick + + List dest = new ArrayList<>(); + dest.add(inputModel.nodes.get("Albany")); + dest.add(inputModel.nodes.get("Guilderland")); + dest.add(inputModel.nodes.get("EastGreenbush")); + dest.add(inputModel.nodes.get("Schenectady")); + + System.out.println("evac"); + for (Node node : evac) { + System.out.println(node.name); + } + + System.out.println("dest"); + for (Node node : dest) { + System.out.println(node.name); + } + + + String ampl = AmplGenerator.createData(inputModel, evac, dest); + System.out.println(ampl); + // Start simulation OutputModel outputModel = NumberCruncher.crunch(inputModel); String kml = KmlGenerator.createKml(outputModel);