Package minimise :: Module grid
[hide private]
[frames] | no frames]

Source Code for Module minimise.grid

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 2004 Edward d'Auvergne                                  # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23   
 24  from Numeric import Float64, ones, zeros 
 25   
 26  from constraint_linear import Constraint_linear 
 27   
 28   
29 -def grid(func=None, grid_ops=None, args=(), A=None, b=None, l=None, u=None, c=None, print_flag=0, print_prefix=""):
30 """Grid search function. 31 32 Keyword Arguments 33 ~~~~~~~~~~~~~~~~~ 34 35 func: The function to minimise. 36 37 grid_ops: Matrix of options for the grid search. 38 39 args: The tuple of arguments to supply to the function func. 40 41 42 Grid options 43 ~~~~~~~~~~~~ 44 45 The first dimension of grid_ops corresponds to the parameters of the function func, and the 46 second dimension corresponds to: 47 0 - The number of increments. 48 1 - Lower limit. 49 2 - Upper limit. 50 """ 51 52 # Initialise. 53 n = len(grid_ops) 54 grid_size = 0 55 total_steps = 1 56 step_num = ones((n)) 57 params = zeros((n), Float64) 58 min_params = zeros((n), Float64) 59 param_values = [] # This data structure eliminates the round-off error of summing a step size value to the parameter value. 60 for k in xrange(n): 61 params[k] = grid_ops[k][1] 62 min_params[k] = grid_ops[k][1] 63 total_steps = total_steps * grid_ops[k][0] 64 param_values.append([]) 65 for i in xrange(grid_ops[k][0]): 66 param_values[k].append(grid_ops[k][1] + i * (grid_ops[k][2] - grid_ops[k][1]) / (grid_ops[k][0] - 1)) 67 68 # Print out. 69 if print_flag: 70 if print_flag >= 2: 71 print print_prefix 72 print print_prefix 73 print print_prefix + "Grid search" 74 print print_prefix + "~~~~~~~~~~~" 75 76 # Linear constraints. 77 if A != None and b != None: 78 constraint_flag = 1 79 constraint_linear = Constraint_linear(A, b) 80 c = constraint_linear.func 81 if print_flag >= 3: 82 print print_prefix + "Linear constraint matrices." 83 print print_prefix + "A: " + `A` 84 print print_prefix + "b: " + `b` 85 86 # Bound constraints. 87 elif l != None and u != None: 88 constraint_flag = 1 89 print "Bound constraints are not implemented yet." 90 return 91 92 # General constraints. 93 elif c != None: 94 constraint_flag = 1 95 96 # No constraints. 97 else: 98 constraint_flag = 0 99 100 # Set a ridiculously large initial grid value. 101 f_min = 1e300 102 103 # Initial print out. 104 if print_flag: 105 print "\n" + print_prefix + "Searching the grid." 106 107 # Test if the grid is too large (ie total_steps is a long integer) 108 if type(total_steps) == long: 109 raise NameError, "A grid search of size " + `total_steps` + " is too large." 110 111 # Search the grid. 112 k = 0 113 for i in xrange(total_steps): 114 # Check that the grid point does not violate a constraint, and if it does, skip the function call. 115 skip = 0 116 if constraint_flag: 117 ci = c(params) 118 if min(ci) < 0.0: 119 if print_flag >= 3: 120 print print_prefix + "%-3s%-8i%-4s%-65s" % ("k:", k, "xk:", `params`) 121 print print_prefix + "Constraint violated, skipping grid point." 122 print print_prefix + "ci: " + `ci` 123 print "" 124 skip = 1 125 126 # Function call, test, and increment grid_size. 127 if not skip: 128 # Back calculate the current function value. 129 f = apply(func, (params,)+args) 130 131 # Test if the current function value is less than the least function value. 132 if f < f_min: 133 f_min = f 134 min_params = 1.0 * params 135 136 # Print out code. 137 if print_flag: 138 print print_prefix + "%-3s%-8i%-4s%-65s %-4s%-20s" % ("k:", k, "xk:", `min_params`, "fk:", `f_min`) 139 140 # Grid count. 141 grid_size = grid_size + 1 142 143 # Print out code. 144 if print_flag >= 2: 145 if f != f_min: 146 print print_prefix + "%-3s%-8i%-4s%-65s %-4s%-20s" % ("k:", k, "xk:", `params`, "fk:", `f`) 147 if print_flag >= 3: 148 print print_prefix + "%-20s%-20s" % ("Increment:", `step_num`) 149 print print_prefix + "%-20s%-20s" % ("Params:", `params`) 150 print print_prefix + "%-20s%-20s" % ("Min params:", `min_params`) 151 print print_prefix + "%-20s%-20g\n" % ("f:", f) 152 print print_prefix + "%-20s%-20g\n" % ("Min f:", f_min) 153 154 # Increment k. 155 k = k + 1 156 157 # Increment the grid search. 158 for j in xrange(n): 159 if step_num[j] < grid_ops[j][0]: 160 step_num[j] = step_num[j] + 1 161 params[j] = param_values[j][step_num[j]-1] 162 break # Exit so that the other step numbers are not incremented. 163 else: 164 step_num[j] = 1 165 params[j] = grid_ops[j][1] 166 167 # Return the results. 168 return min_params, f_min, grid_size
169