001/* An analysis to calculate the maximum/minimum cycle mean of a directed
002 cyclic graph.
003
004 Copyright (c) 2003-2014 The University of Maryland. 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 MARYLAND 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 MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF MARYLAND 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 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024
025 */
026package ptolemy.graph.analysis;
027
028import java.util.List;
029
030import ptolemy.graph.Graph;
031import ptolemy.graph.analysis.analyzer.Analyzer;
032import ptolemy.graph.analysis.analyzer.CycleMeanAnalyzer;
033import ptolemy.graph.analysis.strategy.KarpCycleMeanStrategy;
034import ptolemy.graph.mapping.ToDoubleMapping;
035
036///////////////////////////////////////////////////////////////////
037//// CycleMeanAnalysis
038
039/**
040 An analysis to calculate the maximum/minimum cycle mean of a directed
041 cyclic graph.
042 <p>
043 The analyzer associated to this analysis, adds up the cost of the edges on
044 different cycles and divides it by the number of edges in that cycle,
045 then picks the maximum/minimum of them.
046 <p>
047 Strongly connected components are being processed separately, and the cycle
048 mean has the maximum/minimum value among them.
049 When there are multiple edges between two nodes, the edge with the
050 maximum/minimum weight is considered for the cycle that gives the
051 maximum/minimum cycle mean.
052
053 <p>
054 Note that the mathematical definition of maximum cycle mean and maximum profit
055 to cost are different, though some time the name "maximum cycle mean" is used to
056 refer to the maximum profit to cost ratio.
057 <p>
058 For a detailed mathematical description of the problem, refer to:
059 <p>
060 Ali Dasdan , Sandy S. Irani , Rajesh K. Gupta, Efficient algorithms for optimum
061 cycle mean and optimum cost to time ratio problems,
062 Proceedings of the 36th ACM/IEEE conference on Design automation conference,
063 p.37-42, June 21-25, 1999, New Orleans, Louisiana, United States
064 <p>
065
066 @since Ptolemy II 4.0
067 @Pt.ProposedRating Red (shahrooz)
068 @Pt.AcceptedRating Red (ssb)
069 @version $Id$
070 @author Shahrooz Shahparnia
071 @see ptolemy.graph.analysis.MaximumProfitToCostRatioAnalysis
072 */
073public class CycleMeanAnalysis extends Analysis {
074    /** Construct a maximum cycle mean analysis associated with a graph with a
075     *  default analyzer.
076     *
077     *  @param graph The given graph.
078     *  @param edgeLengths  The lengths associated with the edges of the graph.
079     */
080    public CycleMeanAnalysis(Graph graph, ToDoubleMapping edgeLengths) {
081        super(new KarpCycleMeanStrategy(graph, edgeLengths));
082    }
083
084    /** Construct a maximum cycle mean analysis associated with a graph with a
085     *  given analyzer.
086     *
087     *  @param analyzer The given analyzer.
088     */
089    public CycleMeanAnalysis(CycleMeanAnalyzer analyzer) {
090        super(analyzer);
091    }
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Return the nodes on the cycle that corresponds to the maximum/minimum
097     *  cycle mean as an ordered list. If there is more than one cycle with the
098     *  same maximal/minimal cycle mean, one of them is returned randomly,
099     *  but the same cycle is returned by different invocations of the method,
100     *  unless the graph changes.
101     *
102     *  @return The nodes on the cycle that corresponds to one of the
103     *  maximum/minimum cycle means as an ordered list.
104     */
105    public List cycle() {
106        return ((CycleMeanAnalyzer) analyzer()).cycle();
107    }
108
109    /** Return the maximum cycle mean value.
110     *
111     *  @return The maximum cycle mean value.
112     */
113    public double maximumCycleMean() {
114        return ((CycleMeanAnalyzer) analyzer()).maximumCycleMean();
115    }
116
117    /** Return minimum cycle mean value.
118     *
119     *  @return The minimum cycle mean value.
120     */
121    public double minimumCycleMean() {
122        return ((CycleMeanAnalyzer) analyzer()).minimumCycleMean();
123    }
124
125    /** Return a description of the analysis and the associated analyzer.
126     *
127     *  @return A description of the analysis and the associated analyzer.
128     */
129    @Override
130    public String toString() {
131        return "Cycle mean analysis using the following analyzer:\n"
132                + analyzer().toString();
133    }
134
135    /** Check if a given analyzer is compatible with this analysis.
136     *  In other words if it is possible to use it to compute the computation
137     *  associated with this analysis.
138     *
139     *  @param analyzer The given analyzer.
140     *  @return True if the given analyzer is valid for this analysis.
141     */
142    @Override
143    public boolean validAnalyzerInterface(Analyzer analyzer) {
144        return analyzer instanceof CycleMeanAnalyzer;
145    }
146}