package simulation;
import java.io.File;
import java.util.List;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import miscellaneous.CSVList;
import miscellaneous.DirectoryManager;
import recorders.Recorder;
import rtools.R;
import tables.Dim;
import tables.DimTable;
import tables.DimensionMap;
import vsv.VSVModel;
import weka.core.converters.JEXTableWriter;
public class MultiSim {
// //////////////////////
// / Model Parameters ///
// //////////////////////
Parameter p1 = new Parameter(Statics.k_TRANSCRIPTION, "", "1e0");
Parameter p2 = new Parameter(Statics.ratio_TRANSCRIPTION_2_REPLICATION, "", "1e4");// 1e4
Parameter p3 = new Parameter(Statics.ratio_GENOME_2_ANTIGENOME, "(1/2.7 = 0.37)", "0.1");// 0.1
Parameter p4 = new Parameter(Statics.stoich_ERROR, "", "0.001");
Parameter p5 = new Parameter(Statics.k_POL_FORMATION, "", "1e-9");
Parameter p6 = new Parameter(Statics.k_CONDENSATION, "", "1e-10");// 1e-10
Parameter p7 = new Parameter(Statics.k_DETECTION, "", "1e-3");// 1e-3
Parameter p8 = new Parameter(Statics.pol_SPEED, "", "15");// 15
Parameter p9 = new Parameter(Statics.pol_SPACING, "", "400");// 500, 400 is good too
Parameter p10 = new Parameter(Statics.rib_SPEED, "", "12");// 10
Parameter p11 = new Parameter(Statics.rib_SPACING, "", "180");// 238
ParameterSet parameters = new ParameterSet();
// ////////////////////
// / Sim Parameters ///
// ////////////////////
double simDuration = 10; // [hr]
double dt = 5; // [s]
double recorderInterval = 5; // [min]
int numberOfThreads = 4;
// //////////////////////
// / Helper Variables ///
// //////////////////////
DimTable paramTable = new DimTable();
TreeMap<DimensionMap,String> dataPaths = new TreeMap<DimensionMap,String>();
TreeMap<DimensionMap,String> plotPaths = new TreeMap<DimensionMap,String>();
String dataPathsTable = null;
String plotPathsTable = null;
public ExecutorService threader = Executors.newFixedThreadPool(this.numberOfThreads);
public TreeMap<DimensionMap,TestSim> sims = new TreeMap<DimensionMap,TestSim>();
List<Future<Integer>> futures = new Vector<Future<Integer>>();
public volatile boolean canceled = false;
public static void main(String[] args)
{
Recorder.setHostDirectory("/Users/jaywarrick/Desktop/JavaModelOutputs");
// Recorder.setHostDirectory("C:\\Users\\ctimm\\Desktop\\JavaModelOutputs");
R.eval("graphics.off()"); // Call this to close any files that were left
// open during plotting so they can be deleted
// without file permissions issues.
DirectoryManager.deleteContentsOfTempFolder();
MultiSim mSim = new MultiSim();
mSim.startSims();
mSim.waitForSims();
mSim.makePlots(DirectoryManager.getHostDirectory().getAbsolutePath() + File.separator + "plotScript.R");
mSim.shutdown();
}
public MultiSim()
{
// Make an array of the parameters and return it
// parameterArray.addParameter(p0);
this.parameters.addParameter(this.p1);
this.parameters.addParameter(this.p2);
this.parameters.addParameter(this.p3);
this.parameters.addParameter(this.p4);
this.parameters.addParameter(this.p5);
this.parameters.addParameter(this.p6);
this.parameters.addParameter(this.p7);
this.parameters.addParameter(this.p8);
this.parameters.addParameter(this.p9);
this.parameters.addParameter(this.p10);
this.parameters.addParameter(this.p11);
}
// Loop through all the parameter combinations, creating the VSVModels and setting up the
// individual simulations. Then start each sim on it's own thread using the "exectutor service"
// to manage all threads, only running a handful or so at a time (see param numberOfThreads)
// Also, write a file that contains the paths of where we intend to write all the results
public boolean startSims()
{
// Gather parameters
CSVList k_TRANSCRIPTION = new CSVList(this.parameters.getValueOfParameter(Statics.k_TRANSCRIPTION));
CSVList ratio_TRANSCRIPTION_2_REPLICATION = new CSVList(this.parameters.getValueOfParameter(Statics.ratio_TRANSCRIPTION_2_REPLICATION));
CSVList ratio_GENOME_2_ANTIGENOME = new CSVList(this.parameters.getValueOfParameter(Statics.ratio_GENOME_2_ANTIGENOME));
CSVList stoich_ERROR = new CSVList(this.parameters.getValueOfParameter(Statics.stoich_ERROR));
CSVList k_POL_FORMATION = new CSVList(this.parameters.getValueOfParameter(Statics.k_POL_FORMATION));
CSVList k_CONDENSATION = new CSVList(this.parameters.getValueOfParameter(Statics.k_CONDENSATION));
CSVList k_DETECTION = new CSVList(this.parameters.getValueOfParameter(Statics.k_DETECTION));
CSVList pol_SPEED = new CSVList(this.parameters.getValueOfParameter(Statics.pol_SPEED));
CSVList pol_SPACING = new CSVList(this.parameters.getValueOfParameter(Statics.pol_SPACING));
CSVList rib_SPEED = new CSVList(this.parameters.getValueOfParameter(Statics.rib_SPEED));
CSVList rib_SPACING = new CSVList(this.parameters.getValueOfParameter(Statics.rib_SPACING));
this.paramTable = new DimTable();
this.paramTable.add(new Dim(Statics.k_TRANSCRIPTION, k_TRANSCRIPTION));
this.paramTable.add(new Dim(Statics.ratio_TRANSCRIPTION_2_REPLICATION, ratio_TRANSCRIPTION_2_REPLICATION));
this.paramTable.add(new Dim(Statics.ratio_GENOME_2_ANTIGENOME, ratio_GENOME_2_ANTIGENOME));
this.paramTable.add(new Dim(Statics.stoich_ERROR, stoich_ERROR));
this.paramTable.add(new Dim(Statics.k_POL_FORMATION, k_POL_FORMATION));
this.paramTable.add(new Dim(Statics.k_CONDENSATION, k_CONDENSATION));
this.paramTable.add(new Dim(Statics.k_DETECTION, k_DETECTION));
this.paramTable.add(new Dim(Statics.pol_SPEED, pol_SPEED));
this.paramTable.add(new Dim(Statics.pol_SPACING, pol_SPACING));
this.paramTable.add(new Dim(Statics.rib_SPEED, rib_SPEED));
this.paramTable.add(new Dim(Statics.rib_SPACING, rib_SPACING));
for (DimensionMap params : this.paramTable.getMapIterator())
{
// Create the VSVModel to simulate and set the parameters
VSVModel model = new VSVModel();
model.initialize();
model.setParams(params);
// Set up the simulation;
TestSim sim = new TestSim();
sim.setParams(model, this.simDuration * 60 * 60, this.dt, this.recorderInterval * 60);
this.sims.put(params, sim);
// Put it in a thread and run it.
Future<Integer> future = this.threader.submit(sim);
this.futures.add(future);
}
// Create the file that contains the paths/links to all the future outputted data files.
for (DimensionMap map : this.sims.keySet())
{
this.dataPaths.put(map, this.sims.get(map).outputFile.getAbsolutePath());
}
this.dataPathsTable = JEXTableWriter.writeTable("MultiSimData", this.paramTable, this.dataPaths);
String temp = DirectoryManager.getHostDirectory().getAbsolutePath() + File.separator + "data.arff";
Recorder.copyFile(this.dataPathsTable, temp);
this.dataPathsTable = temp;
// Return status
return true;
}
// This loop essentially waits for each thread to return and doesn't finishe until they are all returned.
public void waitForSims()
{
try
{
// Collect outputs and wait for them to finish
@SuppressWarnings("unused")
int done = 0;
for (Future<Integer> future : this.futures)
{
if(this.canceled == true)
{
return;
}
done += future.get();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
public void makePlots(String scriptFilePath)
{
// Prepare R
R.setwd(DirectoryManager.getHostDirectory().getAbsolutePath());
// R.source(scriptFilePath);
R.eval("source(" + R.quotedPath(scriptFilePath) + ")");
// Plot the results
this.plotPaths = new TreeMap<DimensionMap,String>();
for (DimensionMap map : this.paramTable.getMapIterator())
{
String plotPath = DirectoryManager.getUniqueAbsoluteTempPath("jpeg");
if(plotPath != null)
{
R.eval("plotScript(" + R.quotedPath(this.dataPaths.get(map)) + "," + R.quotedPath(plotPath) + ")");
this.plotPaths.put(map, plotPath);
}
}
this.plotPathsTable = JEXTableWriter.writeTable("MultiSimPlots", this.paramTable, this.plotPaths);
String temp = DirectoryManager.getHostDirectory().getAbsolutePath() + File.separator + "plots.arff";
Recorder.copyFile(temp, this.plotPathsTable);
this.plotPathsTable = temp;
}
// This closes down all the threads we used to run the simulations
public void shutdown()
{
this.threader.shutdown();
}
}
class Statics {
public static String k_TRANSCRIPTION = "k_TRANSCRIPTION", ratio_TRANSCRIPTION_2_REPLICATION = "ratio_TRANSCRIPTION_2_REPLICATION", ratio_GENOME_2_ANTIGENOME = "ratio_GENOME_2_ANTIGENOME", stoich_ERROR = "stoich_ERROR",
k_POL_FORMATION = "k_POL_FORMATION", k_CONDENSATION = "k_CONDENSATION", k_DETECTION = "k_DETECTION", pol_SPEED = "pol_SPEED", pol_SPACING = "pol_SPACING", rib_SPEED = "rib_SPEED", rib_SPACING = "rib_SPACING";
}
class Parameter {
public String name, description, value;
public Parameter(String name, String description, String value)
{
this.name = name;
this.description = description;
this.value = value;
}
}
class ParameterSet {
public TreeMap<String,Parameter> parameters = new TreeMap<String,Parameter>();
public void addParameter(Parameter p)
{
this.parameters.put(p.name, p);
}
public String getValueOfParameter(String name)
{
return this.parameters.get(name).value;
}
}