B u ` @s$ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZmZmZddlmZmZmZmZmZddlmZmZmZddlmZmZdd l m!Z!dd l"m#Z#dd l$m%Z%dd l&m'Z'dd l(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6ea7da8da9e)e*e+e,e-e.e/e0e1d Z:e;dZe ddd+ddZ?e ddddZ@e ddd,ddZAe ddd-ddZBe ddd.ddZCe ddd/d d!ZDd0d"d#ZEdeE_Fe ddd1d%d&ZGd'd(ZHd)d*ZIdS)2N) iteritems) pyomo_api)TempfileManager) import_filesetup_redirectreset_redirect)yamlyaml_availableyaml_load_argspymplerpympler_available)ExtensionPointPlugin implements) ContainerOptions) ProblemFormat) SolverFactory)SolverManagerFactory) DataPortal)IPyomoScriptCreateModelIPyomoScriptCreateDataPortalIPyomoScriptPrintModelIPyomoScriptModifyInstanceIPyomoScriptPrintInstanceIPyomoScriptSaveInstanceIPyomoScriptPrintResultsIPyomoScriptSaveResultsIPyomoScriptPostprocessIPyomoScriptPreprocessModelTransformationFactorySuffixdisplayF) Zpyomo_create_modelZpyomo_create_dataportalZpyomo_print_modelZpyomo_modify_instanceZpyomo_print_instanceZpyomo_save_instanceZpyomo_print_resultsZpyomo_save_resultspyomo_postprocesszpyomo.scriptinggz pyomo.script) namespacecstjdd}|rTts,jjjdkr,tdjjjdkrTtrJdjj_n djj_tajjj dkst j ddt j jjjrtjjjdk rtjjjjsd }t|jjjjjjt_fd d }|t _dS) z+ Setup Pyomo execution environment postsolveNrzAConfiguration specifies a yaml file, but pyyaml is not installed!jsonquietz%[%8.2f] Setting up Pyomo environment gz0Directory for temporary files does not exist: %sc sptjjjdkr"djjj}nd}tr0d}nd}d|j||f}t|}|tkr|dd}|d|d kr|dd kr|d d }t ||t |d }d}t tjstrx0|t|krjjj||dkrP|d 7}qW|t|krd}td x`||d D]P} td| ddt| d d| d| dd k rtd| dqWtd d S)z This exception hook gets called when debugging is on. Otherwise, run_command in this module is called. rzmodel modelloadingrunningz+Unexpected exception (%s) while %s %s: z\n z"'Nz# Traceback (most recent call last):z File "z", line z, in z )lenoptionsr)filenamefilter_excepthook__name__strKeyErrorreplaceloggererror traceback extract_tb isEnabledForloggingDEBUGprintsysexit) etypevaluetbnameactionmsgZvalueStrZtb_listiitem)data8/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/scripting/util.pypyomo_excepthookcs:     ,z+setup_environment..pyomo_excepthook)getattrr2r r&results_format ValueErrortime start_timeruntimer>rAstdoutwriteflush disable_gcgcdisabletempdirospathexistsr excepthook)rKr&rHrNrL)rKrMsetup_environment<s*      1r`c st_jjjdks8tjdtt tj t jj j dkr\|d_Sjjdk rxjjD]}t|dd}qrWxttD]}|jjdqWx>jj j gjjjD] }tj|std|d qWdat}tjj j ddj_t|j_d atjj}gj_ xBt!D]:|kr6Gfd d d t"}|} jj #| q6Wd |krjj j$|krd} t%| jj j t&jjd jdS)z Execute preprocessing files Required: parser: Command line parser object Returned: error: This is true if an error has occurred. r(z-[%8.2f] Applying Pyomo preprocessing actions rTN) clear_cache)r2zFile z does not exist!Fcs2eZdZeeddfddZddZdS)z apply_preprocessing..TMPT)servicecstjj|_dS)N)rOlocal usermodelfn)self)rKkeyrLrM__init__sz)apply_preprocessing..TMP.__init__c[s |jf|S)N)re)rfkwdsrLrLrMapplysz&apply_preprocessing..TMP.applyN)r5 __module__ __qualname__rmodelapirhrjrL)rKrgrLrMTMPsrnZpyomo_preprocesszaPreprocessing function 'pyomo_preprocess' defined in file '%s', but model is already constructed!)'rrcr2rTr>rArUrVrRrSrWr1r)r3 print_helpr: preprocessrr rrjrKfilesrDr\r]r^IOErrorr4rdtime_initial_importdir_usermodel_pluginsrmrappend object_name SystemExitrO) rKparser config_valuerpepfiletickZ usermodel_dirrntmprHrL)rKrgrMapply_preprocessingsD         rc CsN |jjjdks0tjdtttj|jjj dkrht rht j t j }||j_td|i}t}xFt|jjjD]4\}}t|trt||kr|||<|t|qW|jjj}t|dkrt|d}|dkr|}n$||krjd}t|||jjj fnTt|dkrj|dkrFd}t||jjj n$||krjd }t|||jjj ft!t"}|dkr0t|dkrd }t||jjj nt|dkrd }t||jjj nf|jjj#} t} |$j%t&|jt&| d } |jjj'd krtdt| d|j_(t} nv||krTd}t|||jjj f||} | dkrd}t|||jjj fn$t|dkrd}t||jjj x"t!t)D]}|j%|j| dqWt!t*}t|dkrd}t|t|dkr|$j%|j| d} nt+} | j,rT| } |jjj'd kr|jj(dk rtd|jj(nt} t|jj-j.dkrxN|jj-j.D]@}|/dd}|dkrd}t|t0|| j1|| dq|W| j2| |jj-j3|jjj |jjj'd} nt|jj-j.dkr|jj-j.d/dd4}|dkrT| j2|jj-j.d|jj-j3|jjj |jjj'd} q|dkr*t5|jj-j.dd d}dt6|krt|dkrd}t||j7dkrd}t|t0|jj-j.d|j7} n,t|dkrd}t|t0|jj-j.d| 8| | j2| |jj-j3|jjj |jjj'd} nl|dks>|d krt9j1t:|jj-j.dft;} | j2| |jj-j3|jjj |jjj'd} nt|ft} qWxF|jj?D]:}t@|&}|A| } | dkrtd$|WdQRXqdW|jjj'd krt|}td%|tBCtjDrtd&| Etd'x"t!tFD]}|j%|j| d(qWd}d}|jjjGdk r|jjj'd krDt}|jjjGd kr|jjHtIjJtIjKfkr|jj-j.ddd)d*}n"|jj-j.ddd)t0|jjH}|jjH}n|jjjG}|jjjL}i}|jjjMrd |d+<|jjjNdkr|jjjN|d,<| j|||d-\}}|jjjdksTtOjPQ|s@td.|d/ntd0t0|d1|jjj'd kr|t|}td2||jjj d3krt rtd'td4t jRSt j }t jRjT|d5d6td'x"t!tUD]}|j%|j| d(qW|jjj dk r:t r:t j t j }||jjk r.||j_td7|tV| | |||jd8S)9a Create instance of Pyomo model. Return: model: Model object. instance: Problem instance. symbol_map: Symbol map created when writing model to a file. filename: Filename that a model instance was written to. r(z[%8.2f] Creating model r.z6 Total memory = %d bytes prior to model constructionrNz'Model '%s' is not defined in file '%s'!z%Multiple models defined in file '%s'!z Unknown model '%s' in file '%s'!zPA model is not defined and the 'pyomo_create_model' is not provided in module %szFMultiple model construction plugins have been registered in module %s!)r2 model_optionsTz2 %6.2f seconds required to construct instancez"'%s' object is 'None' in module %szbModel construction function 'create_model' defined in file '%s', but model is already constructed!)r2r)z>Multiple model data construction plugins have been registered!.r-ZdatzWWhen specifiying multiple data files, they must all be *.dat files. File specified: %s)r3r)) namespacesprofile_memory report_timingpy)ra modeldatazbCannot apply 'pyomo_create_modeldata' and use the 'modeldata' object that is provided in the modelz)'modeldata' object is 'None' in module %szINeither 'modeldata' nor 'pyomo_create_dataportal' is defined in module %sZymlrzUnknown data file type: )r2r)instancez %6.2f seconds to apply %sz3Unexpected error while applying transformation '%s'z8 %6.2f seconds required for problem transformationszMODEL INSTANCE)r2rlpsymbolic_solver_labelsfile_determinism)r3format io_optionsz ERROR: file z has not been created!zModel written to file ''z* %6.2f seconds required to write filer/z. Summary of objects following file outputd)limitz< Total memory = %d bytes following Pyomo instance creation)r)rsmap_idr3rc)Wr2rTr>rArUrVrRrSrWrr r muppyget_size get_objectsrc max_memoryr@setrrd__dict__ isinstancer idaddr)rwr1listkeysrxr3r rrDrbrjrrrsrrrZ _constructedrKrqsplitr6loadZcreate_instancerlowerrrtrreadropenr rQrtypeZ transformr!Z create_usingr9r=r?pprintr save_fileZ model_formatrZcpxlpZlpxlpZ save_formatrrr\r]r^summaryZ summarizeprint_rr)rKmem_usedZ_modelsZ _model_IDS_name_objZ model_namerHr{rr}r)rrr|suffixZuserdataZmodify_start_timeZtransformationZxfrmZ total_timefnamerZwrite_start_timerrZpost_file_output_summaryrLrLrM create_modelsl                      "        rc Cs|jjjdks0tjdtttj|jj dj }|dkrNt dt |jj dj dkrxj|jj dj D]X}|ddkr|dd}t||d}|dkrt||ttjd qtt d ||jfqtWt|jj djd ddkrd|jj dj_d}d}|jj djdkrd }n:|jj djtkrHt d |jj djn|jj dj}i}|jj djdk r|jj dj|d<|jj djdk r|jj dj|d<t|f|}|dkrd} t | t|jj dji} |jjjs|jjjrd| d<|jjjrd| d<|jjjdkr4|jjj| d<|jjj| d<t|jj djd d| d <|jjj | d<|d krzi} |jj dj!| d<|jj dj"dk r|jj dj"| d<t#|f| } | dkrt dt|ddl$m%} x| D]}| &|| |qWt |jj djdkr0| '|jj dj|jj dj(dk rX| '|jj dj(|j)|fd| i| }WdQRXnt |jj djdkr|jj dj(dk rd*dd|jj dj+D}|d|jj dj(| d<n>t |jj djdkr|jj dj| d<n|jj dj(| d<|j)|fd|i| }WdQRX|jjj,dkrt-rt.j/0t.j/1}||j2j3kr||j2_3t4d |t5|||j2d!S)"z Perform optimization with a concrete instance Required: instance: Problem instance. Returned: results: Optimization results. opt: Optimizer object. r(z[%8.2f] Applying solver rNz1Problem constructing solver: no solver specified)"rr.r-) directionzZProblem declaring solver suffix %s. A component with that name already exists on model %s. timelimitserialzUnknown solver manager %shostportz(Problem constructing solver manager '%s'TZ keepfilesrrteerZ solver_io executablez Problem constructing solver `%s`)registered_callbackopt css&|]\}}|dk rd||fVqdS)Nz%s=%srL).0rgrDrLrLrM [sz"apply_optimizer..r2z1 Total memory = %d bytes following optimization)resultsrrc)6r2rTr>rArUrVrRrSrWZsolversZ solver_namerQr1suffixesrOsetattrr"ZIMPORTrFrmanagerrZ pyro_hostZ pyro_portr6 keep_filesr& print_logfiler)rrZ stream_outputrZ io_formatZsolver_executablerZpyomo.core.base.pluginrZ set_callbackZ set_optionsZoptions_stringZsolvejoinrrr r rrrrcrr@r)rKrZsolverZ suffix_namerrZsolver_mngr_nameZsolver_mngr_kwdsZ solver_mngrrHkeywordsZsf_kwdsrrrFZostringrrLrLrMapply_optimizers             ",   rc Cs.|jjjdks0tjdtttj|jj j rt dt dt dt |j t dt dt|j d }x|D]}tj|qWWdQRXt dt dt dy|j|Wntk rt d YnX|jj js|jj jr |jj j}n|jj jd kr d }nd }|j||jj jd |jjjdkst dt t|jt|jdkrt dt dt |jdjt dt |jdjt|jdjdkrt|jdjd}t dt |jdj|dt d||jj jr0t d|jd|jj jdt d|jj jrt dt dt dt dt|djdkrt dt|t dnt dx$tt D]}|j!|j||dqWx$tt"D]}|j!|j||dqW|jjj#dkr*t$r*t%j&'t%j&(} | |j)j*kr| |j)_*t d| dS)z Process optimization results. Required: instance: Problem instance. results: Optimization results object. opt: Optimizer object. r(z[%8.2f] Processing results rz:==========================================================zSolver Logfile: rNzSolver Logfile - ENDzProblem updating solver resultsrz results.ymlz results.json)r3rz Number of solutions: rz Solution Informationz Gap: z Status: r.z Function Value: Valuez Solver results file: )numrzSolution Summaryz No solutions reported by solver.)r2rrz7 Total memory = %d bytes following results processing)+r2rTr>rArUrVrRrSrWr&rr@r6Z _log_filerZ solutionsZstore_to ExceptionZ show_resultsZ save_resultsrPr1ZsolutionZgapstatusZ objectiverrrvariabler#r rrjrrr r rrrrcr) rKrrrINPUTlineZ results_filergr{rrLrLrMprocess_resultsssz            rcCs|jjjdks0tjdtttjx8|jj D],}t |dd}dt |kr:| |j||q:Wx"t tD]}|j|j||dqtW|jjjdkrtrtjtj}||jjkr||j_td|d S) z Apply post-processing steps. Required: instance: Problem instance. results: Optimization results object. r(z.[%8.2f] Applying Pyomo postprocessing actions T)rar$)r2rrr.z+ Total memory = %d bytes upon terminationN)r2rTr>rArUrVrRrSrW postprocessrrtr$r rrjrr r rrrrcrr@)rKrrrzrr{rrLrLrMapply_postprocessings     rcCstg|j_|jjjdkshtjdt t t dkr^|jjj dkr^tjd|jj tj|}|}|}|jjjrtdkryddl}daWndaYnXtr|jjd gd d d n,ddl}|t}td |td dS) a Perform final actions to finish the execution of the pyomo script. This function prints statistics related to the execution of the pyomo script. Additionally, this function will drop into the python interpreter if the `interactive` option is `True`. Required: model: A pyomo model object. Optional: instance: A problem instance derived from the model object. results: Optimization results object. r(z[%8.2f] Pyomo Finished Tr.zMaximum memory used = %d bytes NrFrz# # Dropping into Python interpreterz& # Leaving Interpreter, back to Pyomo )ZbannerZexit_msg)cleanuprcrur2rTr>rArUrVrRrSr rrrW interactiveIPython_availableIPythonZShellZ IPShellEmbedcodeZInteractiveConsolelocalsr@Zinteract)rKr)rrrrshellrLrLrMfinalizes6     rcCs|rRt}t|_d|j_tjdk rRgtd_gtd_tjdt_t|jdkrdt|_|jjdkrtd tj n|jjdkrtd tj n|jjdkrtd tj td tj nf|jjdkr td tj td tj n2|jjdkrconfigure_loggers fileLogger getLoggerhandlersclosersetLevelERRORWARNINGINFOr?logfile FileHandler addHandlerr)r2shutdownrLrLrMr(s@           runknowncCsvd}d}|dkryft|tjkr&|}n |j|d}t}x:t|D].} | ddkrBt|| } t| tj sB| || <qBWWnt k rt ||dSXt |dt |jj} | dkry6y ddl} Wntk rddl} YnXddl} Wn(tk rt ddtd YnXt jd d }| |jd |jt|}| |}|d d|| }|| | | |dd}|| || | | |d}|| || | | |}nlt y|||d}WnPt k rRt!"d}|jj#dks |jj$r4t ddt!%dt&d|t'|f|j(}Ynt)k r:t!"d}|jj#dks|jj$rt ddt j*|jj+ d|j,dk r|j,j-dk rd|j,j-}nd}t.rd}nd}d||f}t'|}t|t/kr$|dkr$t'|0dddd}t12||d}YnXt dd|jj3rXt45t j*|jj+ dt ||dS)as Execute a function that processes command-line arguments and then calls a command-line driver. This function provides a generic facility for executing a command function is rather generic. This function is segregated from the driver to enable profiling of the command-line execution. Required: command: The name of a function that will be executed to perform process the command-line options with a parser object. parser: The parser object that is used by the command-line function. Optional: options: If this is not None, then ignore the args option and use this to specify command options. args: Command-line arguments that are parsed. If this value is `None`, then the arguments in `sys.argv` are used to parse the command-line. name: Specifying the name of the command-line (for error messages). data: A container of labeled data. Returned: retval: Return values from the command-line execution. errorcode: 0 if Pyomo ran successfully Nr)args_)retval errorcode)r2T)rz]Cannot use the 'profile' option: the Python 'profile' or 'pstats' package cannot be imported!z.profile)rz(options=options,parser=parser)rRZ cumulativeZcalls)r2ryr.rzExiting %s: %s)removezmodel r)r*r+z&Unexpected exception while %s %s: Nonez\nr,r-)6rargparse Namespace parse_argsrrtrOrtypes MethodTyperxrrrpushrTZ profile_countZcProfile ImportErrorprofilepstatsrQZcreate_tempfileZrunctxr5 __globals__rZStatsZ strip_dirsZ sort_statsZ print_statsZ print_callersZ print_calleesrAexc_infor>Z catch_errorsrBr@r6rrpoprr)rr4r7r8r9r:rXrYenable)commandryrrFrKr2rr_optionsrgvalZpcountrrZtfiler~perrr)rGrHZerrStrrLrLrM run_commandVs                                  rcCs0x*tD]"}xtt|D] }|qWqWdS)N)rmr Z deactivate)rgr{rLrLrMrs rcCs|ds|drDts tdt|d}tj|ft}||S|dsX|drxt|d}t|}||St d|dS)Nz.ymlz.yamlzFERROR: yaml configuration file specified, but pyyaml is not installed!rz.jsnz.jsonz)ERROR: Unexpected configuration file '%s') endswithr rQrrrr rr'rr)r3rrrLrLrMget_config_valuess   r)N)N)NNN)NN)NNN)NF)NNNrNN)JrrYr>r\rAr;rrRr'sixrZ pyomo.commonrZpyomo.common.tempfilesrZ pyutilib.miscrrrZpyomo.common.dependenciesrr r r r Zpyomo.common.pluginr rrZpyomo.common.collectionsrrZ pyomo.optrZpyomo.opt.baserZpyomo.opt.parallelrZpyomo.dataportalrZ pyomo.corerrrrrrrrrrr r!r"r#Z memory_datarr4rmrr9rSr`rrrrrrrrrrrrLrLrLrM sp       @ [ D  U  A ,