Changeset 576 for branches


Ignore:
Timestamp:
10/04/12 11:14:05 (4 years ago)
Author:
mmckerns
Message:

updated Lan's collapse code so 'collapse_solver' is imported

Location:
branches/collapse
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • branches/collapse/TEST_OUQ_surrogate_diam_collapse.py

    r560 r576  
    4747  solver.SetGenerationMonitor(stepmon) 
    4848  solver.SetConstraints(_constraints) 
    49   
    50   ############################################################################## 
    51    
    52                  ## THE FOLLOWING TERMINATION AND CONSTRAINTS ## 
    53                  ## CAN BE MOVED TO A NEW MODULE AND IMPORTED ## 
    54    
    55   ######################### TERMINATION CONDITIONS ############################# 
    56   from mystic.termination import Or   
    57   from mystic.termination import ChangeOverGeneration as COG 
    58   from collapse_termination_v2 import CLPS_Weight as WCLPS 
    59   from collapse_termination_v2 import CLPS_Position as PCLPS   
    60   from mystic.math.measures import _nested_split   
    61    
    62   tol = convergence_tol 
    63   _COG = COG(tol,ngen) 
    64    
    65   w_lb, x_lb = _nested_split(lb, npts) 
    66   w_ub, x_ub = _nested_split(ub, npts) 
    67   range_x = [] 
    68   for i in range(len(x_lb)): 
    69       range_x.append(abs(x_lb[i][0] - x_ub[i][0]))   
    70   tolP = [ele*tolP_range_fr for ele in range_x]  
    71   if debug: print 'tolP = %s' %tolP 
    72    
    73   indexW_ids = [range(nx) for nx in npts]  # list of indices for CLPS_Weight termination check 
    74   indexP_ids = []  # list of indices for CLPS_Position termination check   
    75    
    76   x = [] 
    77   _WCLPS = [] 
    78   _PCLPS = [] 
    79    
    80   for m in range(len(indexW_ids)): 
    81       for idx in indexW_ids[m]: 
    82           _WCLPS.append(WCLPS(npts, tol_weight = tolW, generations=ngcol, axisW = m, indexW = idx)) 
    83            
    84   for m in range(len(indexP_ids)): 
    85       for pair in indexP_ids[m]: 
    86           _PCLPS.append( PCLPS(npts, tol_position=tolP, generations=ngcol, \ 
    87                                 axisP = m, index1 = pair[0], index2  = pair[1]))  
    88   x.extend(_WCLPS) 
    89   x.extend(_PCLPS) 
    90   x.append(_COG) 
    91   trm = Or(*x) 
    92    
    93   ######################## NEW CONSTRAINTS AFTER COLLAPSE ###################### 
    94    
    95   list_vanished_weights = [] # list of [measure, index] lists 
    96   list_collapsed_pairs = [] # list of [measure, pair] lists, where pair = [index1, index2]   
    97    
    98   for i in range(len(npts)): 
    99     c = [] 
    100     for idx in range(npts[i]): 
    101         for jdx in range(idx,npts[i]): 
    102             if idx !=jdx: 
    103                 c.append([idx, jdx]) 
    104     indexP_ids.append(c)   
    105    
    106   def _set_weight_to_zero(rv, npts, axisW, indexW): 
    107     from mystic.math.dirac_measure import product_measure 
    108     c = product_measure() 
    109     c.load(rv, npts) 
    110     c[axisW][indexW].weight = 0 
    111     return c.flatten() 
    112    
    113   def _set_same_positions(rv, npts, axisP, index1, index2): 
    114     from mystic.math.dirac_measure import product_measure 
    115     c = product_measure() 
    116     c.load(rv, npts) 
    117     c[axisP][index1].weight += c[axisP][index2].weight 
    118     c[axisP][index2].weight = 0 
    119     c[axisP][index2].position = c[axisP][index1].position 
    120     return c.flatten()  
    121    
    122   def new_constraints(rv, list_collapsed_pairs, list_vanished_weights): 
    123     for ele in list_vanished_weights: 
    124       rv = _set_weight_to_zero(rv, npts, axisW = ele[0], indexW = ele[1]) 
    125     for mem in list_collapsed_pairs: 
    126       rv = _set_same_positions(rv, npts, axisP = mem[0], \ 
    127               index1 = mem[1][0], index2 = mem[1][1])             
    128     return _constraints(rv)    
    129    
    130   ############################################################################## 
    131    
    132    
    133   ############################ RUN THE SOLVER ################################## 
    134   collapse_data = {} 
    135   tot_evaluations = 0 # total number of function eveluations 
    136   while _COG(solver) != True:          
    137     solver.Solve(cost,termination=trm,strategy=Best1Exp, \ 
    138                CrossProbability=crossover,ScalingFactor=percent_change)  
    139     tot_evaluations += solver.evaluations     
    140     collapse_data[stepmon._step] = trm(solver, True) 
    141      
    142     if debug: 
    143       print 'last_itr_evaluations = %s' %solver.evaluations 
    144       print "Generation  %d " %stepmon._step  
    145      
    146     int_trm = trm(solver, info = 'self')   
    147     for ele in range(len(int_trm)): 
    148       cond = int_trm[ele] 
    149       string = cond(solver, info = True) 
    150       string1 = string.replace(',', ' ') 
    151       string2 = string1.replace('&', ' ') 
    152       string3 = string2.replace(':', ' ') 
    153       lst = string3.split() 
    154        
    155       if lst[0] == 'CLPS_Weight': 
    156           for i in range(len(lst)): 
    157               if lst[i] == 'measure': 
    158                   axisW = int(lst[i+1]) 
    159                   if lst[i+2] == 'index': indexW = int(lst[i+3]) 
    160                    
    161           if debug: 
    162             print 'axisW = %s' %axisW 
    163             print 'indexW = %s' %indexW 
    164            
    165           list_vanished_weights.append([axisW, indexW])  
    166           indexW_ids[axisW].remove(indexW) 
    167                   
    168       if lst[0] == 'CLPS_Position': 
    169           for i in range(len(lst)):   
    170               if lst[i] == 'measure':         
    171                   axisP = int(lst[i+1]) 
    172                   if lst[i+2] == 'indices':  
    173                       index1 = int(lst[i+3].lstrip('(')) 
    174                       index2 = int(lst[i+4].rstrip(')')) 
    175           if debug: 
    176             print "axisP = %s" %axisP 
    177             print "index1 = %s" %index1 
    178             print "index2 = %s" %index2 
    179            
    180           list_collapsed_pairs.append([axisP, [index1, index2]]) 
    181           indexP_ids[axisP].remove([index1,index2])           
    182        
    183       ##### AFTER COLLAPSE OCCURRED CHANGE THE TERMINATION AND CONSTRAINTS ##### 
    184                      
    185       def _new_constraints(rv): 
    186         return new_constraints(rv, list_collapsed_pairs, list_vanished_weights)       
    187       solver.SetConstraints(_new_constraints) 
    188        
    189       x = [] 
    190       _WCLPS = [] 
    191       _PCLPS = [] 
    192       for m in range(len(indexW_ids)): 
    193           for idx in indexW_ids[m]: 
    194               _WCLPS.append(WCLPS(npts, generations=ngcol, axisW = m, indexW = idx)) 
    195                
    196       for m in range(len(indexP_ids)): 
    197           for pair in indexP_ids[m]: 
    198               _PCLPS.append( PCLPS(npts, tol_position=tolP, generations=ngcol, \ 
    199                                     axisP = m, index1 = pair[0], index2  = pair[1]))  
    200       x.extend(_WCLPS) 
    201       x.extend(_PCLPS) 
    202       x.append(_COG) 
    203       trm = Or(*x) 
    204        
    205       if debug: 
    206         print 'list_vanished_weights = ' 
    207         print  list_vanished_weights 
    208         print 'list_collapsed_pairs = ' 
    209         print  list_collapsed_pairs                          
    210    
    211   solved = solver.bestSolution 
    212  #print "solved: %s" % solver.Solution() 
    213   func_max = MINMAX * solver.bestEnergy       #NOTE: -solution assumes -Max 
    214  #func_max = 1.0 + MINMAX*solver.bestEnergy   #NOTE: 1-sol => 1-success = fail 
    215   func_evals = solver.evaluations 
    216   from mystic.munge import write_support_file 
    217   write_support_file(stepmon) 
    218   ''' 
    219   if collapse_data: 
    220     f = open('collapse_data.txt','w') 
    221     f.write('COLLAPSE DATA = %s' %collapse_data) 
    222     f.close()''' 
    223   return solved, func_max, tot_evaluations, collapse_data 
    224  
    225  
     49   
     50  import collapse_code   
     51  return collapse_code.collapseSolver(solver, cost, npts, lb, ub, convergence_tol, tolW, \ 
     52                        tolP_range_fr, ngen, ngcol, Best1Exp, crossover, \ 
     53                        percent_change, stepmon, _constraints) 
    22654####################################################################### 
    22755# maximize the function 
     
    301129  H_mean = 6.5    #NOTE: SET THE 'mean' HERE! 
    302130  H_range = 1.0   #NOTE: SET THE 'range' HERE! 
    303   nx = 5  #NOTE: SET THE NUMBER OF 'h' POINTS HERE! 
    304   ny = 5  #NOTE: SET THE NUMBER OF 'a' POINTS HERE! 
    305   nz = 5  #NOTE: SET THE NUMBER OF 'v' POINTS HERE! 
     131  nx = 3  #NOTE: SET THE NUMBER OF 'h' POINTS HERE! 
     132  ny = 3  #NOTE: SET THE NUMBER OF 'a' POINTS HERE! 
     133  nz = 3  #NOTE: SET THE NUMBER OF 'v' POINTS HERE! 
    306134  target = (H_mean,) 
    307135  error = (H_range,) 
Note: See TracChangeset for help on using the changeset viewer.