001/* An actor that outputs a random sequence with a Gamma 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.Gamma; 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//// Gamma 042 043/** 044 Produce a random sequence with a Gamma 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 mean 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/Gamma.html">cern.jet.random.Gamma</a> object with 053 double and lambda both set to 1.0. 054 055 @author David Bauer and Kostas Oikonomou 056 @version $Id$ 057 @since Ptolemy II 4.1 058 @Pt.ProposedRating Red (cxh) 059 @Pt.AcceptedRating Red (cxh) 060 */ 061public class ColtGamma extends ColtRandomSource { 062 /** Construct an actor with the given container and name. 063 * @param container The container. 064 * @param name The name of this actor. 065 * @exception IllegalActionException If the actor cannot be contained 066 * by the proposed container. 067 * @exception NameDuplicationException If the container already has an 068 * actor with this name. 069 */ 070 public ColtGamma(CompositeEntity container, String name) 071 throws NameDuplicationException, IllegalActionException { 072 super(container, name); 073 074 output.setTypeEquals(BaseType.DOUBLE); 075 076 alpha = new PortParameter(this, "alpha", new DoubleToken(1.0)); 077 alpha.setTypeEquals(BaseType.DOUBLE); 078 new SingletonParameter(alpha.getPort(), "_showName") 079 .setToken(BooleanToken.TRUE); 080 081 lambda = new PortParameter(this, "lambda", new DoubleToken(1.0)); 082 lambda.setTypeEquals(BaseType.DOUBLE); 083 new SingletonParameter(lambda.getPort(), "_showName") 084 .setToken(BooleanToken.TRUE); 085 086 lambda.moveToFirst(); 087 alpha.moveToFirst(); 088 } 089 090 /////////////////////////////////////////////////////////////////// 091 //// ports and parameters //// 092 093 /** The mean, or alpha. 094 * This has type double with default 1.0. 095 */ 096 public PortParameter alpha; 097 098 /** The variance, or lambda. 099 * This has type double with default 1.0. 100 */ 101 public PortParameter lambda; 102 103 /////////////////////////////////////////////////////////////////// 104 //// public methods //// 105 106 /** Send a random number with a Gamma distribution to the output. 107 * This number is only changed in the prefire() method, so it will 108 * remain constant throughout an iteration. 109 * @exception IllegalActionException If there is no director. 110 */ 111 @Override 112 public void fire() throws IllegalActionException { 113 alpha.update(); 114 lambda.update(); 115 super.fire(); 116 output.send(0, new DoubleToken(_current)); 117 } 118 119 /////////////////////////////////////////////////////////////////// 120 //// protected methods //// 121 122 /** Method that is called after _randomNumberGenerator is changed. 123 */ 124 @Override 125 protected void _createdNewRandomNumberGenerator() { 126 _generator = new Gamma(1.0, 1.0, _randomNumberGenerator); 127 } 128 129 /** Generate a new random number. 130 * @exception IllegalActionException If parameter values are incorrect. 131 */ 132 @Override 133 protected void _generateRandomNumber() throws IllegalActionException { 134 double alphaValue = ((DoubleToken) alpha.getToken()).doubleValue(); 135 double lambdaValue = ((DoubleToken) lambda.getToken()).doubleValue(); 136 137 _current = _generator.nextDouble(alphaValue, lambdaValue); 138 } 139 140 /////////////////////////////////////////////////////////////////// 141 //// private variables //// 142 143 /** The random number for the current iteration. */ 144 private double _current; 145 146 /** The random number generator. */ 147 private Gamma _generator; 148}