001/* An actor that outputs a random sequence with a Gaussian distribution.
002
003 Copyright (c) 1998-2014 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;
029
030import ptolemy.actor.parameters.PortParameter;
031import ptolemy.data.BooleanToken;
032import ptolemy.data.DoubleToken;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// Gaussian
041
042/**
043 Produce a random sequence with a Gaussian distribution.  On each
044 iteration, a new random number is produced.  The output port is of
045 type DoubleToken.  The values that are generated are independent
046 and identically distributed with the mean and the standard
047 deviation given by parameters.  In addition, the seed can be
048 specified as a parameter to control the sequence that is generated.
049
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 0.2
053 @Pt.ProposedRating Green (eal)
054 @Pt.AcceptedRating Green (bilung)
055 */
056public class Gaussian extends RandomSource {
057    /** Construct an actor with the given container and name.
058     *  @param container The container.
059     *  @param name The name of this actor.
060     *  @exception IllegalActionException If the actor cannot be contained
061     *   by the proposed container.
062     *  @exception NameDuplicationException If the container already has an
063     *   actor with this name.
064     */
065    public Gaussian(CompositeEntity container, String name)
066            throws NameDuplicationException, IllegalActionException {
067        super(container, name);
068
069        output.setTypeEquals(BaseType.DOUBLE);
070
071        mean = new PortParameter(this, "mean", new DoubleToken(0.0));
072        mean.setTypeEquals(BaseType.DOUBLE);
073        new Parameter(mean.getPort(), "_showName", BooleanToken.TRUE);
074
075        standardDeviation = new PortParameter(this, "standardDeviation");
076        standardDeviation.setExpression("1.0");
077        standardDeviation.setTypeEquals(BaseType.DOUBLE);
078        new Parameter(standardDeviation.getPort(), "_showName",
079                BooleanToken.TRUE);
080    }
081
082    ///////////////////////////////////////////////////////////////////
083    ////                     ports and parameters                  ////
084
085    /** The mean of the random number.
086     *  This has type double, initially with value 0.
087     */
088    public PortParameter mean;
089
090    /** The standard deviation of the random number.
091     *  This has type double, initially with value 1.
092     */
093    public PortParameter standardDeviation;
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         public methods                    ////
097
098    /** Send a random number with a Gaussian distribution to the output.
099     *  This number is only changed in the prefire() method, so it will
100     *  remain constant throughout an iteration.
101     *  @exception IllegalActionException If there is no director.
102     */
103    @Override
104    public void fire() throws IllegalActionException {
105        mean.update();
106        standardDeviation.update();
107        super.fire();
108        output.send(0, new DoubleToken(_current));
109    }
110
111    ///////////////////////////////////////////////////////////////////
112    ////                         protected methods                 ////
113
114    /** Generate a new random number.
115     *  @exception IllegalActionException If parameter values are incorrect.
116     */
117    @Override
118    protected void _generateRandomNumber() throws IllegalActionException {
119        double meanValue = ((DoubleToken) mean.getToken()).doubleValue();
120        double standardDeviationValue = ((DoubleToken) standardDeviation
121                .getToken()).doubleValue();
122        double rawNum = _random.nextGaussian();
123        _current = rawNum * standardDeviationValue + meanValue;
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         protected variables               ////
128
129    /** The random number for the current iteration. */
130    protected double _current;
131}