View Javadoc
1   package nl.tudelft.simulation.examples.dsol.terminal;
2   
3   import java.rmi.RemoteException;
4   
5   import javax.naming.NamingException;
6   
7   import org.djutils.event.Event;
8   import org.djutils.event.EventListener;
9   
10  import nl.tudelft.simulation.dsol.SimRuntimeException;
11  import nl.tudelft.simulation.dsol.experiment.Replication;
12  import nl.tudelft.simulation.dsol.experiment.SingleReplication;
13  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
14  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterInteger;
15  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
16  import nl.tudelft.simulation.dsol.simulators.DevsSimulator;
17  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
18  
19  /**
20   * <p>
21   * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
23   * project is distributed under a three-clause BSD-style license, which can be found at
24   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
25   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
26   * </p>
27   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs</a>
28   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   */
30  public class ExperimentRunnerTerminal implements EventListener
31  {
32      /** */
33      private static final long serialVersionUID = 1L;
34  
35      /** number of running simulations. */
36      private int numruns = 0;
37  
38      /** number of completed simulations. */
39      private int completed = 0;
40  
41      /** number of runs. */
42      private static final int REPS = 100;
43  
44      /** number of runs. */
45      private static final int RUNS = 3 * 4 * REPS;
46  
47      /**
48       * Construct the terminal experiment.
49       * @throws SimRuntimeException on error
50       * @throws RemoteException on error
51       * @throws NamingException on error
52       * @throws InputParameterException on error
53       */
54      private ExperimentRunnerTerminal() throws SimRuntimeException, RemoteException, NamingException, InputParameterException
55      {
56          long seed = 1;
57          int maxConcurrent = 8;
58          for (int numQC = 4; numQC <= 6; numQC++)
59          {
60              for (int numAGV = 25; numAGV <= 28; numAGV++)
61              {
62                  for (int rep = 1; rep <= REPS; rep++)
63                  {
64                      while (this.numruns > maxConcurrent)
65                      {
66                          try
67                          {
68                              Thread.sleep(1);
69                          }
70                          catch (InterruptedException exception)
71                          {
72                              //
73                          }
74                      }
75  
76                      double runtime = 40 * 60;
77                      DevsSimulator<Double> simulator = new DevsSimulator<Double>("ExperimentRunnerTerminal");
78                      Terminal model = new Terminal(simulator, rep);
79                      Replication<Double> replication = new SingleReplication<Double>("rep1", 0.0, 0.0, runtime);
80                      model.getStreams().put("default", new MersenneTwister(seed++));
81                      InputParameterMap parameters = model.getInputParameterMap();
82                      ((InputParameterInteger) parameters.get("numQC")).setIntValue(numQC);
83                      ((InputParameterInteger) parameters.get("numAGV")).setIntValue(numAGV);
84                      simulator.initialize(model, replication);
85                      model.addListener(this, Terminal.READY_EVENT);
86                      this.numruns++;
87                      simulator.start();
88                      simulator.scheduleEventAbs(runtime - 0.00001, this, "terminate", new Object[] {simulator, numQC, numAGV, rep, model});
89                  }
90              }
91          }
92      }
93  
94      /**
95       * @param simulator DevsSimulator&lt;Double&gt;; the simulator
96       * @param numQC int; num QC
97       * @param numAGV int; num AGV
98       * @param rep int; replication number
99       * @param model Terminal; the model
100      * @throws SimRuntimeException on error
101      * @throws RemoteException on error
102      */
103     public synchronized void terminate(final DevsSimulator<Double> simulator, final int numQC, final int numAGV,
104             final int rep, final Terminal model) throws SimRuntimeException, RemoteException
105     {
106         simulator.stop();
107         System.out.println(numQC + "\t" + numAGV + "\t" + rep + "\tNaN\tNaN\t40\t" + model.getShip().getContainers());
108         this.numruns--;
109         this.completed++;
110         if (this.completed == RUNS)
111         {
112             System.exit(0);
113         }
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public synchronized void notify(final Event event) throws RemoteException
119     {
120         if (event.getType().equals(Terminal.READY_EVENT))
121         {
122             Terminal.Output output = (Terminal.Output) event.getContent();
123             System.out.println(output.getNumQC() + "\t" + output.getNumAGV() + "\t" + output.getRep() + "\t"
124                     + output.getDelayHours() + "\t" + output.getCosts() + "\t" + output.getReady() + "\t3000");
125             this.numruns--;
126             this.completed++;
127             if (this.completed == RUNS)
128             {
129                 System.exit(0);
130             }
131         }
132     }
133 
134     /**
135      * @param args String[]; args
136      * @throws SimRuntimeException on error
137      * @throws RemoteException on error
138      * @throws NamingException on error
139      * @throws InputParameterException on error
140      */
141     public static void main(final String[] args)
142             throws SimRuntimeException, RemoteException, NamingException, InputParameterException
143     {
144         new ExperimentRunnerTerminal();
145     }
146 
147 }