Package: algormeter
AlgorMeter: Tool for developing, testing, measuring and exchange optimizers algorithmsAlgorMeter is a python implementation of an  environment for develop, test, measure, report and  compare optimization algorithms.
Having a common platform that simplifies developing, testing and exchange of optimization algorithms allows for better collaboration and sharing of resources among researchers in the field. This can lead to more efficient development and testing of new algorithms, as well as faster progress in the field overall.
AlgorMeter produces comparative measures among algorithms  in csv format with effective test function call count.It embeds a specific feature devoted to optimize the number of function calls, so that multiple function  calls at the same point are accounted for just once, without storing intermediate results, with benefit in terms of algorithm coding.AlgorMeter contains a standard library of 10 DC problems and 7 convex problems for testing algorithms. More problem collections can be easily added.AlgorMeter provide integrated performance profiles graphics, as developed by E. D. Dolan and J. J. More. They are a powerful standard tool, within the optimization community, to assess the performance of optimization software.problems + algorithms = experimentsA problem is a function f where f: R(n) -> R with n called dimension.f = f1() - f2() difference of convex function where f1, f2: R(n) -> R.'problems' is a list of problem'algorithm' is a code that try to find problem local minima'experiment' is an algorMeter run with a list of problems and a list of algorithms that produce a result reportHow to use...Implement an algorithm...Copy and customize algorithm examples like the following(there are many included example?.py)defgradient(p,**kwargs):'''Simple gradient'''forkinp.loop():p.Xkp1=p.Xk-1/(k+1)*p.gfXk/np.linalg.norm(p.gfXk)and refer to the available following system propertiesalgorMeter propertiesDescriptionk, p.Kcurrent iterationp.Xkcurrent pointp.Xkp1next point.to be set for next iterationp.fXkp.f(p.Xk) = p.f1(p.Xk) - p.f2(p.Xk)p.fXkPrevprevious iteration f(x)p.f1Xkp.f1(p.Xk)p.f2Xkp.f1(p.Xk)p.gfXkp.gf(p.Xk) = p.gf1(p.Xk) - p.gf2(p.Xk)p.gf1Xkp.gf1(p.Xk)p.gf2Xkp.gf2(p.Xk)p.optimumPointOptimum Xp.optimumValuep.f(p.optimumPoint)p.XStartStart Pointto determine the p.Xkp1 for the next iteration....and run it:df,pv=algorMeter(algorithms=[gradient],problems=probList_covx,iterations=500,absTol=1E-2)print('\n',pv,'\n',df)pv and df are pandas dataframe with run result.A .csv file with result is also created in csv folder.(see example*.py)AlgorMeter interfacedefalgorMeter(algorithms,problems,tuneParameters=None,iterations=500,timeout=180runs=1,trace=False,dbprint=False,csv=True,savedata=False,absTol=1.E-4,relTol=1.E-5,**kwargs):algorithms: algorithms list.(algoList_simple is available )problems: problem list. See problems list in example4.py for syntax.(probList_base, probList_covx, probList_DCJBKM are available)tuneParameters = None: see tuneParameters sectioniterations = 500: max iterations numbertimeout = 180: time out in secondsruns = 1: see random sectiontrace = False: see trace sectiondbprint= False: see dbprint sectioncsv = True: write a report in csv format in csv foldersavedata = False: save data in data folderabsTol =1.E-4, relTol = 1.E-5: tolerance used in numpy allClose and isClose**kwargs: python kwargs propagated to algorithmscall to algorMeter returns two pandas dataframe p1, p2. p2 is a success and fail summary count.
p1 is a detailed report with the following columns.ProblemDimAlgorithmStatus: Success, Fail or ErrorIterationsf(XStarf(BKXStar)Delta: absolute difference between  f(XStar) and f(BKXStar)SecondsStart pointXStar: minimumBKXStar: best known minum\f1	f2 gf1	gf2: effective calls count... : other columns with count to counter.up utility (see below)Stop and success conditiondefstop(self)->bool:'''return True if experiment must stop. Override it if needed'''returnbool(np.isclose(self.fXk,self.fXkPrev,rtol=self.relTol,atol=self.absTol)ornp.allclose(self.gfXk,np.zeros(self.dimension),rtol=self.relTol,atol=self.absTol))defisSuccess(self)->bool:'''return True if experiment success. Override it if needed'''returnself.isMinimum(self.XStar)can be overriden like indefstop():returnstatus...p.stop=stopp.isSuccess=stopAnother maybe more simple way is to call statement break in main loop.See example3.pyProblems function call optimizationAlgorMeter embeds a specific feature devoted to optimize the number of function calls, so that multiple function  calls at the same point are accounted for just once, without storing intermediate results, with benefit in terms of algorithm coding.  So in algorithm implementation is not necessary to store the previous result in variables to reduce f1, f2, gf1, gf2 function calls. AlgorMeter cache 128 previous calls to obtain such automatic optimization.Problems ready to useImporting 'algormeter.libs' probList_base, probList_covx, probList_DCJBKM problems list are available.probList_DCJBKMcontains ten frequently used unconstrained DC optimization problems, where objective functions are presented as DC (Difference of Convex) functions:
𝑓(𝑥)=𝑓1(𝑥)−𝑓2(𝑥).Joki, BagirovprobList_covxcontains  DemMal,Mifflin1, Miffilin2,LQ,QL,MAXQ,MAXL,CB2,CB3,MaxQuad, Rosen, Shor, TR48, A48 and Goffin test convex functions/problemprobList_no_covxcontains special no convex functions: Rosenbrock, CrescentprobList_basecontains Parab, ParAbs, Acad simple functions for algorithms early development and test.See 'ProblemsLib.pdf'CountersInstruction likecounter.up('lb<0', cls='qp')is used to count events in code, summerized in statistics at the end of experiment as a column, available in dataframe returned by call to algorMeter and in final csv.
For the code above a column with count of counter.up calls and head 'qp.lb>0' is produced.Also are automatically available columns 'f1', 'f2', 'gf1', 'gf1' with effective calls to f1, f2, gf1, gf2See example3.pydbprint = TrueInstruction dbx.print produce print out only if algorMeter call has option dbprint == Truedbx.print('t:',t, 'Xprev:',Xprev, 'f(Xprev):',p.f(Xprev) ).See example3.pyNB: If dbprint = True python exceptions are not handled and raised.Trace == TrueIf Default.TRACE = True a line with function values are shown as follows in the console for each iteration for algorithms analysis purpose.Acad-2 k:0,f:-0.420,x:[ 0.7 -1.3],gf:[ 1.4 -0.6],f1:2.670,gf1:[ 3.1 -2.9],f2:3.090,gf2:[ 1.7 -2.3]Acad-2 k:1,f:-1.816,x:[-1.0004 -0.5712],gf:[-8.3661e-04  8.5750e-01],f1:0.419,gf1:[-2.0013 -0.7137],f2:2.235,gf2:[-2.0004 -1.5712]Acad-2 k:2,f:-1.754,x:[-0.9995 -1.4962],gf:[ 9.6832e-04 -9.9250e-01],f1:2.361,gf1:[-1.9985 -3.4887],f2:4.115,gf2:[-1.9995 -2.4962]These lines represent the path followed by the algorithm for the specific problem.NB: If trace = True python exceptions are not handled and raised.See example3.pytuneParametersSome time is necessary tune some parameter combinations.  Procede as follow (See example4.py):Define and use module, non locals parameters in your algo code.Define a list of lists with possible values of tuning parameters as follows:tpar=[# [name, [values list]]('alpha',[1.+iforiinnp.arange(.05,.9,.05)]),# ('beta', [1. + i for i in np.arange(.05,.9,.05)]),]call algorMeter with csv = True and tuneParameters= like tuneParameters=tpar.open csv file produced and analyze the performance of parameters combinations by looking column '# TuneParams'. Useful is a pivot table on such columns.
See example4.pyRandom start pointIf algorMeter parameter run is set with a number greater than 1, each algorithm is repeated on the same problem with random start point in range -1 to 1 for all dimensions.
By the method setRandom(center, size) random X can be set in [center-size, center+size] interval.See example5.pyRecord datawith option data == True store in 'npy' folder one file in numpy format, for each experiment with X and Y=f(X) for all iterations.
It is a numpy array with:X = data[:,:-1]Y = data[:,-1]File name is like 'gradient,JB05-50.npy'.These files are read by viewer.py data visualizer.Performance ProfilePerformance profiles graphics, as developed by E. D. Dolan and J. J. More, are a powerful tool to assess the performance of optimization software. For this reason they are standard accepted within the optimization community. See example2.pydf,pv=algorMeter(algorithms=algorithms,...)perfProf(df,costs=['f1','Iterations'])# df: first pandas dataframe output of algormeter call# costs: list of column labels in dfplt.show(block=True)It is possible to graph performance profiles by preparing a pandas data frame using a spreadsheet with the mandatory columns 'Problem','Dim','Algorithm','Status' and the columns costs that you want to drawdf=pd.read_excel(r'Path of Excel file\File name.xlsx',sheet_name='your Excel sheet name')perfProf(df,costs=['cost1','cost2'])plt.show(block=True)MinimizeIn case you need to find the minimum of a problem/function by applying an algorithm developed with algormeter, the minimize method is available. (See example6.py):p=MyProb(K)found,x,y=p.minimize(myAlgo)Visualizer.pyRunning visualizer.py produce or updates contour image in folder 'pics' for each experiment with dimension = 2 with data in folder 'npy'.Examples indexexample1.py: Simplest possible exampleexample2.py: Dolan, More performance profileexample3.py: dbx.print, trace,counter.up, counter.log, override stop, break  exampleexample4.py: algorithm parameters tuningexample5.py: multiple run of each problem with random start pointexample6.py: minimize new problem with algometer algorithmContributingYou can download or fork the repository freely.https://github.com/xedla/algormeterIf you see a mistake you can send me a mail atpietrodalessandro@gmail.comIf you open up a ticket, please make sure it describes the problem or feature request fully.Any suggestion are welcome.LicenseIf you use AlgorMeter for the preparation of a scientific paper, the citation with a link to this repository would be appreciated.This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.InstallationAlgormeter is available as pypi pip package.pip3installalgormeterDependenciesPython version at leastPython 3.10.6Package installable with pip3numpypandasmatplotlibAlgormeter plays well withVisual Studio Codeand in jupyter