Changeset 296


Ignore:
Timestamp:
07/07/10 12:22:35 (6 years ago)
Author:
mmckerns
Message:

extended Sows/Solvers? to log solver 'rank' (see ticket #94)

Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/UQ/math/examples2/log_reader.py

    r255 r296  
    99f.close() 
    1010 
    11 contents = file.split("_#_  __ChiSq__  __params__\n") 
     11contents = file.split("___#___  __ChiSq__  __params__\n") 
    1212lines = contents[-1].split('\n') 
    1313 
     
    1616for line in lines[:-1]: 
    1717  values = line.split("   ") 
    18   iter.append(eval(values[0])) 
     18  iter.append(eval(values[0])[0]) #FIXME: we ignore 'id' here 
    1919  cost.append(eval(values[1])) 
    2020  param.append(eval(values[2])) 
     
    3131ax1 = fig.add_subplot(2,1,1) 
    3232for j in range(len(param[0])): 
    33   #ax1.plot(iter,conv[j],label=str(j)) 
    3433  ax1.plot(iter,conv[j],label=str(j),marker='o') 
    3534plt.legend() 
    3635 
    3736ax2 = fig.add_subplot(2,1,2) 
    38 #ax2.plot(iter,cost,label='cost') 
    3937ax2.plot(iter,cost,label='cost',marker='o') 
    4038plt.legend() 
  • mystic/README

    r143 r296  
    22 
    33Current release: 
    4     mystic-0.1a2 
     4    mystic-0.2a1 
    55 
    66Installation: 
  • mystic/mystic/abstract_map_solver.py

    r242 r296  
    88a parallel "map" function to enable parallel computing.  This module 
    99describes the map solver interface.  As with the AbstractSolver, the 
    10 "Solve" method must be overwritte with the derived solver's optimization 
     10"Solve" method must be overwritten with the derived solver's optimization 
    1111algorithm. Additionally, for the AbstractMapSolver, a call to self.map 
    1212is required.  In many cases, a minimal function call interface for a 
  • mystic/mystic/abstract_nested_solver.py

    r242 r296  
    88instances -- utilizing a parallel "map" function to enable parallel 
    99computing.  This module describes the nested solver interface.  As with 
    10 the AbstractSolver, the "Solve" method must be overwritte with the derived 
     10the AbstractSolver, the "Solve" method must be overwritten with the derived 
    1111solver's optimization algorithm. Similar to AbstractMapSolver, a call to 
    1212self.map is required.  In many cases, a minimal function call interface for a 
  • mystic/mystic/abstract_solver.py

    r242 r296  
    112112        self.bestSolution     = [0.0] * dim 
    113113        self.trialSolution    = [0.0] * dim 
     114        self.id               = None     # identifier (use like "rank" for MPI) 
    114115 
    115116        self._init_popEnergy  = 1.0E20 #XXX: or numpy.inf? 
  • mystic/mystic/differential_evolution.py

    r251 r296  
    255255        if self._handle_sigint: signal.signal(signal.SIGINT, self.signal_handler) 
    256256 
     257        id = self.id 
    257258        self.probability = CrossProbability 
    258259        self.scale = ScalingFactor 
     
    278279                             
    279280        self.energy_history.append(self.bestEnergy) 
    280         StepMonitor(self.bestSolution[:], self.bestEnergy) 
     281        StepMonitor(self.bestSolution[:], self.bestEnergy, id) 
    281282        self.generations = 0  #XXX: above currently *not* counted as an iteration 
    282283        if callback is not None: 
     
    308309                             
    309310            self.energy_history.append(self.bestEnergy) 
    310             StepMonitor(self.bestSolution[:], self.bestEnergy) 
     311            StepMonitor(self.bestSolution[:], self.bestEnergy, id) 
    311312            self.generations += 1 
    312313            if callback is not None: 
     
    452453        if self._handle_sigint: signal.signal(signal.SIGINT, self.signal_handler) 
    453454 
     455        id = self.id 
    454456        self.probability = CrossProbability 
    455457        self.scale = ScalingFactor 
     
    487489        self.energy_history.append(self.bestEnergy) 
    488490       #FIXME: StepMonitor works for 'pp'? 
    489         StepMonitor(self.bestSolution[:], self.bestEnergy) 
     491        StepMonitor(self.bestSolution[:], self.bestEnergy, id) 
    490492        self.generations = 0  #XXX: above currently *not* counted as an iteration 
    491493        if callback is not None: 
     
    529531            self.energy_history.append(self.bestEnergy) 
    530532           #FIXME: StepMonitor works for 'pp'? 
    531             StepMonitor(self.bestSolution[:], self.bestEnergy) 
     533            StepMonitor(self.bestSolution[:], self.bestEnergy, id) 
    532534            self.generations += 1 
    533535            if callback is not None: 
  • mystic/mystic/nested.py

    r242 r296  
    128128        cf = [cost for i in range(len(initial_values))] 
    129129        tm = [termination for i in range(len(initial_values))] 
     130        id = range(len(initial_values)) 
    130131 
    131132        # generate the local_optimize function 
    132133        local_opt = """\n 
    133 def local_optimize(cost, termination, x0): 
     134def local_optimize(cost, termination, x0, rank): 
    134135    from %s import %s as LocalSolver 
    135136    from mystic import Sow 
     
    141142 
    142143    solver = LocalSolver(ndim) 
     144    solver.id = rank 
    143145    solver.SetInitialPoints(x0) 
    144146""" % (self._solver.__module__, self._solver.__name__) 
     
    156158        exec local_opt 
    157159 
    158         # map:: params, energy, smon, emon = local_optimize(cost,term,x0) 
     160        # map:: params, energy, smon, emon = local_optimize(cost,term,x0,id) 
    159161        mapconfig = dict(nnodes=self._nnodes, launcher=self._launcher, \ 
    160162                         mapper=self._mapper, queue=self._queue, \ 
    161163                         timelimit=self._timelimit, \ 
    162164                         ncpus=self._ncpus, servers=self._servers) 
    163         results = self._map(local_optimize, cf, tm, initial_values, **mapconfig) 
     165        results = self._map(local_optimize, cf, tm, initial_values, id, **mapconfig) 
    164166 
    165167        # get the results with the lowest energy 
     
    183185        # write 'bests' to monitors  #XXX: non-best monitors may be useful too 
    184186        for i in range(len(bestpath.y)): 
    185             StepMonitor(bestpath.x[i], bestpath.y[i]) 
     187            StepMonitor(bestpath.x[i], bestpath.y[i], self.id) 
    186188            #XXX: could apply callback here, or in exec'd code 
    187189        for i in range(len(besteval.y)): 
     
    300302        cf = [cost for i in range(len(initial_values))] 
    301303        tm = [termination for i in range(len(initial_values))] 
     304        id = range(len(initial_values)) 
    302305 
    303306        # generate the local_optimize function 
    304307        local_opt = """\n 
    305 def local_optimize(cost, termination, x0): 
     308def local_optimize(cost, termination, x0, rank): 
    306309    from %s import %s as LocalSolver 
    307310    from mystic import Sow 
     
    313316 
    314317    solver = LocalSolver(ndim) 
     318    solver.id = rank 
    315319    solver.SetInitialPoints(x0) 
    316320""" % (self._solver.__module__, self._solver.__name__) 
     
    333337                         timelimit=self._timelimit, \ 
    334338                         ncpus=self._ncpus, servers=self._servers) 
    335         results = self._map(local_optimize, cf, tm, initial_values, **mapconfig) 
     339        results = self._map(local_optimize, cf, tm, initial_values, id, **mapconfig) 
    336340 
    337341        # get the results with the lowest energy 
     
    355359        # write 'bests' to monitors  #XXX: non-best monitors may be useful too 
    356360        for i in range(len(bestpath.y)): 
    357             StepMonitor(bestpath.x[i], bestpath.y[i]) 
     361            StepMonitor(bestpath.x[i], bestpath.y[i], self.id) 
    358362            #XXX: could apply callback here, or in exec'd code 
    359363        for i in range(len(besteval.y)): 
  • mystic/mystic/scipy_optimize.py

    r242 r296  
    184184        #------------------------------------------------------------- 
    185185 
     186        id = self.id 
    186187        x0 = asfarray(x0).flatten() 
    187188        N = len(x0) #XXX: this should be equal to self.nDim 
     
    206207            allvecs = [sim[0]] 
    207208        fsim[0] = func(x0) 
    208         StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
     209        StepMonitor(sim[0], fsim[0], id) # sim = all values; "best" is sim[0] 
    209210 
    210211        #--- ensure initial simplex is within bounds --- 
     
    227228        self.popEnergy = fsim 
    228229        self.energy_history.append(self.bestEnergy) 
    229         StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
     230        StepMonitor(sim[0], fsim[0], id) # sim = all values; "best" is sim[0] 
    230231 
    231232        iterations = 1 
     
    295296            self.popEnergy = fsim 
    296297            self.energy_history.append(self.bestEnergy) 
    297             StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
     298            StepMonitor(sim[0], fsim[0],id) # sim = all values; "best" is sim[0] 
    298299 
    299300        self.generations = iterations 
     
    511512        #------------------------------------------------------------- 
    512513 
     514        id = self.id 
    513515        x = asfarray(x0).flatten() 
    514516        if retall: 
     
    536538        self.popEnergy[0] = fval  #XXX: pointless? 
    537539        self.energy_history.append(self.bestEnergy) 
    538         StepMonitor(x,fval) # get initial values 
     540        StepMonitor(x, fval, id) # get initial values 
    539541 
    540542        iter = 0; 
     
    589591            self.population[0] = x    #XXX: pointless 
    590592            self.popEnergy[0] = fval  #XXX: pointless 
    591             StepMonitor(x,fval) # get ith values; #XXX: should be [x],[fval] ? 
     593            StepMonitor(x, fval, id) # get ith values; #XXX: should be [x],[fval] ? 
    592594     
    593595        self.generations = iter 
  • mystic/mystic/tools.py

    r247 r296  
    131131        self._x = []    
    132132        self._y = []    
    133  
    134     def __call__(self, x, y): 
     133        self._id = [] 
     134 
     135    def __call__(self, x, y, id=None): 
    135136        from numpy import ndarray 
    136137        if isinstance(x,ndarray): x = list(x) 
    137138        self._x.append(x) 
    138139        self._y.append(y) 
     140        self._id.append(id) 
    139141      
    140142    def get_x(self):    
     
    144146        return self._y 
    145147 
     148    def get_id(self):    
     149        return self._id 
     150 
    146151    x = property(get_x, doc = "Params") 
    147152    y = property(get_y, doc = "Costs") 
     153    id = property(get_id, doc = "Id") 
    148154    pass 
    149155 
     
    162168        self._xinterval = xinterval 
    163169        return 
    164     def __call__(self, x, y): 
     170    def __call__(self, x, y, id=None): 
    165171        from numpy import ndarray 
    166172       #Sow.__call__(self, x, y) 
    167         super(VerboseSow,self).__call__(x, y) 
     173        super(VerboseSow,self).__call__(x, y, id) 
    168174        if isinstance(y,(list,ndarray)): 
    169175            y = y[0] #XXX: get the "best" fit... which should be in y[0] 
     
    172178        if int(self._step % self._yinterval) == 0: 
    173179           #print "Generation %d has best Chi-Squared: %s" % (self._step, y) 
    174             print "Generation %d has best Chi-Squared: %f" % (self._step, y) 
     180            message = "Generation %d has best Chi-Squared: %f" % (self._step, y) 
     181            if id != None: message = "[id: %d] " % (id) + message 
     182            print message 
    175183        if int(self._step % self._xinterval) == 0: 
    176184            if isinstance(x,ndarray): x = list(x) 
    177             print "Generation %d has best fit parameters:\n %s" % (self._step, x) 
     185            message = "Generation %d has best fit parameters:\n %s" % (self._step, x) 
     186            if id != None: message = "[id: %d] " % (id) + message 
     187            print message 
    178188        self._step += 1 
    179189        return 
     
    198208        self._file = open(self._filename,ind) 
    199209        self._file.write("%s\n" % datetime.datetime.now().ctime() ) 
    200         self._file.write("_#_  __ChiSq__  __params__\n") 
     210        self._file.write("___#___  __ChiSq__  __params__\n") 
    201211        self._file.close() 
    202212        return 
    203     def __call__(self, x, y): 
     213    def __call__(self, x, y, id=None): 
    204214        self._file = open(self._filename,'a') 
    205215        from numpy import ndarray 
    206216       #Sow.__call__(self, x, y) 
    207         super(LoggingSow,self).__call__(x, y) 
     217        super(LoggingSow,self).__call__(x, y, id) 
    208218        if isinstance(y,(list,ndarray)): 
    209219            y = y[0] #XXX: get the "best" fit... which should be in y[0] 
     
    212222        if int(self._step % self._yinterval) == 0: 
    213223            if isinstance(x,ndarray): x = list(x) 
    214             self._file.write(" %d   %f   %s\n" % (self._step, y, x)) 
     224            step = [self._step] 
     225            if id != None: step.append(id) 
     226            self._file.write("%s     %f   %s\n" % (tuple(step), y, x)) 
    215227        self._step += 1 
    216228        self._file.close() 
Note: See TracChangeset for help on using the changeset viewer.