import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; /** * Created with IntelliJ IDEA. * User: rebeccamancy * Date: 10/01/2013 * Time: 15:18 * To change this template use File | Settings | File Templates. */ public class Exper_PatchSizeHetero_v01 { HashMap configMap; // Hashmap containing configuration parameters // Input-output properties Boolean display, trace, headers; String inputFolder, outputFolder; String outputQSDFile, outputExtinctionTimesFile; int landscapeID; String distribution; // QSD-related properties int numSeqs, timeWindowLength, numDraws, maxIndexed; double rhatThreshold; // Simulation-related properties int maxTime, numReps, numPaths, numLandscapes; String outputType; double eParam, cParam, alpha, delta; // Other properties of the experiment int numPatches; Point minXY, maxXY; Point[] space; OH2000Model mod; Habitat hab; Ecology eco; /** * Constructs an experiment * @param configLocation * @param numPatches * @param landscapeID * @param distribution */ public Exper_PatchSizeHetero_v01(String configLocation, String numPatches, String landscapeID, String distribution) { // Read in from configuration file configMap = Util.makeConfigMap(configLocation); // Input-output parameters display = Boolean.parseBoolean(configMap.get("DISPLAY")); trace = Boolean.parseBoolean(configMap.get("TRACE")); headers = Boolean.parseBoolean(configMap.get("HEADERS")); inputFolder = configMap.get("INPUT_FOLDER"); outputFolder = configMap.get("OUTPUT_FOLDER"); // QSD-related properties numSeqs = Integer.parseInt(configMap.get("NUM_SEQS")); timeWindowLength = Integer.parseInt(configMap.get("TIME_WINDOW_LENGTH")); numDraws = Integer.parseInt(configMap.get("NUM_DRAWS")); rhatThreshold = Double.parseDouble(configMap.get("RHAT_THRESHOLD")); maxIndexed = Integer.parseInt(configMap.get("MAX_INDEXED")); // Simulation-related properties maxTime = Integer.parseInt(configMap.get("MAX_TIME")); numReps = Integer.parseInt(configMap.get("NUM_REPS")); numPaths = Integer.parseInt(configMap.get("NUM_PATHS")); numLandscapes = Integer.parseInt(configMap.get("NUM_LANDSCAPES")); outputType = configMap.get("OUTPUT_TYPE"); eParam = Double.parseDouble(configMap.get("eParam")); cParam = Double.parseDouble(configMap.get("cParam")); alpha = Double.parseDouble(configMap.get("alpha")); delta = eParam/cParam; this.numPatches = Integer.parseInt(numPatches); // Setting things up String inputFile = inputFolder + "/landscapes/n" + numPatches + "_ls" + landscapeID + "_" + distribution + ".csv"; outputQSDFile = outputFolder + "/qsdists/n" + numPatches + "_ls" + landscapeID + "_" + distribution; this.landscapeID = Integer.parseInt(landscapeID); this.distribution = distribution; hab = new LandscapeHabitat(inputFile, this.numPatches); numReps = Integer.parseInt(configMap.get("NUM_REPS")); } /** * Runs an experiment ... * @param args */ public static void main(String args[]) { ///----------- FIRST MAKE THE QSD ------------/// // Construct an experiment from .properties file and landscape from filename given by args[1]=numPatches; args[2]=landscapeID; args[3]=distribution System.out.println(args[0] + args[1] + args[2] + args[3]); Exper_PatchSizeHetero_v01 ex = new Exper_PatchSizeHetero_v01(args[0], args[1], args[2], args[3]); // Make a model and an ecology based on parameters file ex.mod = new OH2000Model(ex.eParam, ex.cParam, ex.alpha); ex.eco = new Ecology(ex.hab, ex.mod); // has LambdaM as a property // Output lambda M System.out.println("LambdaM = " + ex.eco.getLambdaM()); System.out.println("e/c = " + ex.eParam/ex.cParam); System.out.println(ex.eco.toString()); // Construct a cluster of simulators and run each to QSD (using e and c from parameters file) SimCluster qsdSimCluster = new SimCluster(ex.numSeqs, ex.numPatches, ex.eco, ex.numDraws, ex.timeWindowLength, ex.maxIndexed); qsdSimCluster.run2QSD(ex.maxIndexed, ex.rhatThreshold, ex.trace); // Save estimated qsd of the first simulator to the Ecology ex.eco.setQsd(qsdSimCluster.getFirstSim().getEstQSD()); System.out.println("main: Total time: " + qsdSimCluster.getFirstSim().getTime()); // Write out qsd String f = ex.outputQSDFile + "_" + ex.cParam + "_" + ex.eParam + ".csv"; ex.eco.getQsd().write(f); ///----------- SIMULATE TO EXTINCTION FROM THE QSD ------------/// SimCluster extTimesSimCluster = new SimCluster(ex.numReps, ex.numPaths, ex.eco, ex.numDraws, ex.maxTime, ex.eco.getQsd()); extTimesSimCluster.run2extinction(ex.trace); // Write out extinction times to a file String f2 = ex.outputFolder + "/extinctiontimes/n" + ex.numPatches + "_ls" + ex.landscapeID + "_" + ex.distribution + "_" + ex.cParam + "_" + ex.eParam + ".csv"; System.out.println(f2); write(f2, Util.toStringCSV(extTimesSimCluster.getExtinctionTimes())); // Write out paths to a file String f3 = ex.outputFolder + "/extpaths/n" + ex.numPatches + "_ls" + ex.landscapeID + "_" + ex.distribution + "_" + ex.cParam + "_" + ex.eParam + ".csv"; System.out.println(f3); write(f3, Util.toStringCSV(extTimesSimCluster.getExtPaths())); // Only 101 rows of output for this ... else too slow } /** * Helper method to write out a string to a file * @param f filename and path * @param output output string */ private static void write(String f, String output) { PrintWriter outputWriter = null; try { outputWriter = new PrintWriter(f); } // Catch file opening errors and close files catch (IOException e) { System.out.println("Error opening file: " + e); System.exit(1); } try { outputWriter.println(output); } catch (Exception e) { System.out.println("Error processing file: " + f + " Error: " + e); System.exit(1); } finally { if (outputWriter != null) outputWriter.close(); } } }