001/* An actor that outputs a random sequence with a Zeta 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.Zeta;
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//// Zeta
043
044/**
045 Produce a random sequence with a Zeta distribution.  On each
046 iteration, a new random number is produced.  The output port is of
047 type DoubleToken.  The values that are generated are independent
048 and identically distributed with the mean and the standard
049 deviation given by parameters.  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/Zeta.html">cern.jet.random.Zeta</a> object with
054 ro and pk set to 1.0.
055
056 @author David Bauer and Kostas Oikonomou
057 @version $Id$
058 @since Ptolemy II 4.1
059 @Pt.ProposedRating Red (cxh)
060 @Pt.AcceptedRating Red (cxh)
061 */
062public class ColtZeta 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 ColtZeta(CompositeEntity container, String name)
072            throws NameDuplicationException, IllegalActionException {
073        super(container, name);
074
075        output.setTypeEquals(BaseType.INT);
076
077        ro = new PortParameter(this, "ro", new DoubleToken(1.0));
078        ro.setTypeEquals(BaseType.DOUBLE);
079        new SingletonParameter(ro.getPort(), "_showName")
080                .setToken(BooleanToken.TRUE);
081
082        pk = new PortParameter(this, "pk", new DoubleToken(1.0));
083        pk.setTypeEquals(BaseType.DOUBLE);
084        new SingletonParameter(pk.getPort(), "_showName")
085                .setToken(BooleanToken.TRUE);
086
087        pk.moveToFirst();
088        ro.moveToFirst();
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                     ports and parameters                  ////
093
094    /** ro.
095     *  This has type double, initially with value 1.0.
096     */
097    public PortParameter ro;
098
099    /** pk.
100     *  This has type double, initially with value 1.0.
101     */
102    public PortParameter pk;
103
104    ///////////////////////////////////////////////////////////////////
105    ////                         public methods                    ////
106
107    /** Send a random number with a Zeta 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        ro.update();
115        pk.update();
116        super.fire();
117        output.send(0, new IntToken(_current));
118    }
119
120    ///////////////////////////////////////////////////////////////////
121    ////                         protected methods                 ////
122
123    /** Method that is called after _randomNumberGenerator is changed.
124     */
125    @Override
126    protected void _createdNewRandomNumberGenerator() {
127        _generator = new Zeta(1.0, 1.0, _randomNumberGenerator);
128    }
129
130    /** Generate a new random number.
131     *  @exception IllegalActionException If the parameter values are incorrect.
132     */
133    @Override
134    protected void _generateRandomNumber() throws IllegalActionException {
135        double roValue = ((DoubleToken) ro.getToken()).doubleValue();
136        double pkValue = ((DoubleToken) pk.getToken()).doubleValue();
137
138        _generator.setState(roValue, pkValue);
139        _current = _generator.nextInt();
140    }
141
142    ///////////////////////////////////////////////////////////////////
143    ////                         private variables                 ////
144
145    /** The random number for the current iteration. */
146    private int _current;
147
148    /** The random number generator. */
149    private Zeta _generator;
150}