def Target_Terms(self, mvals, Order=0, verbose=False, customdir=None): ## This is the objective function; it's a dictionary containing the value, first and second derivatives Objective = {'X':0.0, 'G':np.zeros(self.FF.np), 'H':np.zeros((self.FF.np,self.FF.np))} # Loop through the targets, stage the directories and submit the Work Queue processes. for Tgt in self.Targets: Tgt.stage(mvals, AGrad = Order >= 1, AHess = Order >= 2, customdir=customdir) if self.asynchronous: # Asynchronous evaluation of objective function and Work Queue tasks. # Create a list of the targets, and remove them from the list as they are finished. Need2Evaluate = self.Targets[:] # This ensures that the OrderedDict doesn't get out of order. for Tgt in self.Targets: self.ObjDict[Tgt.name] = None # Loop through the targets and compute the objective function for ones that are finished. while len(Need2Evaluate) > 0: for Tgt in Need2Evaluate: if Tgt.wq_complete(): # List of functions that I can call. Funcs = [Tgt.get_X, Tgt.get_G, Tgt.get_H] # Call the appropriate function Ans = Funcs[Order](mvals, customdir=customdir) # Print out the qualitative indicators if verbose: Tgt.meta_indicate(customdir=customdir) # Note that no matter which order of function we call, we still increment the objective / gradient / Hessian the same way. if not in_fd(): self.ObjDict[Tgt.name] = {'w' : Tgt.weight/self.WTot , 'x' : Ans['X']} for i in range(3): Objective[Letters[i]] += Ans[Letters[i]]*Tgt.weight/self.WTot Need2Evaluate.remove(Tgt) break else: pass else: wq = getWorkQueue() if wq is not None: wq_wait(wq) for Tgt in self.Targets: # The first call is always done at the midpoint. Tgt.bSave = True # List of functions that I can call. Funcs = [Tgt.get_X, Tgt.get_G, Tgt.get_H] # Call the appropriate function Ans = Funcs[Order](mvals, customdir=customdir) # Print out the qualitative indicators if verbose: Tgt.meta_indicate(customdir=customdir) # Note that no matter which order of function we call, we still increment the objective / gradient / Hessian the same way. if not in_fd(): self.ObjDict[Tgt.name] = {'w' : Tgt.weight/self.WTot , 'x' : Ans['X']} for i in range(3): Objective[Letters[i]] += Ans[Letters[i]]*Tgt.weight/self.WTot # The target has evaluated at least once. for Tgt in self.Targets: Tgt.evaluated = True # Safeguard to make sure we don't have exact zeros on Hessian diagonal for i in range(self.FF.np): if Objective['H'][i,i] == 0.0: Objective['H'][i,i] = 1.0 return Objective
def Target_Terms(self, mvals, Order=0, verbose=False, customdir=None): ## This is the objective function; it's a dictionary containing the value, first and second derivatives Objective = {'X':0.0, 'G':np.zeros(self.FF.np), 'H':np.zeros((self.FF.np,self.FF.np))} # Loop through the targets, stage the directories and submit the Work Queue processes. for Tgt in self.Targets: Tgt.stage(mvals, AGrad = Order >= 1, AHess = Order >= 2, customdir=customdir) if self.asynchronous: # Asynchronous evaluation of objective function and Work Queue tasks. # Create a list of the targets, and remove them from the list as they are finished. Need2Evaluate = self.Targets[:] # This ensures that the OrderedDict doesn't get out of order. for Tgt in self.Targets: self.ObjDict[Tgt.name] = None # Loop through the targets and compute the objective function for ones that are finished. while len(Need2Evaluate) > 0: for Tgt in Need2Evaluate: if Tgt.wq_complete(): # List of functions that I can call. Funcs = [Tgt.get_X, Tgt.get_G, Tgt.get_H] # Call the appropriate function Ans = Funcs[Order](mvals, customdir=customdir) # Print out the qualitative indicators if verbose: Tgt.meta_indicate(customdir=customdir) # Note that no matter which order of function we call, we still increment the objective / gradient / Hessian the same way. if not in_fd(): self.ObjDict[Tgt.name] = {'w' : Tgt.weight/self.WTot , 'x' : Ans['X']} for i in range(3): Objective[Letters[i]] += Ans[Letters[i]]*Tgt.weight/self.WTot Need2Evaluate.remove(Tgt) break else: pass else: wq = getWorkQueue() if wq != None: wq_wait(wq) for Tgt in self.Targets: # The first call is always done at the midpoint. Tgt.bSave = True # List of functions that I can call. Funcs = [Tgt.get_X, Tgt.get_G, Tgt.get_H] # Call the appropriate function Ans = Funcs[Order](mvals, customdir=customdir) # Print out the qualitative indicators if verbose: Tgt.meta_indicate(customdir=customdir) # Note that no matter which order of function we call, we still increment the objective / gradient / Hessian the same way. if not in_fd(): self.ObjDict[Tgt.name] = {'w' : Tgt.weight/self.WTot , 'x' : Ans['X']} for i in range(3): Objective[Letters[i]] += Ans[Letters[i]]*Tgt.weight/self.WTot # The target has evaluated at least once. for Tgt in self.Targets: Tgt.evaluated = True # Safeguard to make sure we don't have exact zeros on Hessian diagonal for i in range(self.FF.np): if Objective['H'][i,i] == 0.0: Objective['H'][i,i] = 1.0 return Objective