001/* An actor that outputs a random sequence with a HyperGeometric 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.HyperGeometric;
031import ptolemy.actor.parameters.PortParameter;
032import ptolemy.data.BooleanToken;
033import ptolemy.data.IntToken;
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//// HyperGeometric
042
043/**
044 Produce a random sequence with a HyperGeometric 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/HyperGeometric.html">cern.jet.random.HyperGeometric</a> object with
053 N set to 2, and s and n both set to 1.
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 ColtHyperGeometric 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 ColtHyperGeometric(CompositeEntity container, String name)
071            throws NameDuplicationException, IllegalActionException {
072        super(container, name);
073
074        output.setTypeEquals(BaseType.INT);
075
076        N = new PortParameter(this, "N", new IntToken(2));
077        N.setTypeEquals(BaseType.INT);
078        new SingletonParameter(N.getPort(), "_showName")
079                .setToken(BooleanToken.TRUE);
080
081        s = new PortParameter(this, "s", new IntToken(1));
082        s.setTypeEquals(BaseType.INT);
083        new SingletonParameter(s.getPort(), "_showName")
084                .setToken(BooleanToken.TRUE);
085
086        n = new PortParameter(this, "n", new IntToken(1));
087        n.setTypeEquals(BaseType.INT);
088        new SingletonParameter(n.getPort(), "_showName")
089                .setToken(BooleanToken.TRUE);
090
091        n.moveToFirst();
092        s.moveToFirst();
093        N.moveToFirst();
094    }
095
096    ///////////////////////////////////////////////////////////////////
097    ////                     ports and parameters                  ////
098
099    /** N.
100     *  This has type int with default 2.
101     */
102    public PortParameter N;
103
104    /** coltLambda.
105     *  This has type int with default 1.
106     */
107    public PortParameter s;
108
109    /** coltLambda.
110     *  This has type int with default 1.
111     */
112    public PortParameter n;
113
114    ///////////////////////////////////////////////////////////////////
115    ////                         public methods                    ////
116
117    /** Send a random number with a HyperGeometric distribution to the output.
118     *  This number is only changed in the prefire() method, so it will
119     *  remain constant throughout an iteration.
120     *  @exception IllegalActionException If there is no director.
121     */
122    @Override
123    public void fire() throws IllegalActionException {
124        N.update();
125        s.update();
126        n.update();
127        super.fire();
128        output.send(0, new IntToken(_current));
129    }
130
131    ///////////////////////////////////////////////////////////////////
132    ////                         protected methods                 ////
133
134    /** Method that is called after _randomNumberGenerator is changed.
135     */
136    @Override
137    protected void _createdNewRandomNumberGenerator() {
138        _generator = new HyperGeometric(2, 1, 1, _randomNumberGenerator);
139    }
140
141    /** Generate a new random number.
142     *  @exception IllegalActionException If parameter values are incorrect.
143     */
144    @Override
145    protected void _generateRandomNumber() throws IllegalActionException {
146        int NValue = ((IntToken) N.getToken()).intValue();
147        int sValue = ((IntToken) s.getToken()).intValue();
148        int nValue = ((IntToken) n.getToken()).intValue();
149
150        _current = _generator.nextInt(NValue, sValue, nValue);
151    }
152
153    ///////////////////////////////////////////////////////////////////
154    ////                         private variables                 ////
155
156    /** The random number for the current iteration. */
157    private int _current;
158
159    /** The random number generator. */
160    private HyperGeometric _generator;
161}