001/* An actor that outputs a random sequence with a Exponential distribution. 002 003 Copyright (c) 2004-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.actor.lib.colt; 029 030import cern.jet.random.Exponential; 031import ptolemy.actor.parameters.PortParameter; 032import ptolemy.data.BooleanToken; 033import ptolemy.data.DoubleToken; 034import ptolemy.data.expr.SingletonParameter; 035import ptolemy.data.type.BaseType; 036import ptolemy.kernel.CompositeEntity; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039 040/////////////////////////////////////////////////////////////////// 041//// Exponential 042 043/** 044 Produce a random sequence with a Exponential distribution. On each 045 iteration, a new random number is produced. The output port is of 046 type DoubleToken. The values that are generated are independent 047 and identically distributed with the lambda and the standard 048 deviation given by parameters. In addition, the seed can be 049 specified as a parameter to control the sequence that is generated. 050 051 <p> This actor instantiates a 052 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/random/Exponential.html">cern.jet.random.Exponential</a> object with 053 lambda set to 1.0. The distribution is defined as 054 <pre> 055 p(x) = lambda*exp(-x*lambda) for x ≥ 0, lambda > 0 056 </pre> 057 The above description of Exponential is 058 <a href="doc-files/colt-copyright.htm">copyrighted</a>. 059 Note that the mean is 1/lambda, and the standard deviation is 1/lambda^2. 060 061 <p>A definition of the Exponential distribution can be found at 062 <a href="http://www.cern.ch/RD11/rkb/AN16pp/node78.html#SECTION000780000000000000000"><code>http://www.cern.ch/RD11/rkb/AN16pp/node78.html#SECTION000780000000000000000</code></a> 063 064 @author David Bauer and Kostas Oikonomou (contributor: Edward A. Lee) 065 @version $Id$ 066 @since Ptolemy II 4.1 067 @Pt.ProposedRating Red (cxh) 068 @Pt.AcceptedRating Red (cxh) 069 */ 070public class ColtExponential extends ColtRandomSource { 071 /** Construct an actor with the given container and name. 072 * @param container The container. 073 * @param name The name of this actor. 074 * @exception IllegalActionException If the actor cannot be contained 075 * by the proposed container. 076 * @exception NameDuplicationException If the container already has an 077 * actor with this name. 078 */ 079 public ColtExponential(CompositeEntity container, String name) 080 throws NameDuplicationException, IllegalActionException { 081 super(container, name); 082 083 output.setTypeEquals(BaseType.DOUBLE); 084 085 lambda = new PortParameter(this, "lambda", new DoubleToken(1.0)); 086 lambda.setTypeEquals(BaseType.DOUBLE); 087 new SingletonParameter(lambda.getPort(), "_showName") 088 .setToken(BooleanToken.TRUE); 089 090 lambda.moveToFirst(); 091 } 092 093 /////////////////////////////////////////////////////////////////// 094 //// ports and parameters //// 095 096 /** The mean value of the exponential. 097 * This is a double with default value 1.0. 098 */ 099 public PortParameter lambda; 100 101 /////////////////////////////////////////////////////////////////// 102 //// public methods //// 103 104 /** Send a random number with a Exponential distribution to the output. 105 * This number is only changed in the prefire() method, so it will 106 * remain constant throughout an iteration. 107 * @exception IllegalActionException If there is no director. 108 */ 109 @Override 110 public void fire() throws IllegalActionException { 111 lambda.update(); 112 super.fire(); 113 output.send(0, new DoubleToken(_current)); 114 } 115 116 /////////////////////////////////////////////////////////////////// 117 //// protected methods //// 118 119 /** Method that is called after _randomNumberGenerator is changed. 120 */ 121 @Override 122 protected void _createdNewRandomNumberGenerator() { 123 _generator = new Exponential(1.0, _randomNumberGenerator); 124 } 125 126 /** Generate a new random number. 127 * @exception IllegalActionException If parameter values are incorrect. 128 */ 129 @Override 130 protected void _generateRandomNumber() throws IllegalActionException { 131 double lambdaValue = ((DoubleToken) lambda.getToken()).doubleValue(); 132 _current = _generator.nextDouble(lambdaValue); 133 } 134 135 /////////////////////////////////////////////////////////////////// 136 //// private variables //// 137 138 /** The random number for the current iteration. */ 139 private double _current; 140 141 /** The random number generator. */ 142 private Exponential _generator; 143}