View Javadoc
1   package org.sim0mq.demo.mm1;
2   
3   import java.rmi.RemoteException;
4   
5   import nl.tudelft.simulation.dsol.DSOLModel;
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.simtime.SimTimeDouble;
15  import nl.tudelft.simulation.dsol.simtime.dist.DistContinuousSimTime;
16  import nl.tudelft.simulation.dsol.simtime.dist.DistContinuousTime;
17  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19  import nl.tudelft.simulation.dsol.statistics.Tally;
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 2002-2016 <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 implements DSOLModel.TimeDouble
36  {
37      /** The default serial version UID for serializable classes. */
38      private static final long serialVersionUID = 1L;
39  
40      /** the simulator. */
41      private DEVSSimulatorInterface.TimeDouble devsSimulator;
42  
43      /** tally dN. */
44      @SuppressWarnings("checkstyle:visibilitymodifier")
45      Tally<Double, Double, SimTimeDouble> dN;
46  
47      /** tally qN. */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      Tally<Double, Double, SimTimeDouble> qN;
50  
51      /** utilization uN. */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      Utilization uN;
54  
55      /** PARAMETER iat. */
56      public double iat = Double.NaN;
57  
58      /** PARAMETER serviceTime. */
59      public double serviceTime = Double.NaN;
60      
61      /** PARAMETER seed. */
62      public long seed = 1;
63      
64  
65      /** {@inheritDoc} */
66      @Override
67      public final void constructModel(final SimulatorInterface<Double, Double, SimTimeDouble> simulator)
68              throws SimRuntimeException, RemoteException
69      {
70          if (Double.isNaN(this.iat))
71          {
72              throw new SimRuntimeException("Parameter iat not defined for model");
73          }
74          if (Double.isNaN(this.serviceTime))
75          {
76              throw new SimRuntimeException("Parameter servicetime not defined for model");
77          }
78  
79          this.devsSimulator = (DEVSSimulatorInterface.TimeDouble) simulator;
80          StreamInterface defaultStream = new MersenneTwister(this.seed);
81  
82          // The Generator
83          Generator.TimeDouble generator = new Generator.TimeDouble(this.devsSimulator, Object.class, null);
84          generator.setInterval(new DistContinuousTime.TimeDouble(new DistExponential(defaultStream, this.iat)));
85          generator.setStartTime(new DistContinuousSimTime.TimeDouble(new DistConstant(defaultStream, 0.0)));
86          generator.setBatchSize(new DistDiscreteConstant(defaultStream, 1));
87          generator.setMaxNumber(1000);
88  
89          // The queue, the resource and the release
90          Resource<Double, Double, SimTimeDouble> resource = new Resource<>(this.devsSimulator, 1.0);
91  
92          // created a resource
93          StationInterface queue = new Seize.TimeDouble(this.devsSimulator, resource);
94          StationInterface release = new Release.TimeDouble(this.devsSimulator, resource, 1.0);
95  
96          // The server
97          DistContinuousTime.TimeDouble serviceTimeDistribution =
98                  new DistContinuousTime.TimeDouble(new DistExponential(defaultStream, this.serviceTime));
99          StationInterface server = new Delay.TimeDouble(this.devsSimulator, serviceTimeDistribution);
100 
101         // The flow
102         generator.setDestination(queue);
103         queue.setDestination(server);
104         server.setDestination(release);
105 
106         // Statistics
107         this.dN = new Tally<>("d(n)", this.devsSimulator, queue, Seize.DELAY_TIME);
108         this.qN = new Tally<>("q(n)", this.devsSimulator, queue, Seize.QUEUE_LENGTH_EVENT);
109         this.uN = new Utilization("u(n)", this.devsSimulator, server);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public final SimulatorInterface.TimeDouble getSimulator()
115     {
116         return this.devsSimulator;
117     }
118 }