Package: xtellixClient
Python Client to Connect Solve-Hub xtellix Optimization ServerDraft Version 0.0.1This is a simple example usage of how to initialize the Optimization Engine Server and Perform Optimization on your objective functions.The remaining documents is in two (2) parts:Section A: Initializing Server; andSection B: Running the Optimization Loop.SECTION APREREQUISITES: INSTALL KEY LIBRARIES & INITIALIZE SERVERInstall xtellixClient using pip commandhttps://pypi.org/project/xtellixClient-0.0.1/Read more atGithubpip install xtellixClientSTEP 1A: IMPORT XTELLIX CLIENT LIBRARIESImport the xtellixClient moduleimportxtellixClient.xtellixClientasxmSTEP 1B: IMPORT OTHER KEY LIBRARIESimportmathimportnumpyasnpfromtqdmimporttrangeimporttimeSTEP 2: OBJECTIVE FUNCTIONDefine your cost or objective functionHere we define the Griewank function as en example. More infomation about the Griewank benchmark function can be found on theweb.defgriewank_function(x,dim):"""Griewank's function 	multimodal, symmetric, inseparable """sumPart=0prodPart=1foriinrange(dim):sumPart+=x[i]**2prodPart*=math.cos(float(x[i])/math.sqrt(i+1))return1+(float(sumPart)/4000.0)-float(prodPart)OPTIONAL STEP 2B: COST FUNCTION WRAPPERS FOR THE OBJECTIVE FUNCTIONTo make it easier to dynamically call other benchmark functions without changing much of the code, we recommend defining a general purpose wrapper to be called during the optimization processdefcost_function(newSuggestions,dim):"""Generic function wrapper for the cost function """returngriewank_function(newSuggestions,dim)STEP 3: INITIALIZE CONNECTION TO THE OPTIMIZATION SERVERConnect to your unique optimization server using your provided credentials:server_endpoint, andclient_secret_token. These are two are used to established a secured successful connection before you can begin any optimization project. Watch for server connection errors and contact the support team for assistance#set server_endpoint and client_secret_token as variablessever_endpoint="http://127.0.0.1:5057"client_secret=1234567890#Initialize connection and watch for errorsxm.connect(sever_endpoint,client_secret)STEP 4: INITIALIZE THE OPTIMIZATION ENGINELet's begin by setting up all the initial parameters for the objective function, then the optimization enginea. Initial parameters for the Cost Functionubound=600#upper bound of the Griewank functionlbound=-600#lower bound of the Griewank functiondim=100#problem dimensionb. Optimization Engine SettingsinitMetric=30000000#largest possible cost function value - arbitrary very large/low number for minimization/maximization problems respectivelymaxIter=dim*200# maximum number of iterations. We recommend 100 to 200 times the dimension of the problem. and 10 - 50 times for intensive CPU problemsmaxSamples=8# maximum number of default stochastic samplingiseedId=0#Seed value for random number generatorminOrMax=True### True for MINIMIZATION | False for MAXIMIZATIONc. Prepare the initial parameter valuex0=np.ones([dim])*lboundd. Compute the first objective functionfobj=cost_function(x0,dim)initMetric=fobj#Optional: use the first value as initial metricprint("Initial Objective Function Value = ",fobj)e. Initialize Optimization Enginexm.initializeOptimizer(initMetric,ubound,lbound,dim,maxIter,maxSamples,x0,iseedId,minOrMax)SECTION BTHE OPTIMIZATION LOOP: SOLVING YOUR OPTIMIZATION PROBLEM3 SIMPLE STEPS: GET -> COMPUTE -> UPDATESolving the optimizatin problem (here: Griewank function) is done in the following three (3) steps:a. Get new suggested parameters from the optimization servernewSuggestions=xm.getParameters()b. Compute new cost function based on the new parametersfobj=cost_function(newSuggestions,dim)c. Send new cost function value to the optimization serverxm.updateObjectiveFunctionValue(fobj)d. Repeat the whole process until optimization is achievedThe whole process can be summarized below:The Optimization Loop with comments#OPtional Step: Use TQDM Library for nice progress barwithtrange(maxIter)ast:foriint:##a: Get parameters from Optimization EnginenewSuggestions=xm.getParameters()##b: Compute new cost function value based on the parametersfobj=cost_function(newSuggestions,dim)##c: Send new cost function value to optimization serverxm.updateObjectiveFunctionValue(fobj)##Optional step: Check the progress of the optmizationobj,pareato,_,svrit=xm.getProgress()###Optional step: Update the progress bart.set_description('Function Eval%i'%i)t.set_postfix(current=obj,best=pareato)The Optimization Loop WITHOUT commentsWe see the simplicity of the process without the commentsforiinrange(maxIter):newSuggestions=xm.getParameters()fobj=cost_function(newSuggestions,dim)xm.updateObjectiveFunctionValue(fobj)GET FINAL PARAMETERS FROM SERVERGet the optimized parametersx0=xm.getParameters()Or Get the optimized parameter (force download a fresh copy from the server)x0=xm.getParameters(False)Calculate the final objective function valuefobj=cost_function(x0,dim)Print final objective function value and optimized parametersprint(fobj)print(x0)The full code for the above exampleimportxtellixClient.xtellixClientasxmimportmathimportnumpyasnpfromtqdmimporttrangedefgriewank_function(x,dim):"""Griewank's function 	multimodal, symmetric, inseparable """sumPart=0prodPart=1foriinrange(dim):sumPart+=x[i]**2prodPart*=math.cos(float(x[i])/math.sqrt(i+1))return1+(float(sumPart)/4000.0)-float(prodPart)defcost_function(newSuggestions,dim):"""Generic function wrapper for the cost function """returngriewank_function(newSuggestions,dim)#set server_endpoint and client_secret_token as variablessever_endpoint="http://127.0.0.1:5057"client_secret=1234567890#Initialize connection and watch for errorsxm.connect(sever_endpoint,client_secret)ubound=600lbound=-600dim=100initMetric=30000000maxIter=dim*200maxSamples=8iseedId=0minOrMax=True## True for MINIMIZATION | False for MAXIMIZATIONx0=np.ones([dim])*lboundfobj=cost_function(x0,dim)print("Initial Objective Function Value = ",fobj)xm.initializeOptimizer(initMetric,ubound,lbound,dim,maxIter,maxSamples,x0,iseedId,minOrMax)##OPTIMIZATION LOOPforiinrange(maxIter):newSuggestions=xm.getParameters()fobj=cost_function(newSuggestions,dim)xm.updateObjectiveFunctionValue(fobj)##Optional step: Check the progress of the optmizationobj,pareato,feval,_=xm.getProgress()print("Feval = ",feval," Best Objective = ",pareato," Current Objective = ",obj)x0=xm.getParameters(False)fobj=cost_function(x0,dim)print(fobj)print(x0)