From 8e9686e01d9b69183a2e53e72a02c96712385eb4 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 18 Feb 2009 10:09:32 +0000 Subject: [PATCH] added option to make random seed automatically generated at simulation load + a single random generator instance is used instead of sharing seed between different parts of the simulator --- tools/cooja/java/se/sics/cooja/GUI.java | 15 +++---- .../cooja/java/se/sics/cooja/Simulation.java | 45 ++++++++++++++++--- .../sics/cooja/dialogs/CreateSimDialog.java | 40 +++++++++++++++-- 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index 51addc532..ac140f850 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: GUI.java,v 1.102 2009/02/08 18:33:05 fros4943 Exp $ + * $Id: GUI.java,v 1.103 2009/02/18 10:09:32 fros4943 Exp $ */ package se.sics.cooja; @@ -592,7 +592,7 @@ public class GUI extends Observable { JMenuItem menuItem2 = new JMenuItem("same random seed"); menuItem2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - reloadCurrentSimulation(false, false); + reloadCurrentSimulation(false, getSimulation().getRandomSeed()); } }); menuItem.add(menuItem2); @@ -600,7 +600,7 @@ public class GUI extends Observable { menuItem2 = new JMenuItem("new random seed"); menuItem2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - reloadCurrentSimulation(false, true); + reloadCurrentSimulation(false, getSimulation().getRandomSeed()+1); } }); menuItem.add(menuItem2); @@ -2389,7 +2389,7 @@ public class GUI extends Observable { * @param autoStart Start executing simulation when loaded * @param newSeed Change simulation seed */ - public void reloadCurrentSimulation(final boolean autoStart, final boolean newSeed) { + public void reloadCurrentSimulation(final boolean autoStart, final long randomSeed) { if (getSimulation() == null) { logger.fatal("No simulation to reload"); return; @@ -2402,9 +2402,6 @@ public class GUI extends Observable { /* Get current simulation configuration */ Element root = new Element("simconf"); Element simulationElement = new Element("simulation"); - if (newSeed) { - getSimulation().setRandomSeed(getSimulation().getRandomSeed() + 1); - } simulationElement.addContent(getSimulation().getConfigXML()); root.addContent(simulationElement); @@ -2421,6 +2418,8 @@ public class GUI extends Observable { myGUI.doRemoveSimulation(false); Simulation newSim = loadSimulationConfig(root, true); myGUI.setSimulation(newSim); + myGUI.getSimulation().setRandomSeed(randomSeed); + if (autoStart) { newSim.startSimulation(); } @@ -2486,7 +2485,7 @@ public class GUI extends Observable { * @param autoStart Start executing simulation when loaded */ public void reloadCurrentSimulation(boolean autoStart) { - reloadCurrentSimulation(autoStart, false); + reloadCurrentSimulation(autoStart, getSimulation().getRandomSeed()); } /** diff --git a/tools/cooja/java/se/sics/cooja/Simulation.java b/tools/cooja/java/se/sics/cooja/Simulation.java index 269b244a7..f9ab181dc 100644 --- a/tools/cooja/java/se/sics/cooja/Simulation.java +++ b/tools/cooja/java/se/sics/cooja/Simulation.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: Simulation.java,v 1.37 2009/01/08 15:42:38 fros4943 Exp $ + * $Id: Simulation.java,v 1.38 2009/02/18 10:09:59 fros4943 Exp $ */ package se.sics.cooja; @@ -74,9 +74,11 @@ public class Simulation extends Observable implements Runnable { private long randomSeed = 123456; + private boolean randomSeedGenerated = false; + private int maxMoteStartupDelay = 1000; - private Random delayMotesRandom = new Random(); + private Random randomGenerator = new Random(); // Tick observable private class TickObservable extends Observable { @@ -276,7 +278,6 @@ public class Simulation extends Observable implements Runnable { */ public Simulation(GUI gui) { myGUI = gui; - delayMotesRandom.setSeed(randomSeed); } /** @@ -342,6 +343,26 @@ public class Simulation extends Observable implements Runnable { */ public void setRandomSeed(long randomSeed) { this.randomSeed = randomSeed; + randomGenerator.setSeed(randomSeed); + logger.info("Simulation random seed: " + randomSeed); + } + + /** + * @param generated Autogenerated random seed at simulation load + */ + public void setRandomSeedGenerated(boolean generated) { + this.randomSeedGenerated = generated; + } + + /** + * @return Autogenerated random seed at simulation load + */ + public boolean getRandomSeedGenerated() { + return randomSeedGenerated; + } + + public Random getRandomGenerator() { + return randomGenerator; } /** @@ -386,7 +407,11 @@ public class Simulation extends Observable implements Runnable { // Random seed element = new Element("randomseed"); - element.setText(Long.toString(randomSeed)); + if (randomSeedGenerated) { + element.setText("generated"); + } else { + element.setText(Long.toString(getRandomSeed())); + } config.add(element); // Max mote startup delay @@ -467,8 +492,12 @@ public class Simulation extends Observable implements Runnable { // Random seed if (element.getName().equals("randomseed")) { - randomSeed = Long.parseLong(element.getText()); - delayMotesRandom.setSeed(randomSeed); + if (element.getText().equals("generated")) { + randomSeedGenerated = true; + setRandomSeed(new Random().nextLong()); + } else { + setRandomSeed(Long.parseLong(element.getText())); + } } // Max mote startup delay @@ -602,7 +631,9 @@ public class Simulation extends Observable implements Runnable { } if (maxMoteStartupDelay > 0 && mote.getInterfaces().getClock() != null) { - mote.getInterfaces().getClock().setDrift(-delayMotesRandom.nextInt(maxMoteStartupDelay)); + mote.getInterfaces().getClock().setDrift( + -randomGenerator.nextInt(maxMoteStartupDelay) + ); } currentRadioMedium.registerMote(mote, this); diff --git a/tools/cooja/java/se/sics/cooja/dialogs/CreateSimDialog.java b/tools/cooja/java/se/sics/cooja/dialogs/CreateSimDialog.java index f50e87594..8827885af 100644 --- a/tools/cooja/java/se/sics/cooja/dialogs/CreateSimDialog.java +++ b/tools/cooja/java/se/sics/cooja/dialogs/CreateSimDialog.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: CreateSimDialog.java,v 1.14 2008/12/04 14:03:42 joxe Exp $ + * $Id: CreateSimDialog.java,v 1.15 2009/02/18 10:10:47 fros4943 Exp $ */ package se.sics.cooja.dialogs; @@ -34,8 +34,10 @@ package se.sics.cooja.dialogs; import java.awt.*; import java.awt.event.*; import java.text.*; +import java.util.Random; import java.util.Vector; import javax.swing.*; + import org.apache.log4j.Logger; import se.sics.cooja.*; @@ -61,6 +63,7 @@ public class CreateSimDialog extends JDialog { private JFormattedTextField delayTime, simulationTime, tickTime; private JFormattedTextField randomSeed, tickLists, delayedStartup; + private JCheckBox randomSeedGenerated; private JTextField title; private JComboBox radioMediumBox; @@ -146,7 +149,14 @@ public class CreateSimDialog extends JDialog { } // Set random seed - myDialog.randomSeed.setValue(new Long(simulationToConfigure.getRandomSeed())); + if (simulationToConfigure.getRandomSeedGenerated()) { + myDialog.randomSeedGenerated.setSelected(true); + myDialog.randomSeed.setEnabled(false); + myDialog.randomSeed.setText("[autogenerated]"); + } else { + myDialog.randomSeed.setEnabled(true); + myDialog.randomSeed.setValue(new Long(simulationToConfigure.getRandomSeed())); + } // Set delayed mote startup time myDialog.delayedStartup.setValue(new Integer(simulationToConfigure.getDelayedMoteStartupTime())); @@ -375,10 +385,25 @@ public class CreateSimDialog extends JDialog { numberField.setColumns(4); randomSeed = numberField; + randomSeedGenerated = new JCheckBox(); + randomSeedGenerated.setToolTipText("Autmatically generate random seed at simulation load"); + randomSeedGenerated.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (((JCheckBox)e.getSource()).isSelected()) { + randomSeed.setEnabled(false); + randomSeed.setText("[autogenerated]"); + } else { + randomSeed.setEnabled(true); + randomSeed.setValue(new Integer(123456)); + } + } + + }); horizBox.add(label); horizBox.add(Box.createHorizontalStrut(150)); horizBox.add(numberField); - horizBox.setToolTipText("Main random seed. Determines mote tick order, mote startup delay etc."); + horizBox.add(randomSeedGenerated); + horizBox.setToolTipText("Simulation random seed. Controls the random behavior such as mote startup delays, node positions etc."); advancedBox.add(horizBox); advancedBox.add(Box.createVerticalStrut(5)); @@ -422,7 +447,14 @@ public class CreateSimDialog extends JDialog { } } - mySimulation.setRandomSeed(((Number) randomSeed.getValue()).longValue()); + if (randomSeedGenerated.isSelected()) { + mySimulation.setRandomSeedGenerated(true); + mySimulation.setRandomSeed(new Random().nextLong()); + } else { + mySimulation.setRandomSeedGenerated(false); + mySimulation.setRandomSeed(((Number) randomSeed.getValue()).longValue()); + } + mySimulation.setDelayedMoteStartupTime(((Number) delayedStartup.getValue()).intValue()); dispose();