一个Python客户机,用于使用restapi连接到xtellix优化服务器
xtellixClient的Python项目详细描述
Python客户端连接Solve Hub xtellix优化服务器
草案版本0.0.1
下面是一个简单的引擎初始化和优化的例子。其余文档分为两(2)部分:部分A:初始化服务器;和节B:运行优化循环。
A部分
先决条件:安装密钥库和初始化服务器
Install xtellixClient使用pip命令https://pypi.org/project/xtellixClient-0.0.1/ 更多信息请访问Github
pip install xtellixClient
步骤1A:导入XTELLIX客户端库
导入xtellixClient模块
^{pr2}$步骤1B:导入其他密钥库
importmathimportnumpyasnpfromtqdmimporttrangeimporttime
第二步:目标函数
定义成本或目标函数 这里我们将Griewank函数定义为en示例。有关Griewank基准函数的更多信息可以在web上找到。在
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)
可选步骤2B:目标函数
为了使动态调用其他基准函数更容易,而不需要更改太多代码,我们建议定义一个在优化过程中调用的通用包装器
defcost_function(newSuggestions,dim):"""Generic function wrapper for the cost function """returngriewank_function(newSuggestions,dim)
步骤3:初始化与优化服务器的连接
使用您提供的凭据连接到您唯一的优化服务器:server\u endpoint,和client_secret_token。这两个用于在开始任何优化项目之前建立安全的成功连接。注意服务器连接错误,并与支持团队联系以获得帮助
#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)
步骤4:初始化优化引擎
让我们首先设置目标函数的所有初始参数,然后设置优化引擎
a。成本函数的初始参数
ubound=600#upper bound of the Griewank functionlbound=-600#lower bound of the Griewank functiondim=100#problem dimension
b。优化引擎设置
initMetric=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 MAXIMIZATION
c。准备初始参数值
x0=np.ones([dim])*lbound
d.计算第一个目标函数
fobj=cost_function(x0,dim)initMetric=fobj#Optional: use the first value as initial metricprint("Initial Objective Function Value = ",fobj)
e.初始化优化引擎
xm.initializeOptimizer(initMetric,ubound,lbound,dim,maxIter,maxSamples,x0,iseedId,minOrMax)
B部分
优化循环:解决优化问题
3个简单步骤:获取->计算->更新
解决优化问题(此处:Griewank函数)分以下三(3)步完成: a。从优化服务器获取新的建议参数
newSuggestions=xm.getParameters()
b。根据新参数计算新的成本函数
fobj=cost_function(newSuggestions,dim)
c。将新的成本函数值发送到优化服务器
xm.updateObjectiveFunctionValue(fobj)
d。重复整个过程,直到实现优化 整个过程总结如下:
带注释的优化循环
#OPtional Step: Use TQDM Library for nice progress bar withtrange(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)
没有注释的优化循环
我们看到了没有注释的过程的简单性
foriinrange(maxIter):newSuggestions=xm.getParameters()fobj=cost_function(newSuggestions,dim)xm.updateObjectiveFunctionValue(fobj)
从服务器获取最终参数
获取优化参数
x0=xm.getParameters()
或获取优化参数(从服务器强制下载新副本)
x0=xm.getParameters(False)
计算最终目标函数值
fobj=cost_function(x0,dim)
打印最终目标函数值和优化参数
print(fobj)print(x0)
上面例子的完整代码
^{pr21}$- 项目
标签: