Quick Start Guide (Python 3)

by Mark Amo-Boateng (PhD)

Version 0.0.1 (21.04.2020)

The complete source code for this quick start can be downloaded at: https://www.xtellix.com/quickstart.ipynb

Optimizing the Griewank Function in 100 Dimension via REST API

The Griewank function is used to test how good an optimization algorithm can be, especially in terms of computational efficiency, convergence, and ability to find the global minimum. Griewank has servaral local minimum and is usually tested in the range of -600 to 600. More information about the Griewank function can be found at:

1. Pre-requisites

Install xtellixCloud client and Import Required Libraries. XtellixCloud client is built for Python 3.

In [1]:
pip install -i https://test.pypi.org/simple/ xtellixClient==0.0.3
Looking in indexes: https://test.pypi.org/simple/
Requirement already satisfied: xtellixClient==0.0.3 in c:\users\yaw_m\anaconda3\lib\site-packages (0.0.3)
Note: you may need to restart the kernel to use updated packages.
In [2]:
import xtellixCloud.client as xm
In [3]:
import math
import numpy as np
from tqdm import trange
import time

2. Define Your Objective Function and Experimental Conditions

Define your objective function

Here we use the Griewank test function in multiple dimensions

In [4]:
## Example using the Griewank Function. 
## Visit http://www.sfu.ca/~ssurjano/griewank.html for more information

def griewank_function(x, dim):
    """Griewank's function multimodal, symmetric, inseparable """
    partA = 0
    partB = 1
    for i in range(dim):
        partA += x[i]**2
        partB *= math.cos(float(x[i]) / math.sqrt(i+1))
    return 1 + (float(partA)/4000.0) - float(partB)  
In [5]:
##[Optional step]. Create a wrapper for your cost function.
def cost_function(newSuggestions, dim):
    """Generic function wrapper for the cost function """
    return griewank_function(newSuggestions, dim)

Define the Conditions of the Experiments

In [6]:
dim=100     #problem dimension
ubound=600  #upper bound of the Griewank function
lbound=-600 #lower bound of the Griewank function
In [7]:
initMetric = 30000000 #largest possible cost function value - arbitrary very large/low number for minimization/maximization problems respectively
maxIter=dim*200 # maximum number of iterations. We recommend 100 to 200 times the dimension of the problem. and 10 - 50 times for intensive CPU problems
maxSamples=8 # maximum number of default stochastic sampling
iseedId=0 #Seed value for random number generator
minOrMax = True  ### True for MINIMIZATION | False for MAXIMIZATION
isMultiBound = False ## True for parameters with different bounds
In [8]:
x0 = np.ones([dim]) * lbound #initial guess values for your model
In [9]:
## Verify that the wrapper is working correctly
fobj = cost_function(x0, dim)
initMetric = fobj #Optional: use the first value as initial metric
print("Initial Objective Function Value = ",fobj)
Initial Objective Function Value =  9001.0

3. Connect and Auto-Tune your model

Supscribe and get your server access credentials

If you have not already done so, supscribe or request access to the optimization servers at https://www.xtellix.com. Once approved you will receive the following typical server access credentials:

Optimization Server Access Protocols

  • Server Endpoint: http://eu1.xtellixapp.com
  • Server Zone: EU 1
  • Sever ID: abcd1234
  • Client Secret: 1234567890
  • Server Secret: 1234567890
  • Total Parameters: 100,000,000 (double precision)
  • Number of Servers: 1

Connecting to the Xtellix Optimization Services follows this format: {protocol}://{ServerEndpoint}/{serviceEndpoint}/{apiVersion}/{serverID}/{actionAPI}

For Core Optimization, the {serviceEndpoint} is core; and the {apiVersion} is v0.01. Therefore, using the above credentials as an example, the Service API Endpoint for Core Optimization is:

http://eu1.xtellixapp.com/core/v0.01/abcd1234

Note that the {actionAPI} is supplied by the client library; and there is NO trailing / or \.

Service API Endpoint: http://eu1.xtellixapp.com/core/v0.01/abcd1234

Connect and initialize the optimization engine

In [10]:
#set server_API_endpoint and client_secret_token as variables
#http://eu1.xtellixapp.com/core/v0.01/{serverID}

service_API_endpoint = "http://localhost/core/v0.01/demo1"

#service_API_endpoint = "http://eu1.xtellixapp.com/core/v0.01/abcd1234"

client_secret = 1234567890

#Initialize connection and watch for errors
xm.connect(service_API_endpoint, client_secret)
*******************************************************
Server Version: 
*******************************************************
Optimization Engine Server Info:
Copyright(C) 2010 - 2020 Dr Mark Amo - Boateng m.amoboateng@gmail.com
Server Version 0.0.3 beta

*******************************************************
Client Version: 
*******************************************************
Copyright (C) 2010-2020 Dr Mark Amo-Boateng m.amoboateng@gmail.com
Client Version: 0.0.1 beta
*******************************************************
In [11]:
  xm.initializeOptimizer(initMetric,ubound, lbound, dim, maxIter, maxSamples, x0, iseedId,minOrMax,isMultiBound)
New Server Secret:  144342485
Optimization Engine Running.....
Out[11]:
<Response [200]>

Optimization Loop: Test 3 key steps

In [12]:
#Test the call functions
#The 3 steps in each optimization loop

##LOOP till condition
    #newParameters from cloud server
    #calculate new cost function
    #update cost function on server
    
newSuggestions = xm.getParameters()
fobj = cost_function(newSuggestions, dim)
xm.updateObjectiveFunctionValue(fobj)
Out[12]:
<Response [200]>

Optimization Loop: Run and Auto-Tune Your Parameters

In [13]:
#OPtional Step: Use TQDM Library for nice progress bar 
with trange(maxIter) as t:
    for i in t:
        
        ##Resume if Connection Terminates              
        try:
            ##a: Get parameters from Optimization Engine
            newSuggestions = xm.getParameters()

            ##b: Compute new cost function value based on the parameters
            fobj = cost_function(newSuggestions, dim)

            ##c: Send new cost function value to optimization server
            xm.updateObjectiveFunctionValue(fobj)
        except:
            print("#", i, "Connection Interrupted. Resuming automatically in 30 seconds")            
            time.sleep(30)
            pass
        
        ##Optional step: Check the progress of the optmization
        obj,pareato,_,svrit = xm.getProgress()        

        ###Optional step: Update the progress bar
        t.set_description('Function Eval %i' % i)
        t.set_postfix(current=obj, best=pareato)
Function Eval 8147:  41%|████      | 8147/20000 [01:51<05:07, 38.59it/s, best=0.0635, current=0.0636]  
# 8148 Connection Interrupted. Resuming automatically in 30 seconds
Function Eval 19999: 100%|██████████| 20000/20000 [05:11<00:00, 64.27it/s, best=6.98e-10, current=7e-10]   

Get final results from server

In [14]:
x0 = xm.getParameters(False) #Get non-cached version of final parameters
fobj = cost_function(x0, dim)
print(fobj)
print(x0)
7.645503119491082e-10
[-1.14988803e-05  4.81410127e-06 -1.73347074e-06 -1.33106765e-07
  2.75766391e-06  5.16012254e-06  1.76732738e-07  1.29508095e-06
 -1.06868185e-05 -5.55126069e-07  1.51278867e-06 -2.25684339e-05
 -4.02522355e-07 -1.18104044e-04 -2.26608422e-06 -1.04024395e-06
 -2.87826868e-06 -1.45386353e-06  8.84988421e-07  1.22712481e-06
 -6.97131233e-07  1.10391058e-07 -4.33958248e-07  1.79536981e-07
  1.75687427e-06  5.70825135e-06  1.03145330e-06 -5.60001895e-06
 -1.86629025e-07 -2.20934651e-06 -8.68615216e-08  5.59689909e-06
  2.65756871e-07  6.41117796e-07 -1.94036645e-06 -7.64600207e-07
  2.16802800e-08 -2.69098301e-06  1.57219933e-07  7.87981932e-06
 -1.49206305e-07  1.04141964e-07  1.23778788e-06 -9.34298621e-08
  6.25101884e-06 -6.63085637e-07  1.23412189e-06  1.57779423e-07
  9.61519994e-08  1.26947992e-06  1.29940761e-06 -1.89609557e-06
 -1.61123606e-07  5.04230339e-07 -8.13565140e-07  1.78236656e-07
 -5.38051702e-07  1.58561983e-06 -6.63063617e-07 -7.47191217e-06
 -3.31974182e-07  2.61341724e-07  1.54100120e-07 -5.92112634e-06
 -3.62842004e-07  4.62474193e-06 -6.38550560e-06  8.46070968e-07
  6.34783338e-08 -4.30519919e-06 -8.64826820e-07  1.14096567e-07
 -1.99062885e-07 -3.56559892e-07  3.48874276e-06 -2.17506994e-06
 -8.23313903e-06 -1.82437413e-07  8.04836225e-07 -7.64137001e-08
  7.78635786e-07 -1.46441160e-06 -1.16580561e-04 -9.20613021e-08
  1.14693371e-06  4.92541565e-06  1.75805201e-06 -1.18282299e-06
 -6.22876943e-07  1.10034107e-06 -1.08586042e-04 -2.93469252e-06
 -3.44970841e-07 -5.45043726e-06 -1.46105676e-06 -1.75939740e-08
  3.20733339e-06 -5.26373674e-07  2.06658718e-06  1.34662680e-06]

Download the Python Notebook file with the complet codes: https://www.xtellix.com/quickstart.ipynb

In [ ]: