Package pypower :: Module fairmax
[hide private]
[frames] | no frames]

Source Code for Module pypower.fairmax

 1  # Copyright (C) 1996-2011 Power System Engineering Research Center (PSERC) 
 2  # Copyright (C) 2011 Richard Lincoln 
 3  # 
 4  # PYPOWER is free software: you can redistribute it and/or modify 
 5  # it under the terms of the GNU General Public License as published 
 6  # by the Free Software Foundation, either version 3 of the License, 
 7  # or (at your option) any later version. 
 8  # 
 9  # PYPOWER is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
12  # GNU General Public License for more details. 
13  # 
14  # You should have received a copy of the GNU General Public License 
15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
16   
17  """Same as built-in C{max}, except breaks ties randomly. 
18  """ 
19   
20  from random import random 
21  from numpy import nonzero, fix 
22   
23   
24 -def fairmax(x):
25 """Same as built-in C{max}, except breaks ties randomly. 26 27 Takes a vector as an argument and returns the same output as the 28 built-in function C{max} with two output parameters, except that 29 where the maximum value occurs at more than one position in the 30 vector, the index is chosen randomly from these positions as opposed 31 to just choosing the first occurance. 32 33 @see: C{max} 34 35 @author: Ray Zimmerman (PSERC Cornell) 36 @author: Richard Lincoln 37 """ 38 val = max(x) ## find max value 39 i = nonzero(x == val) ## find all positions where this occurs 40 n = len(i) ## number of occurences 41 idx = i( fix(n * random()) + 1 ) ## select index randomly among occurances 42 43 return val, idx
44