View Javadoc
1   package org.sim0mq.demo.mm1;
2   
3   import java.io.Serializable;
4   import java.rmi.RemoteException;
5   
6   import nl.tudelft.simulation.dsol.SimRuntimeException;
7   import nl.tudelft.simulation.dsol.formalisms.Resource;
8   import nl.tudelft.simulation.dsol.formalisms.flow.Delay;
9   import nl.tudelft.simulation.dsol.formalisms.flow.Generator;
10  import nl.tudelft.simulation.dsol.formalisms.flow.Release;
11  import nl.tudelft.simulation.dsol.formalisms.flow.Seize;
12  import nl.tudelft.simulation.dsol.formalisms.flow.StationInterface;
13  import nl.tudelft.simulation.dsol.formalisms.flow.statistics.Utilization;
14  import nl.tudelft.simulation.dsol.model.AbstractDSOLModel;
15  import nl.tudelft.simulation.dsol.simtime.SimTimeDouble;
16  import nl.tudelft.simulation.dsol.simtime.dist.DistContinuousSimTime;
17  import nl.tudelft.simulation.dsol.simtime.dist.DistContinuousSimulationTime;
18  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
19  import nl.tudelft.simulation.dsol.statistics.SimTally;
20  import nl.tudelft.simulation.jstats.distributions.DistConstant;
21  import nl.tudelft.simulation.jstats.distributions.DistDiscreteConstant;
22  import nl.tudelft.simulation.jstats.distributions.DistExponential;
23  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
24  import nl.tudelft.simulation.jstats.streams.StreamInterface;
25  
26  /**
27   * The M/M/1 example as published in Simulation Modeling and Analysis by A.M. Law & W.D. Kelton section 1.4 and 2.4.
28   * <p>
29   * (c) copyright 2015-2020 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the Netherlands. <br>
30   * See for project information <a href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL) </a>, no warranty.
32   * @version 2.0 21.09.2003 <br>
33   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
34   */
35  public class MM1Queue41Model extends AbstractDSOLModel.TimeDouble<DEVSSimulatorInterface.TimeDouble>
36  {
37      /** The default serial version UID for serializable classes. */
38      private static final long serialVersionUID = 1L;
39  
40      /** tally dN. */
41      @SuppressWarnings("checkstyle:visibilitymodifier")
42      SimTally.TimeDouble dN;
43  
44      /** tally qN. */
45      @SuppressWarnings("checkstyle:visibilitymodifier")
46      SimTally.TimeDouble qN;
47  
48      /** utilization uN. */
49      @SuppressWarnings("checkstyle:visibilitymodifier")
50      Utilization<Double, Double, SimTimeDouble> uN;
51  
52      /** PARAMETER iat. */
53      @SuppressWarnings("checkstyle:visibilitymodifier")
54      public double iat = Double.NaN;
55  
56      /** PARAMETER serviceTime. */
57      @SuppressWarnings("checkstyle:visibilitymodifier")
58      public double serviceTime = Double.NaN;
59  
60      /** PARAMETER seed. */
61      @SuppressWarnings("checkstyle:visibilitymodifier")
62      public long seed = 1;
63  
64      /**
65       * Construct the model.
66       * @param simulator the simulator
67       */
68      public MM1Queue41Model(final DEVSSimulatorInterface.TimeDouble simulator)
69      {
70          super(simulator);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final void constructModel() throws SimRuntimeException
76      {
77          if (Double.isNaN(this.iat))
78          {
79              throw new SimRuntimeException("Parameter iat not defined for model");
80          }
81          if (Double.isNaN(this.serviceTime))
82          {
83              throw new SimRuntimeException("Parameter servicetime not defined for model");
84          }
85  
86          StreamInterface defaultStream = new MersenneTwister(this.seed);
87  
88          // The Generator
89          Generator.TimeDouble generator = new Generator.TimeDouble("generator", getSimulator(), Object.class, null);
90          generator.setInterval(new DistContinuousSimulationTime.TimeDouble(new DistExponential(defaultStream, this.iat)));
91          generator.setStartTime(new DistContinuousSimTime.TimeDouble(new DistConstant(defaultStream, 0.0)));
92          generator.setBatchSize(new DistDiscreteConstant(defaultStream, 1));
93          generator.setMaxNumber(1000);
94  
95          // The queue, the resource and the release
96          Resource<Double, Double, SimTimeDouble> resource = new Resource<>(getSimulator(), "resource", 1.0);
97  
98          // created a resource
99          StationInterface.TimeDouble queue = new Seize.TimeDouble("queue", getSimulator(), resource);
100         StationInterface.TimeDouble release = new Release.TimeDouble("release", getSimulator(), resource, 1.0);
101 
102         // The server
103         DistContinuousSimulationTime.TimeDouble serviceTimeDistribution =
104                 new DistContinuousSimulationTime.TimeDouble(new DistExponential(defaultStream, this.serviceTime));
105         StationInterface.TimeDouble server = new Delay.TimeDouble("delay", getSimulator(), serviceTimeDistribution);
106 
107         // The flow
108         generator.setDestination(queue);
109         queue.setDestination(server);
110         server.setDestination(release);
111 
112         // Statistics
113         try
114         {
115             this.dN = new SimTally.TimeDouble("d(n)", getSimulator(), queue, Seize.DELAY_TIME);
116             this.qN = new SimTally.TimeDouble("q(n)", getSimulator(), queue, Seize.QUEUE_LENGTH_EVENT);
117             this.uN = new Utilization<Double, Double, SimTimeDouble>("u(n)", getSimulator(), server);
118         }
119         catch (RemoteException exception)
120         {
121             exception.printStackTrace();
122         }
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public Serializable getSourceId()
128     {
129         return "MM1Queue41Model";
130     }
131 
132 }