Author: bugman Date: Mon Jan 11 11:10:14 2010 New Revision: 10166 URL: http://svn.gna.org/viewcvs/relax?rev=10166&view=rev Log: Copied the grid.py minimisation module from the original multi_processor branch. The command used was: svn cp svn+ssh://bugman@xxxxxxxxxxx/svn/relax/branches/multi_processor/minimise/grid.py subdivide_grid.py This module is now part of minfx, but the Grid_info class will be used by the multi-processor code to subdivide the diffusion tensor grid search. The code needs much work! Added: branches/multi_processor_merge/generic_fns/subdivide_grid.py - copied, changed from r10165, branches/multi_processor/minimise/grid.py Copied: branches/multi_processor_merge/generic_fns/subdivide_grid.py (from r10165, branches/multi_processor/minimise/grid.py) URL: http://svn.gna.org/viewcvs/relax/branches/multi_processor_merge/generic_fns/subdivide_grid.py?p2=branches/multi_processor_merge/generic_fns/subdivide_grid.py&p1=branches/multi_processor/minimise/grid.py&r1=10165&r2=10166&rev=10166&view=diff ============================================================================== --- branches/multi_processor/minimise/grid.py (original) +++ branches/multi_processor_merge/generic_fns/subdivide_grid.py Mon Jan 11 11:10:14 2010 @@ -1,6 +1,7 @@ ############################################################################### # # -# Copyright (C) 2003, 2004 Edward d'Auvergne # +# Copyright (C) 2003, 2004, 2010 Edward d'Auvergne # +# Copyright (C) 2007 Gary S Thompson (https://gna.org/users/varioustoxins) # # # # This file is part of the program relax. # # # @@ -21,14 +22,11 @@ ############################################################################### #FIXME exceptiosn not progated properly in main loop -import sys,math +import math from Numeric import Float from textwrap import dedent -from copy import copy - -from Numeric import Float64, ones, zeros - -from constraint_linear import Constraint_linear + +from Numeric import Float64, ones #constants @@ -261,181 +259,3 @@ def __iter__(self): return Grid_info.Iterator(self,self.start,self.start+self.range) - - - - - -def grid(func=None, grid_ops=None, args=(), A=None, b=None, l=None, u=None, c=None, print_flag=0, print_prefix=""): - """Grid search function. - - - Keyword Arguments - ~~~~~~~~~~~~~~~~~ - - func: The function to minimise. - - grid_ops: Matrix of options for the grid search. - - args: The tuple of arguments to supply to the function func. - - - Grid options - ~~~~~~~~~~~~ - - The first dimension of grid_ops corresponds to the parameters of the function func, and the - second dimension corresponds to: - 0 - The number of increments. - 1 - Lower limit. - 2 - Upper limit. - """ - - if not isinstance(grid_ops, Grid_info): - info = Grid_info(grid_ops) - # FIXME: issue warning? - else: - info=grid_ops - - #print 'info',info - #info.print_steps() - - # Print out. - if print_flag and pre_and_post_amble: - if print_flag >= 2: - print print_prefix - print print_prefix - print print_prefix + "Grid search" - print print_prefix + "~~~~~~~~~~~" - - #FIXME: edge conditions l == None u == soemthing - # Linear constraints. - if A != None and b != None: - constraint_flag = 1 - constraint_linear = Constraint_linear(A, b) - c = constraint_linear.func - if print_flag >= 3 and pre_and_post_amble: - print print_prefix + "Linear constraint matrices." - print print_prefix + "A: " + `A` - print print_prefix + "b: " + `b` - - # Bound constraints. - elif l != None and u != None: - constraint_flag = 1 - # FIXME: doesn't raise exceptipn - print "Bound constraints are not implemented yet." - return - - # General constraints. - elif c != None: - constraint_flag = 1 - - # No constraints. - else: - constraint_flag = 0 - - # Set a ridiculously large initial grid value. - f_min = 1e300 - # FIXME:should be float.PosMax but internal float classhes with relax float, rename relax float to ieee_float - - # Initial print out. - if print_flag and pre_and_post_amble: - print "\n" + print_prefix + "Searching the grid." - - # Test if the grid is too large (ie total_steps is a long integer) - if type(info.steps) == long: - # FIXME: should be range error - raise NameError, "A grid search of size " + `total_steps` + " is too large." - - # Search the grid. - - iter = info.__iter__() - k = iter.step - k_start = k - first_time = True - for params in iter: - - # Check that the grid point does not violate a constraint, and if it does, skip the function call. - skip = False - if constraint_flag: - ci = c(params) - if min(ci) < 0.0: - if print_flag >= 3: - print print_prefix + "%-3s%-8i%-4s%-65s" % ("k:", k, "xk:", `params`) - print print_prefix + "Constraint violated, skipping grid point." - print print_prefix + "ci: " + `ci` - print "" - skip = True - #print 'skip',k,skip - # Function call, test, and increment grid_size. - if not skip: - # Back calculate the current function value. - f = func(*(params,)+args) - # nasty hacks (1) to make al results different - #f=f-k - #print '*****params',k,f - # Test if the current function value is less than the least function value. - if f < f_min: - f_min = f - #FIXME: replacw with a copy - min_params = params * 1.0 - # nasty hacks (2) to make al results different - #for i,elem in enumerate(min_params): - # min_params[i]=elem+((k*(i+1))/1000.0) - - # Print out code. - if print_flag: - print print_prefix + "%-3s%-8i%-4s%-65s %-4s%-20s" % ("k:", k, "xk:", `min_params`, "fk:", `f_min`) - - # FIXME: not needed - # Grid count. - #grid_size = grid_size + 1 - - # Print out code. - if print_flag >= 2 and pre_and_post_amble: - if f != f_min: - print print_prefix + "%-3s%-8i%-4s%-65s %-4s%-20s" % ("k:", k, "xk:", `params`, "fk:", `f`) - if print_flag >= 3: - print print_prefix + "%-20s%-20s" % ("Increment:", `step_num`) - print print_prefix + "%-20s%-20s" % ("Params:", `params`) - print print_prefix + "%-20s%-20s" % ("Min params:", `min_params`) - print print_prefix + "%-20s%-20g\n" % ("f:", f) - print print_prefix + "%-20s%-20g\n" % ("Min f:", f_min) - - # Increment k. - k = k + 1 - - - # Return the results. - return min_params, f_min, k-k_start - -def test(grid_ops): - - n = len(grid_ops) - grid_size = 0 - total_steps = 1 - step_num = ones((n)) - params = zeros((n), Float64) - min_params = zeros((n), Float64) - param_values = [] # This data structure eliminates the round-off error of summing a step size value to the parameter value. - for k in xrange(n): - params[k] = grid_ops[k][GRID_LOWER] - min_params[k] = grid_ops[k][GRID_LOWER] - total_steps = total_steps * grid_ops[k][GRID_STEPS] - param_values.append([]) - for i in xrange(grid_ops[k][GRID_STEPS]): - param_values[k].append(grid_ops[k][GRID_LOWER] + i * (grid_ops[k][GRID_UPPER] - grid_ops[k][GRID_LOWER]) / (grid_ops[k][GRID_STEPS] - 1)) - - print params - print param_values - print step_num - for i in xrange(total_steps): - print i,step_num - - for j in xrange(n): - if step_num[j] < grid_ops[j][GRID_STEPS]: - step_num[j] = step_num[j] + 1 - params[j] = param_values[j][step_num[j]-1] - break # Exit so that the other step numbers are not incremented. - else: - step_num[j] = 1 - params[j] = grid_ops[j][GRID_LOWER]