B W`}@sddlZddlmZddlZddlZddlZddlmZmZddlm Z ddl Z yddl m Z Wne k r~edYnXddlZejejdGdd d eZdS) N) ClientError)datetime timedelta)uuid4) OptimizerzReinstall package using pip install faropt[async] to use this functionality. If you have already done that, try uninstalling and reinstalling scipy)levelc@sHeZdZdZdddZddZddd Zdd d Zdd d ZddZ dS)AsyncOptaAsyncOpt class for typical black-box optimizattion problems where the function evaluation is slow. Take for example an engine simulation - each simulation may take an hour, and you don't want to make the optimizer wait for an update. This class implements a serverless ask- and tell-optimizer interface to ask an optimizer for the next point to evaluate the function at, based on expected improvement (Bayesian opt), and users can tell the optimizer the results of an optimization to update the response surface faroptc Cs:td}y|j|d}|ddddkrtdd|_||_|ddd }x|D]}|d d kr|d |_td |j|d dkr|d |_td|j|d dkr|d |_ td|j |d dkrX|d |_ td|j qXWWn4t k r.}zd|_t |Wdd}~XYnXd|_ dS)z_Constructor method: Gets buckets and tables associated with the already launched stack Zcloudformation)Z StackNameZStacksrZ StackStatus)ZCREATE_COMPLETEZUPDATE_COMPLETEzFarOpt backend is ready!TZOutputsZ OutputKeyZ s3asyncoutputZ OutputValuezAsync Bucket: Zs3outputzBucket: recipetablezRecipe Table: jobtablez Job table: FN)boto3clientZdescribe_stackslogginginfoready stackname asyncbucketZbucketr r Exceptionerror modelname)selfrcfresponseoutputsoutputer=/tmp/pip-unpacked-wheel-9of43mn_/faropt/templates/blackbox.py__init__s2            zAsyncOpt.__init__c CsNtdd}t||WdQRXtd}|d|j|d}tddS)a3Updates the model by pickling and uploading (overwriting) the opt model to a special async bucket generated by the stack :param modelname: UUID of the model :type modelname: string :param opt: object containing params of the optimizer :type opt: scikit-opt z/tmp/model.pklwbNs3z /model.pklzUpdated model!) openpickledumpr rZ upload_filerrr)rroptf s3_clientrrrr update_model=s   zAsyncOpt.update_modelr cCstt|_tdd|Drtdt|t|ddddd}||j|td |j|d krt d }|j |j |jd d d|dgid}|jSt ddS)adUpdates the model by pickling and uploading (overwriting) the opt model to a special async bucket generated by the stack :param bounds: UUID of the model :type bounds: list of tuples, like [(-1.0,1.0),(-5,5),(-10.5,10.5)...] :param modeltag: Friendly name of the optimizer :type modeltag: string, optional css|]}t|tVqdS)N) isinstancetuple).0itemrrr Wsz(AsyncOpt.create_model..zCreating model with bounds: ZGPZEIZsamplinglhs)Zacq_funcZ acq_optimizerZinitial_point_generatorzcreated model with name r r!z /model.pklTagSettag)KeyValue)Bucketr1ZTaggingz9Input bounds as a list of tuples, like [(-2.0, 2.0), ...]N) strrrallrrrr(r rZput_object_taggingrr)rZboundsZmodeltagr%r'rrrr create_modelMs   zAsyncOpt.create_modelc Csvtd}|dkr$|jdkr$td|dkr2||_||j|jdd}tdd}t|}WdQRX||_ | S)zasks the model for a new point to evaluate at, based on expected improvement :param modelname: UUID of the model :type modelname: string, opional r!r z;Please create_model() or pass in a modelname to ask_model()z /model.pklz/tmp/model.pklrbN) r rr ValueError download_filerr"r#loadmodelask)rrr'rr& opt_restoredrrr ask_modelqs  zAsyncOpt.ask_modelc Cstd}|dkr$|jdkr$td|dkr2||_||j|jdd}tdd}t|}WdQRX||_ | ||}| |j|j |S)atells the model what the fuction value was for a new point (xval). The model's response surface is then updated based on this information :param xval: Point that was evaluated, like [0,0], that is within the bounds set in create_model :type xval: list :param fval: function value measured at xval :type fval: float :param modelname: UUID of the model :type modelname: string, opional r!r z;Please create_model() or pass in a modelname to ask_model()z /model.pklz/tmp/model.pklr7N) r rrr8r9rr"r#r:r;tellr() rZxvalZfvalrr'rr&r=resrrr tell_models   zAsyncOpt.tell_modelcCstd}|j|jddd}x`|dD]R}|d}|j|j|d}|dgkr`td d |q(td |dd d d |q(WdS)zLPrints a list of all models created so faar with UUID and modeltags r!z /model.pkld)r3Z DelimiterZMaxKeysZCommonPrefixesZPrefix)r3r1r/zmodel z :rr2:N)r rZ list_objectsrgetZget_object_taggingprint)rrresultoZ tmpprefixrrrr list_modelss   zAsyncOpt.list_modelsN)r )r )r )r ) __name__ __module__ __qualname____doc__rr(r6r>rArHrrrrrs ' $  r)r Zbotocore.exceptionsrroszipfilerruuidrtimeZskoptr ImportErrorrr# basicConfigINFOobjectrrrrrs