B ÔÂ_š7ã@s¨dgZddlZddlZddlZddlZddlZddlmZm Z ddlm Z ddl m Z ddl mZddlZddlTddlTddlmZmZe d ¡ZGd d„deƒZdS) ÚSystemCallSolveréN)ÚApplicationErrorÚ WindowsError)ÚBunch)ÚTempfileManager)Úrun)Ú*)Ú SolverStatusÚ SolverResultsz pyomo.optc@s„eZdZdZdd„Zd!dd„Zd"d d „Zd d „Zd d„Zdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd „ZdS)#rz A generic command line solver cKsd| dd¡}| dd¡}tj|f|Žd|_d|_d|_d|_d|_d|_|dk r`|j ||ddS)z Constructor Ú executableNÚvalidateTFÚ)Únamer ) ÚpopÚ OptSolverÚ__init__Ú _keepfilesÚ _results_fileÚ_timerÚ_user_executableÚ_last_solve_timeÚ_define_signal_handlersÚset_executable)ÚselfÚkwargsr r ©rú=/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/opt/solver/shellcmd.pyr#s  zSystemCallSolver.__init__NTcCs||dkr0d|_|r,| ¡dkr,td|jƒ‚dS|s<||_nd}YnX|dkrb|r^d}t||jƒ‚dSdS)z! True if the solver is available TFNz#No executable found for solver '%s')Z_assert_availablerÚ availabler ÚNotImplementedErrorrr)rZexception_flagZansÚmsgrrrr$is   zSystemCallSolver.availablecCst‚dS)z; Create the command line that is executed. N)r%)rr Z problem_filesrrrÚcreate_command_linezsz$SystemCallSolver.create_command_linecCstƒS)zU Process the logfile for information about the optimization process. )r )rrrrÚprocess_logfile€sz SystemCallSolver.process_logfilecCs|S)zh Process auxilliary data files generated by the optimizer (e.g. solution files) r)rÚresultsrrrÚprocess_soln_file†sz"SystemCallSolver.process_soln_filecCs|jdk r|jS| ¡S)z= Returns the executable used by this solver. N)rr)rrrrr °szSystemCallSolver.executablecCst‚dS)zE Returns the default executable used by this solver. N)r%)rrrrr·sz$SystemCallSolver._default_executablecOsÌt ¡| dd¡|_| dd¡|_tj|f|ž|Žx*|jD] }tj   |¡s>d}t ||ƒ‚q>W|  |  ¡|j¡|_|jj|_|jdk r¤tj   |j¡r¤t |j¡|jdk rÈtj   |j¡rÈt |j¡dS)z# Peform presolves. Z keepfilesFZuse_signal_handlingNz.Solver failed to locate input problem file: %s)rÚpushrrrrÚ _presolveÚ_problem_filesÚosr#Úexistsrr'r Ú_commandZlog_fileÚ _log_fileÚremoveÚ _soln_file)rÚargsÚkwdsÚfilenamer&rrrr,½s"      zSystemCallSolver._presolvecCsÄtj d¡rtj d¡ ¡|_t tj¡r:t  d|j j ¡|j rŒ|j dk rXtd|j ƒ|jdk rptd|jƒ|jgk rŒtdt|jƒƒtj ¡| |j ¡\|_|_tj ¡t|j|jdS)NZtimerTz Running %szSolver log file: '%s'zSolver solution file: '%s'zSolver problem files: %s)ÚrcÚlog)rr r!r#rÚloggerÚ isEnabledForÚloggingÚDEBUGÚdebugr0Úcmdrr1Úprintr3r-ÚstrÚsysÚstdoutÚflushÚ_execute_commandÚ_rcÚ_logr)rrrrÚ _apply_solveràs       zSystemCallSolver._apply_solvercCsÚ|jdk rRt|jdƒ}| dt|jjƒd¡| d¡| |jd¡| ¡|jdk r‚tj   |j¡s‚d}t ||j|j fƒ‚d}|j dk rÆ|  |j¡}|jsÆ|jdk rÆtj   |j¡rÆt |j¡tj|j d|S)NÚwzSolver command line: Ú z*File '%s' not generated while executing %s)r2)r1ÚopenÚwriter@r0r>rFÚcloser.r#r/ÚIOErrorÚ_results_formatÚprocess_outputrErr3r2rr)rZOUTPUTr&r)rrrÚ _postsolveøs&        zSystemCallSolver._postsolvecCsºt ¡}yZd|kr|j}nd}t|j||jdkr6|jn|jtdd|jƒ|j|j|jd\}}Wn6t k r˜t   ¡d}d}t ||j|fƒ‚YnXt j  ¡t ¡||_||gS)z% Execute the command ÚscriptNég{®Gáz„?)ÚstdinZ timelimitÚenvÚteeZdefine_signal_handlersz3Could not execute the command: %s Error message: %s)ÚtimerQrr>Z _timelimitÚmaxrTÚ_teerrrAÚexc_inforrBrCr)rÚcommandÚ start_timeÚ_inputr7r8Úerrr&rrrrD"s&$  z!SystemCallSolver._execute_commandcCst ¡}|jdkrtdƒ‚| ¡}t ¡}|jdkrDtd||ƒ|jdkr|| |¡t ¡}|jdkrÞtd||ƒnbt|j ƒdkr¨|j|j ||  d¡|j d}n|j|j ||j d }t ¡}|jdkrÞtd ||ƒ|dkr||j _ |dkrtj|j _|jdkr|j|j _|S) z+ Process the output files. NzResults format is NoneTz- %6.2f seconds required to read logfile z3 %6.2f seconds required to read solution file rRr)ÚresÚsolnÚsuffixes)r^r`z2 %6.2f seconds required to read solution file)rVrNrr(Z_report_timingr?Z_results_readerr*ÚlenZsolutionrZ _suffixesZsolverZerror_rcr ÚerrorÚstatusr)rr7r[r)Zlog_file_completion_timeZsoln_file_completion_timeZresults_reader_completion_timerrrrO@s<             zSystemCallSolver.process_outputcCstjS)zW Returns the default results format for different problem formats. )Z ResultsFormatr_)rZ prob_formatrrrÚ_default_results_formatlsz(SystemCallSolver._default_results_format)NT)F)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr$r'r(r*r rr,rGrPrDrOrdrrrrr s 3 *#*,)Ú__all__r.rArVr;Z pyutilib.miscZpyutilibZpyutilib.commonrrrZpyutilib.servicesrZpyutilib.subprocessrZ pyomo.commonrZpyomo.opt.baseZpyomo.opt.base.solversZpyomo.opt.resultsr r Ú getLoggerr9rrrrrrÚ s