run it my dudes

This commit is contained in:
Michael Peters 2019-11-03 09:57:14 -05:00
parent 3e0beafa55
commit a3a67035d8
9 changed files with 211 additions and 4 deletions

View File

@ -6,5 +6,6 @@
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="include/commons-csv-1.7/commons-csv-1.7.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

20
ampl/ampl_model.dat Normal file
View File

@ -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
;

7
ampl/ampl_solve.run Normal file
View File

@ -0,0 +1,7 @@
model model.mdl;
data ampl_model.dat;
solve;
display Flow;

6
data/flows.csv Normal file
View File

@ -0,0 +1,6 @@
Brunswick,EastGreenbush,300
Guilderland,Schenectady,0
Troy,Albany,200
Troy,Brunswick,0
Troy,EastGreenbush,200
Troy,Guilderland,300
1 Brunswick EastGreenbush 300
2 Guilderland Schenectady 0
3 Troy Albany 200
4 Troy Brunswick 0
5 Troy EastGreenbush 200
6 Troy Guilderland 300

View File

@ -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)

View File

@ -0,0 +1,44 @@
package evac;
import java.util.List;
public class AmplGenerator {
public static String createData(InputModel model, List<Node> evac, List<Node> 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();
}
}

View File

@ -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<Node> evac = new ArrayList<>();
evac.add(inputModel.nodes.get("Troy")); // troy
evac.add(inputModel.nodes.get("Brunswick")); // brunswick
List<Node> 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);
}
}

View File

@ -123,7 +123,7 @@ public class KmlGenerator {
builder.append(String.format(" <tr><td>Direction</td><td>%f\u00b0 @ %f mph</td></tr>\n", hurricaneData.velocity.inclination, hurricaneData.velocity.speed));
builder.append(String.format(" <tr><td>Wind Speed</td><td>%f mph</td></tr>\n", hurricaneData.wind));
//builder.append(String.format(" <tr><td>Wind Gusts</td><td>%f mph</td></tr>\n", hurricaneData.gusts));
builder.append(String.format(" <tr><td>Category</td><td>%d</td></tr>\n", hurricaneData.category));
builder.append(String.format(" <tr><td>Category</td><td>%f</td></tr>\n", hurricaneData.category));
builder.append(" </table>\n");
builder.append(" ]]></description>\n");
builder.append(" <Point>\n");

View File

@ -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<Node> evac = new ArrayList<>();
evac.add(inputModel.nodes.get("Troy")); // troy
evac.add(inputModel.nodes.get("Brunswick")); // brunswick
List<Node> 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);