B _ @sdddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z ddl mZddlmZy ddlZWnek rddlZYnXyddlZdZWnek rdZYnXddlmZeaddlZddlmZdd lmZdd lmZmZm Z m!Z!m"Z"dd l#m$Z$m%Z%m&Z&dd l'm(Z(dd l)m*Z*ddl+m,Z,ddl-m.Z.ddl/Tddl0m1Z1ddl0Z2da3da4e5e6e7e8e9e:e;ee?dZ@daAeddddZBeddd.ddZCeddddZDeddd/ddZEeddd0dd ZFeddd1d!d"ZGeddd2d#d$ZHd3d%d&ZIdeI_Jeddd4d(d)ZKd*d+ZLd,d-ZMdS)5N) itervaluesiterkeys iteritems)xrange) pyomo_apiTF)Options) Container)TempfileManager)yamlyaml_availableyaml_load_argspymplerpympler_available)ExtensionPointPlugin implements) ProblemFormat) SolverFactory)SolverManagerFactory) DataPortal)*) TextLabeler) 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 postsolveNr zAConfiguration 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 )lenoptionsrfilenamefilter_excepthook__name__strKeyErrorreplaceloggererror traceback extract_tb isEnabledForloggingDEBUGprintsysexit) etypevaluetbnameactionmsgZvalueStrZtb_listiitem)data8/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/scripting/util.pypyomo_excepthookqs:     ,z+setup_environment..pyomo_excepthook)getattrr&r rresults_format ValueErrortime start_timeruntimer2r5stdoutwriteflush disable_gcgcdisabletempdirospathexistsr excepthook)r?rr<rBr@)r?rAsetup_environmentJs*      1rTc stj_jjjdksjjjgjjjD] }tj|std|d qWdat }tjjjjjddj_t |j_ d at!jj}gj_"xBt#D]:|krBGfd d d t$}|} jj"%| qBWd |krƈjjj&|krd} t'| jjjt(jjd jdS)z Execute preprocessing files Required: parser: Command line parser object Returned: error: This is true if an error has occurred. rz-[%8.2f] Applying Pyomo preprocessing actions rTN) clear_cache)r&zFile z does not exist!Fcs2eZdZeeddfddZddZdS)z apply_preprocessing..TMPT)servicecstjj|_dS)N)rClocal usermodelfn)self)r?keyr@rA__init__sz)apply_preprocessing..TMP.__init__c[s |jf|S)N)rY)rZkwdsr@r@rAapplysz&apply_preprocessing..TMP.applyN)r) __module__ __qualname__rmodelapir\r^r@)r?r[r@rATMPsrbZpyomo_preprocesszaPreprocessing function 'pyomo_preprocess' defined in file '%s', but model is already constructed!))pyutilibmiscrrWr&rHr2r5rIrJrFrGrKr%rr' print_helpr. preprocess import_filerZIPyomoScriptPreprocessr^r?filesr8rPrQrRIOErrorr(rXtime_initial_importdir_usermodel_pluginsrarappend object_name SystemExitrC) r?parser config_valuerfepfiletickZ usermodel_dirrbtmpr<r@)r?r[rAapply_preprocessingsD         rvc Cs^ |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"}|dkr8t|dkrd }t||jjj nt|dkrd }t||jjj nn|jjj#} t} |$j%t&j'j(|jt&j'j(| d } |jjj)d krtdt| d|j_*t} nv||kr\d}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.r\| } |jjj)d kr|jj*dk rtd|jj*nt} t|jj/j0dkrxN|jj/j0D]@}|1dd}|dkrd}t|t2|| j3|| dqW| j4| |jj/j5|jjj |jjj)d} nt|jj/j0dkr|jj/j0d1dd6}|dkr\| j4|jj/j0d|jj/j5|jjj |jjj)d} q|dkr6t&j'j7|jj/j0dd d}dt8|krt|dkrd}t||j9dkrd}t|t2|jj/j0d|j9} n,t|dkrd}t|t2|jj/j0d| :| | j4| |jj/j5|jjj |jjj)d} nl|dksJ|d krt;j3t<|jj/j0dft=} | j4| |jj/j5|jjj |jjj)d} nt>d!|jj/j0dn$| j4| |jj/j5|jjj |jjj)d} |jjj)d krtdt| t}xpt!t?D]d}|jjj)d krt} |j%|j| | d"|jjj)d krtd#t| t@|ft} qWxF|jjAD]:}tB|&}|C| } | dkrtd$|WdQRXqpW|jjj)d krt|}td%|tDEtjFrtd&| Gtd'x"t!tHD]}|j%|j| d(qWd}d}|jjjIdk r|jjj)d krPt}|jjjId kr|jjJtKjLtKjMfkr|jj/j0ddd)d*}n"|jj/j0ddd)t2|jjJ}|jjJ}n|jjjI}|jjjN}i}|jjjOrd |d+<|jjjPdkr|jjjP|d,<| j|||d-\}}|jjjdks`tQjRS|sLtd.|d/ntd0t2|d1|jjj)d krt|}td2||jjj d3krt rtd'td4t jTUt j }t jTjV|d5d6td'x"t!tWD]}|j%|j| d(qW|jjj dk rFt rFt j t j }||jjk r:||j_td7|t&j'jX| | |||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. rz[%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!)r& 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!)r&rz>Multiple model data construction plugins have been registered!.r!ZdatzWWhen specifiying multiple data files, they must all be *.dat files. File specified: %s)r'r) namespacesprofile_memory report_timingpy)rU 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: )r&rinstancez %6.2f seconds to apply %sz3Unexpected error while applying transformation '%s'z8 %6.2f seconds required for problem transformationszMODEL INSTANCE)r&r~lpsymbolic_solver_labelsfile_determinism)r'format 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)rr~smap_idr'rW)Yr&rHr2r5rIrJrFrGrKrzrr muppyget_size get_objectsrW max_memoryr4setrrX__dict__ isinstanceZModelidaddrrnr%listkeysror'rIPyomoScriptCreateModelr8rVr^rcrdrr{rjIPyomoScriptPrintModelIPyomoScriptCreateDataPortalrZ _constructedr?rhsplitr*loadZcreate_instancerylowerrgrkr}readr openr rEIPyomoScriptModifyInstancetypeZ transformZTransformationFactoryZ create_usingr-r1r3pprintIPyomoScriptPrintInstance save_fileZ model_formatrZcpxlpZlpxlpZ save_formatrrrPrQrRsummaryZ summarizeprint_IPyomoScriptSaveInstancer)r?mem_usedZ_modelsZ _model_IDS_name_objZ model_namer<rrrwrtrr}r~rssuffixZuserdataZmodify_start_timeZtransformationZxfrmZ total_timefnamerZwrite_start_timerrZpost_file_output_summaryr@r@rA 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 |t5j6j7|||j2d!S)"z Perform optimization with a concrete instance Required: instance: Problem instance. Returned: results: Optimization results. opt: Optimizer object. rz[%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 keepfilesrrteer{Z solver_io executablez Problem constructing solver `%s`)registered_callbackopt css&|]\}}|dk rd||fVqdS)Nz%s=%sr@).0r[r8r@r@rA isz"apply_optimizer..r&z1 Total memory = %d bytes following optimization)resultsrrW)8r&rHr2r5rIrJrFrGrKZsolversZ solver_namerEr%suffixesrCsetattrZSuffixZIMPORTr:rmanagerrZ pyro_hostZ pyro_portr* keep_filesr print_logfilerrrZ stream_outputr{Z io_formatZsolver_executablerZpyomo.core.base.pluginrZ set_callbackZ set_optionsZoptions_stringZsolvejoinrrzrr rrrrWrr4rcrdr)r?r~ZsolverZ suffix_namerrZsolver_mngr_nameZsolver_mngr_kwdsZ solver_mngrr<keywordsZsf_kwdsrrr:Zostringrr@r@rAapply_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. rz[%8.2f] Processing results rz:==========================================================zSolver Logfile: rNzSolver Logfile - ENDzProblem updating solver resultsr z results.ymlz results.json)r'rz Number of solutions: rz Solution Informationz Gap: z Status: r"z Function Value: Valuez Solver results file: )numrzSolution Summaryz No solutions reported by solver.)r&r~rz7 Total memory = %d bytes following results processing)+r&rHr2r5rIrJrFrGrKrrr4r*Z _log_filerZ solutionsZstore_to ExceptionZ show_resultsZ save_resultsrDr%ZsolutionZgapstatusZ objectiverrrvariableZdisplayrIPyomoScriptPrintResultsr^IPyomoScriptSaveResultsrzrr rrrrWr) r?r~rrINPUTlineZ results_filer[rrrr@r@rAprocess_resultssz            rcCs|jjjdks0tjdtttjx<|jj D]0}t j j |dd}dt |kr:||j||q:Wx"ttD]}|j|j||dqxW|jjjdkrtrtjtj}||jjkr||j_td|d S) z Apply post-processing steps. Required: instance: Problem instance. results: Optimization results object. rz.[%8.2f] Applying Pyomo postprocessing actions T)rUr)r&r~rr"z+ Total memory = %d bytes upon terminationN)r&rHr2r5rIrJrFrGrK postprocessrcrdrgrkrrIPyomoScriptPostprocessr^rzrr rrrrWrr4)r?r~rrqrrrrr@r@rAapply_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. rz[%8.2f] Pyomo Finished Tr"zMaximum memory used = %d bytes NrFrz# # Dropping into Python interpreterz& # Leaving Interpreter, back to Pyomo )ZbannerZexit_msg)cleanuprWrlr&rHr2r5rIrJrFrGrrzrrK interactiveIPython_availableIPythonZShellZ IPShellEmbedcodeZInteractiveConsolelocalsr4Zinteract)r?rr~rrrshellr@r@rAfinalizes6     rcCs|rVt}t|_d|j_tjdk rVgtd_gtd_tjdt_tj |jdkrht|_|jjdkrtd tj n|jjdkrtd tj n|jjdkrtd tjtd tjnf|jjdkrtd tjtd tjn2|jjdkr@td tjtd tj|jjrt|jjdt_gtd_gtd_tdtjtdtjtj |jjdS) Nrpyomorcwarninginfoverbosedebugw)rrHr2configure_loggers fileLogger getLoggerhandlersclosercrdZreset_redirectsetLevelERRORWARNINGINFOr3logfile FileHandler addHandlerZsetup_redirect)r&shutdownr@r@rAr6s@            runknowncCs4d}d}|dkryjt|tjkr&|}n |j|d}tj}x:t|D].} | ddkrFt|| } t | t j sF| || <qFWWnt k rt ||dSXt|dt|jj} | dkrtsd} tdd t| tjd d } t|jd |jt| }t| }|d d|| }| | |!| |dd}|| | | |!| |d}|| | | |!| |}nlty|||d}WnPt k rt"#d}|jj$dks|jj%rtdd t"&dt'd|t(|f|j)}Ynt*k rt"#d}|jj$dksD|jj%rbtdd tj+|jj, d|j-dk r|j-j.dk rd|j-j.}nd}t/rd}nd}d||f} t(|}t|t0kr|dkrt(|1dddd}t23| |d}YnXtdd |jj4rt56tj+|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)r&zQCannot use the 'profile' option. The Python 'pstats' package cannot be imported!T)rz.profile)rz(options=options,parser=parser)rFZ cumulativeZcalls)r&rpr"rzExiting %s: %s)removezmodel rrrz&Unexpected exception while %s %s: Nonez\nr r!)7rargparse Namespace parse_argsrcrdrrkrCrtypes MethodTyperorrr pushrHZ profile_countpstats_availablerEZcreate_tempfileprofileZrunctxr) __globals__rpstatsZStatsZ strip_dirsZ sort_statsZ print_statsZ print_callersZ print_calleesr5exc_infor2Z catch_errorsr6r4r*rrpoprrrr(r+r,r-r.rLrMenable)commandrprr:r?r&rr_optionsr[valZpcountr<Ztfileruperrrr;ZerrStrr@r@rA run_commandds                                rcCs0x*tD]"}xtt|D] }|qWqWdS)N)rarZ deactivate)r[rrr@r@rArs 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 rErr rr rrri)r'rrr@r@rAget_config_valuess   r)N)N)NNN)NN)NNN)NF)NNNrNN)NrrMr2rPr5r/rrFrsixrrrZ six.movesrZ pyomo.commonrZcProfiler ImportErrorrrZ pyutilib.miscrZ memory_datarcrZpyutilib.servicesr Zpyomo.common.dependenciesr r r r rZpyomo.common.pluginrrrZ pyomo.optrZpyomo.opt.baserZpyomo.opt.parallelrZpyomo.dataportalrZ pyomo.coreZpyomo.core.baserrrr(rrrrrrrrrrarr-rGrTrvrrrrrrrrrrr@r@r@rA s             [ D  U  A ,