B u `RX@sRddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z ddl mZddlmZmZmZmZmZddlmZmZddlmZmZmZmZddlmZdd lm Z m!Z!dd l"m#Z#dd l$m%Z%m&Z&m'Z'ej()e*Z+d Z,dd dZ-ddZ.Gddde#eZ/dddZ0ddZ1dddZ2e!3de/e4dkrNe5e2dS)N) ComponentUID) ReaderFactory ResultsFormat UndefinedData)PySPConfiguredObject)PySPConfigValuePySPConfigBlocksafe_register_common_optionsafe_register_unique_option_domain_tuple_of_str_or_dict)parse_command_linelaunch_command)InvocationTypeScenarioTreeManagerFactoryScenarioTreeManagerClientPyroScenarioTreeSolveResults)#ScenarioTreeManagerSolverClientPyro)SPSolverResultsSPSolverFactory)SPSolverShellCommand)_write_bundle_nl_write_scenario_nl_write_problem_list_filezSchurIpoptSolver Optionsc " Cstj|st| dkrdg} i} i} d} |dkr|js@t|j} x|jjD]6}t}t ||||| |j <t}||| |j <qPWnV|dkst|j } xB|jj D]6}t}t ||||| |j <t}||| |j <qW| dk stt| dkstg}|||d|d|d| |j}t|drt|d |j|j jdkrt|d t|x|D]\}}|}|d krtd nf|d krtdnR|dkrtdn>|dkrtdn*|dkrtdn||dt|qzWt}t|drt}zt||j|j j|||j|j jd}d}|j|j jdkrtddg}|jd||jjg|jj|jj d|j|j j!|dd}|"t|dkst|d}Wdt|Xnt#j$j%|g||||d\}}|dkstt|t}||}i}t&t'j(}x| D]}t}| |\}}|)dsht|ddd} || | d}!t}||!_*||!j+_|||| ||!_,t|!j+j-dkrt|!j.dkr|!j./| ||!||<q@WWdQRX|S) Nz.*bundles scenariosrz-AMPLzuse_problem_file=yeszoption_file_name=MPIzmpi_spawn_mode=yesz output_file=Zuse_problem_filez@Use of the 'use_problem_file' command-line option is disallowed.Zmpi_spawn_modez>Use of the 'mpi_spawn_mode' command-line option is disallowed.Zoption_file_namez@Use of the 'option_file_name' command-line option is disallowed.Z output_filez;Use of the 'output_file' command-line option is disallowed.z5Use of the '-AMPL' command-line option is disallowed.=)argsZmaxprocsi)ZsendbufZrecvbufoproot)r )cwdoutfileteez.nlsol)suffixesZ infeasible)0ospathexistsAssertionError scenario_treecontains_bundlesZ_process_bundle_solve_resultrtimernameZ_process_scenario_solve_resultrrlenappendZ findRootNodehasattrZ mpi_comm_treeZrankstrstrip ValueErrorgetcwdchdirZSpawnsizearrayZReducerINTZSUMROOTZbcastZ Disconnectpyutilib subprocessrunrrr&endswithZ_smapsolverpyomo_solve_timetermination_conditionsolutionclear)"workerworking_directorysubproblem_typelogfileproblem_list_filename executableoutput_solver_log io_optionscommand_line_optionsoptions_filenamer'ZfiledataZ write_timeZ load_functionbundlestartstopscenariorZ root_nodekeyvalZcurrdirspawnrcmsg solve_timeworker_resultsreader object_nameZ nl_filenameZ symbol_mapZ sol_filenameresultsr]A/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/pysp/solvers/schuripopt.pyEXTERNAL_invoke_solve9s                              r_c Csi}i}|j|}|jd}|j}|j}|dk s6t|jj}xH|jD]>} x8|jD].} || } | rhqR| j | j f|t | |d<qRWqFW|S)Nr)Z cuid_buffer) r,Zget_noderstageinstancer+Z_ScenarioTreeSymbolMapbySymbolZ _variable_idsZis_expression_typevaluestaler) rEZ node_namerCtmpnoderRr`rarbid_Z variable_idvarr]r]r^EXTERNAL_collect_solutions     ricsPeZdZfddZddZeddZeddZdd d Zdd dZ Z S)SchurIpoptSolvercs&tt|||d|_dS)N schuripopt)superrj__init__register_optionsset_options_to_default _executable)self) __class__r]r^rms  zSchurIpoptSolver.__init__cCs||_d|j_dS)NT)rn_optionsZ_implicit_declaration)rqr]r]r^ros z'SchurIpoptSolver.set_options_to_defaultcCs|jS)N)rs)rqr]r]r^optionsszSchurIpoptSolver.optionscCsdS)Nrkr])rqr]r]r^r/szSchurIpoptSolver.nameFNcCstj|st|tj|d}|j} t| ||d} | dksDttj|d} tj| rbtg} t| dr} xj|j D]\\}}| }| dr|dkrt d| |d dd t|d q~| ||fq~WWdQRX|rtd |td | td| tdz,|jdttj|| |||j||| | f d}Wdt|ttfr|rtj|rt|} t| WdQRXXt| }x4|D],}||}x|D]}|||qWqW|S)NzPySP_Subproblems.txt)ignore_bundles)rrzschuripopt.optwZOF_ZOF_output_filezOUse of the 'output_file' option is disallowed. Use the logfile keyword instead.  z'Schuripopt solver problem list file: %sz"Schuripopt solver options file: %sz"Schuripopt solver problem type: %sz,Sending solver invocation request to workersr_)invocation_type function_args)r(r)r*makedirsjoinr,rr+openrtitemsr4 startswithr5writer3r1printZinvoke_functionthisfilerSinglerJ isinstancerrioreadrupdate)rqmanagerZoutput_directoryrHrurKverboserLrIr,rGrNrMfrSrTrYr\ worker_nameZ worker_resultr[r]r]r^_launch_solverst     $    zSchurIpoptSolver._launch_solverc Ksd}|jdtd}|dkr:tj|d}|d|n ||jd<|r`td|td||j||||||d }d } t } t } t } t } |j d krz|j st t|jt|j jkst x|j jD]}| dk r t|j|jtrd} n| |j|j|j7} | |j|j| |j|j| |j|jt|j|jtrb| dq| |j|jqWn|j d kst t|jt|j jkst x|j jD]}| dk rt|j|jtrd} n| |j|j|j7} | |j|j| |j|j| |j|jt|j|jtrD| dn| |j|jqWt| d ksnt t| d kst t| d kst t| d kst t}d|_d|_| |_| |j_| |j_| |j_ t!|j"#|j_$t!|j%#|j_&d|_'t(|jjdkrt(|jjdkr| |_i}|_'x|j j)ddD]v}xn|j*D]d}|+|jdj}|j,|dt-t.j/|jfd}i}||j<x"|D]}||d|t0|<qWq`WqTW|S)a Solve a stochastic program with the SchurIpopt solver. See the 'solve' method on the base class for additional keyword documentation. Args: sp: The stochastic program to solve. output_solver_log (bool): Stream the solver output during the solve. logfile: The name of the logfile to save the solver output into. verbose: Report verbose status information to aid debugging. **kwds: Passed to the DDSIP file writer as I/O options (e.g., symbolic_solver_labels=True). Returns: A results object with information about the solution. zPySP_Subproblems.txtZworkdir)dirNzschuripopt.logrHz'Schuripopt solver working directory: %szSchuripopt solver logfile: %s)rHrKrrLgrrr!okZoptimalrri)rzr{)1Z_create_tempdirr(r6r)r}Z _add_tempfile_filesrrsetZ solve_typer,r-r+r0 objectiverrr/rZ probabilityadd solver_statussolver_messagerBsolution_statusrrboundpopstatusr@messagemaxrXvaluesr.rAZ pyomo_timexhatr3ZstagesZnodesZget_worker_for_scenarioZinvoke_function_on_workerrrrrepr)rqsprKrrHkwdsrIrFrrrrrBrrOrRr\rr`rfrZ node_solutionZ node_xhatrgr]r]r^ _solve_implSs               &zSchurIpoptSolver._solve_impl)FFFN)FFN) __name__ __module__ __qualname__rmropropertyrtr/rr __classcell__r]r])rrr^rjs    Rrjc Cs|dkrt}t|dt|dt|dt|dt|dt|dt|dt|dt|t|t|d td td dd d dditd|S)Nr disable_gcprofile tracebackoutput_scenario_tree_solutionrKkeep_solver_filessymbolic_solver_labelssolver_optionsr]zSolver options to pass to SchurIpopt (e.g., relax_integrality=yes). This option can be used multiple times from the command line to specify more than one SchurIpopt option.r)domain descriptiondocZ visibilityactionr1)Zap_kwdsZap_group) rr rrnrjr rr _schuripopt_group_label)rtr]r]r^runschuripopt_register_optionss@  rc Cs(t}t|}|tdtdt}|j}t|dkrt|tkrxZ|D]0}d|ksdt | d\}}| |j | <qTWn x| D]\}}||j |<qW|j||j|j|jd} | j} | `tdt| |jrtd|j|jWdQRXtdtdt|dS) zO Construct a senario tree manager and solve it with the SD solver. z=Running SchurIpopt solver for stochastic programming problemsrr)rKrrz&Final solution (scenario tree format):Nz!Total execution time=%.2f seconds)r.rZ initializerrjrr0typetupler+splitr4rtrZsolverKrrrrr,ZsnapshotSolutionFromScenariosZpprintSolution) rt start_timerrkrZname_valr/rTrSr\rr]r]r^ runschuripopts@       rc Cs\ddl}yt|tddd}Wn$tk r@}z|jSd}~XYnXtt|d|j|j|j dS)Nrrz:Optimize a stochastic program using the SchurIpopt solver.)progrzrunschuripopt: )Z error_labelrZ profile_countr) Z pyomo.environr r SystemExitcoder rrrr)rZpyomortZ_excr]r]r^main6s rrk__main__)N)N)N)6rr(sysr.r9Zpyutilib.subprocessr<Z pyomo.corerZ pyomo.optrrrZ!pyomo.pysp.util.configured_objectrZpyomo.pysp.util.configrrr r r Zpyomo.pysp.util.miscr r Zpyomo.pysp.scenariotree.managerrrrrZ&pyomo.pysp.scenariotree.manager_solverrZpyomo.pysp.solvers.spsolverrrZ'pyomo.pysp.solvers.spsolvershellcommandrZpyomo.pysp.convert.schuripoptrrrr)abspath__file__rrr_rirjrrrZregister_solverrexitr]r]r]r^s8       u *0 "