001/* An actor that outputs a random sequence with a Normal 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.Normal;
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//// Normal
042
043/**
044 Produce a random sequence with a Normal 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/Normal.html">cern.jet.random.Normal</a> object with
053 a mean of 1.0 and a standardDeviation of 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 ColtNormal 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 ColtNormal(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        output.setTypeEquals(BaseType.DOUBLE);
075
076        mean = new PortParameter(this, "mean", new DoubleToken(1.0));
077        mean.setTypeEquals(BaseType.DOUBLE);
078        new SingletonParameter(mean.getPort(), "_showName")
079                .setToken(BooleanToken.TRUE);
080
081        standardDeviation = new PortParameter(this, "standardDeviation",
082                new DoubleToken(1.0));
083        standardDeviation.setTypeEquals(BaseType.DOUBLE);
084        new SingletonParameter(standardDeviation.getPort(), "_showName")
085                .setToken(BooleanToken.TRUE);
086
087        standardDeviation.moveToFirst();
088        mean.moveToFirst();
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                     ports and parameters                  ////
093
094    /** mean.
095     *  This has type double with default value 1.0.
096     */
097    public PortParameter mean;
098
099    /** standardDeviation.
100     *  This has type double with default 1.0.
101     */
102    public PortParameter standardDeviation;
103
104    ///////////////////////////////////////////////////////////////////
105    ////                         public methods                    ////
106
107    /** Send a random number with a Normal distribution to the output.
108     *  This number is only changed in the prefire() method, so it will
109     *  remain constant throughout an iteration.
110     *  @exception IllegalActionException If there is no director.
111     */
112    @Override
113    public void fire() throws IllegalActionException {
114        super.fire();
115        output.send(0, new DoubleToken(_current));
116    }
117
118    ///////////////////////////////////////////////////////////////////
119    ////                         protected methods                 ////
120
121    /** Method that is called after _randomNumberGenerator is changed.
122     */
123    @Override
124    protected void _createdNewRandomNumberGenerator() {
125        _generator = new Normal(1.0, 1.0, _randomNumberGenerator);
126    }
127
128    /** Generate a new random number.
129     *  @exception IllegalActionException If parameter values are incorrect.
130     */
131    @Override
132    protected void _generateRandomNumber() throws IllegalActionException {
133        double meanValue = ((DoubleToken) mean.getToken()).doubleValue();
134        double standardDeviationValue = ((DoubleToken) standardDeviation
135                .getToken()).doubleValue();
136
137        _generator.setState(meanValue, standardDeviationValue);
138        _current = _generator.nextDouble();
139    }
140
141    ///////////////////////////////////////////////////////////////////
142    ////                         private variables                 ////
143
144    /** The random number for the current iteration. */
145    private double _current;
146
147    /** The random number generator. */
148    private Normal _generator;
149}