Package: adannealing
StatusCompatibilitiesContactAdAnnealingA package doing simulated annealingInstallationgit clone https://github.com/pcotteadvestis/adannealing
cd adannealing
pip install .UsageSimple usage :fromadannealingimportAnnealerclassLossFunc2D:def__init__(self):self.constraints=Nonedef__call__(self,w)->float:"""A __call__ method must be present. It will be called to evaluate the loss. The argument passed is theparameter value at which the loss has to be computed."""x=w[0]y=w[1]return(x-5)*(x-2)*(x-1)*x+10*y**2defon_fit_start(self,val):"""This method is called by the fitter before optimisation. The argument passed is either the starting point of theoptimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used"""passdefon_fit_end(self,val):"""This method is called by the fitter after optimisation. The argument passed is either the result of theoptimiser (for single annealer) or the list of results if more than one annealer reache the end of fit."""passinit_states,bounds,acceptance=(3.0,0.5),np.array([[0,5],[-1,1]]),0.01ann=Annealer(loss=LossFunc2D(),weights_step_size=0.1,init_states=init_states,# Optionalbounds=bounds,verbose=True)# Weights of local minimum, and loss at local minimumw0,lmin,_,_,_,_=ann.fit(stopping_limit=acceptance)Use multiple initial states in parallel runs and get one output per init states :fromadannealingimportAnnealerAnnealer.set_parallel()classLossFunc2D:def__init__(self):self.constraints=Nonedef__call__(self,w)->float:"""A __call__ method must be present. It will be called to evaluate the loss. The argument passed is theparameter value at which the loss has to be computed."""x=w[0]y=w[1]return(x-5)*(x-2)*(x-1)*x+10*y**2defon_fit_start(self,val):"""This method is called by the fitter before optimisation. The argument passed is either the starting point of theoptimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used"""passdefon_fit_end(self,val):"""This method is called by the fitter after optimisation. The argument passed is either the result of theoptimiser (for single annealer) or the list of results if more than one annealer reache the end of fit."""passbounds,acceptance,n=np.array([[0,5],[-1,1]]),0.01,5ann=Annealer(loss=LossFunc2D(),weights_step_size=0.1,bounds=bounds,verbose=True)# Iterable of n weights of local minimum and loss at local minimumresults=ann.fit(npoints=n,stopping_limit=acceptance)forw0,lmin,_,_,_,_inresults:"""do something"""Use multiple initial states in parallel runs and get the result with the smallest loss :fromadannealingimportAnnealerAnnealer.set_parallel()classLossFunc2D:def__init__(self):self.constraints=NoneclassLossFunc2D:def__init__(self):self.constraints=Nonedef__call__(self,w)->float:"""A __call__ method must be present. It will be called to evaluate the loss. The argument passed is theparameter value at which the loss has to be computed."""x=w[0]y=w[1]return(x-5)*(x-2)*(x-1)*x+10*y**2defon_fit_start(self,val):"""This method is called by the fitter before optimisation. The argument passed is either the starting point of theoptimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used"""passdefon_fit_end(self,val):"""This method is called by the fitter after optimisation. The argument passed is either the result of theoptimiser (for single annealer) or the list of results if more than one annealer reache the end of fit."""passbounds,acceptance,n=np.array([[0,5],[-1,1]]),0.01,5ann=Annealer(loss=LossFunc2D(),weights_step_size=0.1,bounds=bounds,verbose=True)# Weights of the best local minimum and loss at the best local minimumw0,lmin,_,_,_,_=ann.fit(npoints=n,stopping_limit=acceptance,stop_at_first_found=True)One can save the history of the learning by giving a path :fromadannealingimportAnnealerAnnealer.set_parallel()classLossFunc2D:def__init__(self):self.constraints=Nonedef__call__(self,w)->float:"""A __call__ method must be present. It will be called to evaluate the loss. The argument passed is theparameter value at which the loss has to be computed."""x=w[0]y=w[1]return(x-5)*(x-2)*(x-1)*x+10*y**2defon_fit_start(self,val):"""This method is called by the fitter before optimisation. The argument passed is either the starting point of theoptimiser (for single annealer) or the tuple containing different starting points if more than one annealer is used"""passdefon_fit_end(self,val):"""This method is called by the fitter after optimisation. The argument passed is either the result of theoptimiser (for single annealer) or the list of results if more than one annealer reache the end of fit."""passbounds,acceptance,n=np.array([[0,5],[-1,1]]),0.01,5ann=Annealer(loss=LossFunc2D(),weights_step_size=0.1,bounds=bounds,verbose=True)# Weights of the best local minimum and loss at the best local minimumw0,lmin,_,_,_,_=ann.fit(npoints=n,stopping_limit=acceptance,history_path="logs")In this example, callingfitwill producendirectories inlogs, each containing 2 files:history.csvandreturns.csv.
The first is the entier history of the fit, the second is only the iteration that found the local minimum.
If only one point is asked (either by usingnpoints=1orstop_at_first_found=True), will producehistory.csvandreturns.csvdirectly inlogs, and will delete the subfolders of the runs that did not produce the local minimum.One can plot the result of a fit by doing# figure will be saved in logs/annealing.pdffig=ann.plot("logs",nweights=2,weights_names=["A","B","C"],do_3d=True)If the argumentdo_3d=True, then 3-dimensional dynamical figures are produced to inspect the phase space marginalised over different couples of components.