B @`1 @s`ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZddlmZddlmZmZmZddlmZmZmZmZmZdd lmZmZm Z dd l!m"Z"m#Z#dd l$m%Z%dd l&m'Z'dd l(m)Z)ddl*m+Z+ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:e#a;dae?dZ@daAe ddddZBe ddd4ddZCe ddddZDe ddd5ddZEe ddd6ddZFe ddd7d d!ZGe ddd8d"d#ZHed$d%d&d9d'd(ZIdeI_JGd)d*d*eKZLe ddd:d,d-ZMd.d/ZNd0d1ZOd2d3ZPdS);N) iteritems) pyomo_api) deprecated) is_debug_set)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}|r,ts,jjjdkr,tdtajjj dks\t j ddt j jjjrntjjjdk rtjjjjsd}t|jjjjjjt_fd d }|t _dS) z+ Setup Pyomo execution environment postsolveNr zAConfiguration specifies a yaml file, but pyyaml is not installed!quietz%[%8.2f] Setting up Pyomo environment gz0Directory for temporary files does not exist: %sc sltjjjdkr"djjj}nd}tr0d}nd}d|j||f}t|}|tkr|dd}|d|d kr|dd kr|d d }t ||t |d }d}t t strx0|t|krjjj||dkrP|d 7}qW|t|krd}td x`||d D]P} td| ddt| d d| d| dd k r td| dq Wtd 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_tbrprintsysexit) etypevaluetbnameactionmsgZvalueStrZtb_listiitem)data8/tmp/pip-unpacked-wheel-bi3529v6/pyomo/scripting/util.pypyomo_excepthook`s:      ,z+setup_environment..pyomo_excepthook)getattrr3r r(results_format ValueErrortime start_timeruntimeloggingr?stdoutwriteflush disable_gcgcdisabletempdirospathexistsr excepthook)rIr(rFrLrJ)rIrKsetup_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)r3zFile z does not exist!Fcs2eZdZeeddfddZddZdS)z apply_preprocessing..TMPT)servicecstjj|_dS)N)rMlocal usermodelfn)self)rIkeyrJrK__init__sz)apply_preprocessing..TMP.__init__c[s |jf|S)N)rd)rekwdsrJrJrKapplysz&apply_preprocessing..TMP.applyN)r6 __module__ __qualname__rmodelapirgrirJ)rIrfrJrKTMPsrmZpyomo_preprocesszaPreprocessing function 'pyomo_preprocess' defined in file '%s', but model is already constructed!)'rrbr3rRrSr?rTrUrPrQrVr2r*r4 print_helpr; preprocessrrr!rirIfilesrBr[r\r]IOErrorr5rctime_initial_importdir_usermodel_pluginsrlrappend object_name SystemExitrM) rIparser config_valueroepfiletickZ usermodel_dirrmtmprFrJ)rIrfrKapply_preprocessingsD         r~c CsJ |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%|tBtCrtd&| Dtd'x"t!tED]}|j%|j| d(qWd}d}|jjjFdk r|jjj'd kr@t}|jjjFd kr|jjGtHjItHjJfkr|jj-j.ddd)d*}n"|jj-j.ddd)t0|jjG}|jjG}n|jjjF}|jjjK}i}|jjjLrd |d+<|jjjMdkr|jjjM|d,<| j|||d-\}}|jjjdksPtNjOP|sMultiple model data construction plugins have been registered!.r.ZdatzWWhen specifiying multiple data files, they must all be *.dat files. File specified: %s)r4r*) namespacesprofile_memory report_timingpy)r` 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 %sZymlr zUnknown data file type: )r3r*instancez %6.2f seconds to apply %sz3Unexpected error while applying transformation '%s'z8 %6.2f seconds required for problem transformationszMODEL INSTANCE)r3rlpsymbolic_solver_labelsfile_determinism)r4format io_optionsz ERROR: file z has not been created!zModel written to file ''z* %6.2f seconds required to write filer0z. Summary of objects following file outputd)limitz< Total memory = %d bytes following Pyomo instance creation)r*rsmap_idr4rb)Vr3rRrSr?rTrUrPrQrVrrr muppyget_size get_objectsrb max_memoryr>setrrc__dict__ isinstancer"idaddr*rvr2listkeysrwr4rrrBrarirrrrrrrZ _constructedrIrpsplitr7loadZcreate_instancerlowerrrsrreadr openr rOrtypeZ transformr#Z create_usingrr:pprintr save_fileZ model_formatrZcpxlpZlpxlpZ save_formatrrr[r\r]summaryZ summarizeprint_rr)rImem_usedZ_modelsZ _model_IDS_name_objZ model_namerFrzrr|r*rrr{suffixZuserdataZmodify_start_timeZtransformationZxfrmZ total_timefnamerZwrite_start_timerrZpost_file_output_summaryrJrJrK 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=%srJ).0rfrBrJrJrK Xsz"apply_optimizer..r3z1 Total memory = %d bytes following optimization)resultsrrb)6r3rRrSr?rTrUrPrQrVZsolversZ solver_namerOr2suffixesrMsetattrr$ZIMPORTrDrmanagerrZ pyro_hostZ pyro_portr7 keep_filesr( print_logfiler*rrZ stream_outputrZ io_formatZsolver_executablerZpyomo.core.base.pluginrZ set_callbackZ set_optionsZoptions_stringZsolvejoinrrrr rrrrbrr>r)rIrZsolverZ suffix_namerrZsolver_mngr_nameZsolver_mngr_kwdsZ solver_mngrrFkeywordsZsf_kwdsrrrDZostringrrJrJrKapply_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 resultsr z results.ymlz results.json)r4rz Number of solutions: rz Solution Informationz Gap: z Status: r/z Function Value: Valuez Solver results file: )numrzSolution Summaryz No solutions reported by solver.)r3rrz7 Total memory = %d bytes following results processing)+r3rRrSr?rTrUrPrQrVr(rr>r7Z _log_filerZ solutionsZstore_to ExceptionZ show_resultsZ save_resultsrNr2ZsolutionZgapstatusZ objectiverrrvariabler%rrrirrrr rrrrbr) rIrrrINPUTlineZ results_filerfrzrrJrJrKprocess_resultspsz            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)r`r&)r3rrr/z+ Total memory = %d bytes upon terminationN)r3rRrSr?rTrUrPrQrV postprocessrrsr&rr rirrr rrrrbrr>)rIrrryrrzrrJrJrKapply_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)cleanuprbrtr3rRrSr?rTrUrPrQrrrrV interactiveIPython_availableIPythonZShellZ IPShellEmbedcodeZInteractiveConsolelocalsr>Zinteract)rIr*rrrrshellrJrJrKfinalizes6     rzuconfigure_loggers is deprecated. The Pyomo command uses the PyomoCommandLogContext to update the logger configurationz5.7.3)versioncCsBt|}|r.d|jj_tj|_|dddn||jt_dS)Nr))PyomoCommandLogContextr3rRrSconfigure_loggers fileLogger__exit__ __enter__)r3shutdowncontextrJrJrKr%s rc@s(eZdZdZddZddZddZdS) rz>Context manager to setup/restore logging for the Pyomo commandcCs6|dkrt}|jdkr t|_||_d|_d|_dS)N)rrRr3roriginal)rer3rJrJrKrg9s zPyomoCommandLogContext.__init__cCsHtd}td}|j|j|j|jf|_|jjjdkrF|tjn|jjjdkrb|tj n|jjjdkr|tj |tj nj|jjjdkr|tj |tj nB|jjjdkr|tj |tj n| tj kr|tj |jjjrD|jjj}t|d|_g|_g|_||j||jt||S) Npyomopyutilibr)warninginfoverbosedebugw)rS getLoggerlevelhandlersrr3rRsetLevelERRORWARNINGINFODEBUGgetEffectiveLevelNOTSETlogfile FileHandlerr addHandlerr)re_pyomo _pyutilibZ_logfilerJrJrKrBs8          z PyomoCommandLogContext.__enter__cCsjtd}||jd|jd|_td}||jd|jd|_|jdk rf|jtdS)Nrrr/rr0r1)rSrrrrrcloser )reetZevrCrrrJrJrKrhs      zPyomoCommandLogContext.__exit__N)r6rjrk__doc__rgrrrJrJrJrKr6s &runknownc Cs|dkryft|tjkr|}n |j|d}t}x:t|D].}|ddkr:t||}t|tj s:|||<q:WWnt k rt dddSXt z.t|t||||||\} } WdQRXWd|jjrtt j|jj dXt | | 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 N)argsr_)retval errorcode)remove)rargparse Namespace parse_argsrrsrMrtypes MethodTyperwrrpushr_run_command_implrRrWrXenablepopr) commandrxrrDrIr3_optionsrfvalrrrJrJrK run_commandws,      "r cCsld}d}|jj}|dkr&y6y ddl} Wntk rDddl} YnXddl} Wntk rltdYnXtjdd} | |j d|j t | } | | } | dd| |} | || || dd} | || || || d} | || || || }n>y|||d }Wn*tk rtd }|jjd ksn|jjrxtdtd |t|f|j}Yntk rbtd }|jjd ks|jjr̂|jdk r|jjdk rd|jj}nd}trd}nd}d||f}t|}t |t!krL|dkrLt|"ddd d}t#$||d }YnX||fS)Nrz]Cannot use the 'profile' option: the Python 'profile' or 'pstats' package cannot be imported!z.profile)rz(options=options,parser=parser)rPZ cumulativeZcalls)r3rxr/TrzExiting %s: %szmodel r*r+r,z&Unexpected exception while %s %s: Nonez\nr-r.)%rRZ profile_countZcProfile ImportErrorprofilepstatsrOrZcreate_tempfileZrunctxr6 __globals__rZStatsZ strip_dirsZ sort_statsZ print_statsZ print_callersZ print_calleesrwr?exc_inforSZ catch_errorsr@r>r7rrr*rr5rr8r9r:r;)rrxrrDrIr3rrZpcountr rZtfiler}perrr*rErFZerrStrrJrJrKrsp                       rcCs0x*tD]"}xtt|D] }|qWqWdS)N)rlrZ deactivate)rfrzrJrJrKrs 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 rOrr rr rjsonrq)r4rr rJrJrKget_config_valuess   r)N)N)NNN)NN)NNN)NF)NNNrNN)QrrXrSr[r?r<rrPrsixrZ pyomo.commonrZpyomo.common.deprecationrZpyomo.common.logrZpyomo.common.tempfilesrZ pyutilib.miscrrr Zpyomo.common.dependenciesr r r r rZpyomo.common.pluginrrrZpyomo.common.collectionsrrZ pyomo.optrZpyomo.opt.baserZpyomo.opt.parallelrZpyomo.dataportalrZ pyomo.corerrrrrrrrr r!r"r#r$r%Z memory_datarr5rlrr:rQr_r~rrrrrrrobjectrr rrrrJrJrJrK sz         @ V D  U  A A A\