001/* An actor that outputs a random sequence with a Poisson 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.Poisson; 031import ptolemy.actor.parameters.PortParameter; 032import ptolemy.data.BooleanToken; 033import ptolemy.data.DoubleToken; 034import ptolemy.data.IntToken; 035import ptolemy.data.expr.SingletonParameter; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.CompositeEntity; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040 041/////////////////////////////////////////////////////////////////// 042//// Poisson 043 044/** 045 Produce a random sequence with a Poisson distribution. On each 046 iteration, a new random number is produced. The output port is of 047 type double. The values that are generated are independent 048 and identically distributed with the mean given by a port-parameter. 049 In addition, the seed can be 050 specified as a parameter to control the sequence that is generated. 051 052 <p> This actor instantiates a 053 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/random/Poisson.html">cern.jet.random.Poisson</a> 054 object with a mean of 1.0 by default. 055 056 @author David Bauer and Kostas Oikonomou, contributor: Edward A. Lee 057 @version $Id$ 058 @since Ptolemy II 4.1 059 @Pt.ProposedRating Red (cxh) 060 @Pt.AcceptedRating Red (cxh) 061 */ 062public class ColtPoisson extends ColtRandomSource { 063 /** Construct an actor with the given container and name. 064 * @param container The container. 065 * @param name The name of this actor. 066 * @exception IllegalActionException If the actor cannot be contained 067 * by the proposed container. 068 * @exception NameDuplicationException If the container already has an 069 * actor with this name. 070 */ 071 public ColtPoisson(CompositeEntity container, String name) 072 throws NameDuplicationException, IllegalActionException { 073 super(container, name); 074 075 output.setTypeEquals(BaseType.INT); 076 077 mean = new PortParameter(this, "mean", new DoubleToken(1.0)); 078 mean.setTypeEquals(BaseType.DOUBLE); 079 new SingletonParameter(mean.getPort(), "_showName") 080 .setToken(BooleanToken.TRUE); 081 082 mean.moveToFirst(); 083 } 084 085 /////////////////////////////////////////////////////////////////// 086 //// ports and parameters //// 087 088 /** The mean. 089 * This has type double, initially with value 1.0. 090 */ 091 public PortParameter mean; 092 093 /////////////////////////////////////////////////////////////////// 094 //// public methods //// 095 096 /** Send a random number with a Poisson distribution to the output. 097 * This number is only changed in the prefire() method, so it will 098 * remain constant throughout an iteration. 099 * @exception IllegalActionException If there is no director. 100 */ 101 @Override 102 public void fire() throws IllegalActionException { 103 mean.update(); 104 super.fire(); 105 output.send(0, new IntToken(_current)); 106 } 107 108 /////////////////////////////////////////////////////////////////// 109 //// protected methods //// 110 111 /** Method that is called after _randomNumberGenerator is changed. 112 */ 113 @Override 114 protected void _createdNewRandomNumberGenerator() { 115 _generator = new Poisson(1.0, _randomNumberGenerator); 116 } 117 118 /** Generate a new random number. 119 * @exception IllegalActionException If parameter values are incorrect. 120 */ 121 @Override 122 protected void _generateRandomNumber() throws IllegalActionException { 123 double meanValue = ((DoubleToken) mean.getToken()).doubleValue(); 124 125 _current = _generator.nextInt(meanValue); 126 } 127 128 /////////////////////////////////////////////////////////////////// 129 //// private variables //// 130 131 /** The random number for the current iteration. */ 132 private int _current; 133 134 /** The random number generator. */ 135 private Poisson _generator; 136}