001/* An actor that outputs a random sequence with a triangular distribution.
002
003 Copyright (c) 2006-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.SingletonParameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038
039///////////////////////////////////////////////////////////////////
040//// Triangular
041
042/**
043 Produce a random sequence with a triangular distribution.  On each iteration,
044 a new random number is produced.  The output port is of type double.
045 The values that are generated are independent and identically distributed
046 with mode and bounds as defined by the parameters.  In addition, the
047 seed can be specified as a parameter to control the sequence that is
048 generated.
049
050 @author Raymond A. Cardillo
051 @version $Id$
052 @since Ptolemy II 5.2
053 @Pt.ProposedRating Red (cxh)
054 @Pt.AcceptedRating Red (cxh)
055 @see ptolemy.actor.lib.Bernoulli
056 @see ptolemy.actor.lib.DiscreteRandomSource
057 @see ptolemy.actor.lib.Rician
058 @see ptolemy.actor.lib.Uniform
059 */
060public class Triangular extends RandomSource {
061    /** Construct an actor with the given container and name.
062     *  @param container The container.
063     *  @param name The name of this actor.
064     *  @exception IllegalActionException If the actor cannot be contained
065     *   by the proposed container.
066     *  @exception NameDuplicationException If the container already has an
067     *   actor with this name.
068     */
069    public Triangular(CompositeEntity container, String name)
070            throws NameDuplicationException, IllegalActionException {
071        super(container, name);
072
073        output.setTypeEquals(BaseType.DOUBLE);
074
075        mode = new PortParameter(this, "mode", new DoubleToken(0.5));
076        mode.setTypeEquals(BaseType.DOUBLE);
077        new SingletonParameter(mode.getPort(), "_showName")
078                .setToken(BooleanToken.TRUE);
079
080        min = new PortParameter(this, "min", new DoubleToken(0.0));
081        min.setTypeEquals(BaseType.DOUBLE);
082        new SingletonParameter(min.getPort(), "_showName")
083                .setToken(BooleanToken.TRUE);
084
085        max = new PortParameter(this, "max", new DoubleToken(1.0));
086        max.setTypeEquals(BaseType.DOUBLE);
087        new SingletonParameter(max.getPort(), "_showName")
088                .setToken(BooleanToken.TRUE);
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                     ports and parameters                  ////
093
094    /** The minimum value.
095     *  This parameter contains a DoubleToken, initially with value 0.0.
096     */
097    public PortParameter min;
098
099    /** The maximum value.
100     *  This parameter contains a DoubleToken, initially with value 1.0.
101     */
102    public PortParameter max;
103
104    /** The mode of the distribution (peak of triangle).
105     *  This parameter contains a DoubleToken, initially with value 0.5.
106     */
107    public PortParameter mode;
108
109    ///////////////////////////////////////////////////////////////////
110    ////                         public methods                    ////
111
112    /** Send a random number with a triangular distribution to the output.
113     *  This number is only changed in the prefire() method, so it will
114     *  remain constant throughout an iteration.
115     *  @exception IllegalActionException If there is no director.
116     */
117    @Override
118    public void fire() throws IllegalActionException {
119        mode.update();
120        min.update();
121        max.update();
122        super.fire();
123        output.send(0, new DoubleToken(_current));
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         protected methods                 ////
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 minValue = ((DoubleToken) min.getToken()).doubleValue();
135        double maxValue = ((DoubleToken) max.getToken()).doubleValue();
136        double modeValue = ((DoubleToken) mode.getToken()).doubleValue();
137
138        if (minValue > maxValue) {
139            throw new IllegalActionException(this,
140                    "Invalid bounds: min is greater than max.");
141        }
142
143        if (modeValue < minValue) {
144            throw new IllegalActionException(this,
145                    "Invalid bounds: mode is less than min.");
146        }
147
148        if (modeValue > maxValue) {
149            throw new IllegalActionException(this,
150                    "Invalid bounds: mode is greater than max.");
151        }
152
153        double rawNum = _random.nextDouble();
154        double left = modeValue - minValue;
155        double whole = maxValue - minValue;
156        double right = maxValue - modeValue;
157
158        if (rawNum <= left / whole) {
159            _current = minValue + Math.sqrt(rawNum * whole * left);
160        } else {
161            _current = maxValue - Math.sqrt((1.0d - rawNum) * whole * right);
162        }
163    }
164
165    ///////////////////////////////////////////////////////////////////
166    ////                         private variables                 ////
167    // The random number for the current iteration.
168    private double _current;
169}