private void init() { double[][] cdf = new double[2][m + 1]; double[][] density = new double[2][m + 1]; double h = (b - a) / m; double x; int coex = 0; try { for (int i = 0; i <= m; i++) { x = a + i * h; cdf[0][i] = x; cdf[1][i] = dist.cdf(x); } cdfChart = new XYLineChart("cdf: " + dist.toString(), "", "", cdf); } catch (UnsupportedOperationException e) { coex++; System.err.println(e); // e.printStackTrace(); } try { for (int i = 0; i <= m; i++) { x = a + i * h; density[0][i] = x; density[1][i] = dist.density(x); } densityChart = new XYLineChart("density: " + dist.toString(), "", "", density); } catch (UnsupportedOperationException e) { System.err.println(e); if (coex == 1) throw e; } cdfChart.setprobFlag(true); densityChart.setprobFlag(true); }
Main init() { // Create a chart to monitor the infection progress rate final XYLineChart chart = new XYLineChart("Infection Rate", 5.0, "Infected Nodes (%)", "time(s)"); chart.setYRange(false, 0, 100); chart.setSeriesLinesAndShapes("s0", true, true); Gui.setFrameRectangle("MainFrame", 0, 0, 480, 480); Gui.setFrameRectangle("Infection Rate", 484, 0, 480, 480); // Create the simulation nodes for (int i = 0; i < TOTAL_NODES; i++) new Node(); // Initialize the simulation nodes for (Node i : NodeDB.nodes()) i.init(); NodeDB.randomNode().infect(); // Sets up a periodic task that, at one second intervals, computes and shows the percentage of // infected nodes in the system // Stops the simulation when it detects that every node is infected... new PeriodicTask(1.0) { public void run() { double T = 0, N = 0; for (Node n : NodeDB.nodes()) { if (n.infected) T++; N++; } chart.getSeries("s0").add(Simulation.currentTime(), 100.0 * T / N); if (N == T) stop(); }; }; // From time to time, select a random node to go fail and go offline... new Task(0) { public void run() { NodeDB.randomNode().crash(); reSchedule(0.5 + 0.5 * rg.nextDouble()); // schedules a new execution of this task... } }; // From time to time, create a new node. If the rate of births and deaths is the same, // the size of the system should stay constant on average. new Task(0) { public void run() { new Node().init(); reSchedule(0.5 + 0.5 * rg.nextDouble()); // schedules a new execution of this task... } }; super.setSimulationMaxTimeWarp(2.0); return this; }
/** * Similar to #toLatexCdf, but for the probability density instead of the cdf. * * @param width Chart’s width in centimeters * @param height Chart’s height in centimeters * @return LaTeX source code */ public String toLatexDensity(int width, int height) { return densityChart.toLatex(width, height); }
/** * Exports a chart of the cdf to a LaTeX source code using PGF/TikZ. This method constructs and * returns a string that can be written to a LaTeX document to render the plot. `width` and * `height` represents the width and the height of the produced chart. These dimensions do not * take into account the axes and labels extra space. The `width` and the `height` of the chart * are measured in centimeters. * * @param width Chart’s width in centimeters * @param height Chart’s height in centimeters * @return LaTeX source code */ public String toLatexCdf(int width, int height) { return cdfChart.toLatex(width, height); }
/** * Similar to #viewCdf, but for the probability density instead of the cdf. * * @param width frame width in pixels * @param height frame height in pixels * @return frame containing the chart */ public JFrame viewDensity(int width, int height) { return densityChart.view(width, height); }
/** * Displays a chart of the cumulative distribution function (cdf) on the screen using Swing. This * method creates an application containing a chart panel displaying the chart. The created frame * is positioned on-screen, and displayed before it is returned. The `width` and the `height` of * the chart are measured in pixels. * * @param width frame width in pixels * @param height frame height in pixels * @return frame containing the chart */ public JFrame viewCdf(int width, int height) { return cdfChart.view(width, height); }