B _Y@szddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z m Z ddlmZddlmZmZmZmZmZmZmZmZddlmZmZddlmZmZmZm Z ddl!m"Z"dd l#m$Z$m%Z%dd l&m'Z'dd l(m)Z)m*Z*m+Z+dd l,m-Z-dd l,m.Z/ej01e2Z3dZ4dddZ5ddZ6Gddde'eZ7dddZ8ddZ9dddZ:e%;de7e|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_resultr rlenappendZ findRootNodehasattrZ mpi_comm_treeZrankstrstrip ValueErrorgetcwdchdirZSpawnsizearrayZReducer!INTZSUMROOTZbcastZ Disconnectpyutilib subprocessrunrrr,endswithZ_smapsolverpyomo_solve_timetermination_conditionsolutionclear)"workerworking_directorysubproblem_typelogfileproblem_list_filename executableoutput_solver_log io_optionscommand_line_optionsoptions_filenamer-ZfiledataZ write_timeZ load_functionbundlestartstopscenarior#Z root_nodekeyvalZcurrdirspawnrcmsg solve_timeworker_resultsreader object_nameZ nl_filenameZ symbol_mapZ sol_filenameresultsrcA/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/solvers/schuripopt.pyEXTERNAL_invoke_solve@s                              rec 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) r2Zget_noder stageinstancer1Z_ScenarioTreeSymbolMapbySymbolZ _variable_idsZis_expression_typevaluestaler) rKZ node_namerItmpnoderXrfrgrhid_Z variable_idvarrcrcrdEXTERNAL_collect_solutions     rocsPeZdZfddZddZeddZeddZdd d Zdd dZ Z S)SchurIpoptSolvercs&tt|||d|_dS)N schuripopt)superrp__init__register_optionsset_options_to_default _executable)self) __class__rcrdrss  zSchurIpoptSolver.__init__cCs||_d|j_dS)NT)rt_optionsZ_implicit_declaration)rwrcrcrdrus z'SchurIpoptSolver.set_options_to_defaultcCs|jS)N)ry)rwrcrcrdoptionsszSchurIpoptSolver.optionscCsdS)Nrqrc)rwrcrcrdr5szSchurIpoptSolver.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)r rzschuripopt.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 workersre)invocation_type function_args)r.r/r0makedirsjoinr2rr1openrzitemsr: startswithr;writer9r7printZinvoke_functionthisfilerSinglerP isinstancerrioreadrupdate)rwmanagerZoutput_directoryrNr{rQverboserRrOr2rMrTrSfrYrZr_rb worker_nameZ worker_resultrarcrcrd_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.logrNz'Schuripopt solver working directory: %szSchuripopt solver logfile: %s)rNrQrrRgrr r'okZoptimalrro)rr)1Z_create_tempdirr.r<r/rZ _add_tempfile_filesrrsetZ solve_typer2r3r1r6 objectiverrr5rZ probabilityadd solver_statussolver_messagerHsolution_statusr rboundpopstatusrFmessagemaxr^valuesr4rGZ pyomo_timexhatr9ZstagesZnodesZget_worker_for_scenarioZinvoke_function_on_workerrrrrepr)rwsprQrrNkwdsrOrLrrrrrHrrUrXrbrrfrlrZ node_solutionZ node_xhatrmrcrcrd _solve_implZs               &zSchurIpoptSolver._solve_impl)FFFN)FFN) __name__ __module__ __qualname__rsrupropertyrzr5rr __classcell__rcrc)rxrdrps    Rrpc 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_solutionrQkeep_solver_filessymbolic_solver_labelssolver_optionsrczSolver 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 visibilityactionr7)Zap_kwdsZap_group) r r rrtrpr rr_schuripopt_group_label)rzrcrcrdrunschuripopt_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")rQrrz&Final solution (scenario tree format):Nz!Total execution time=%.2f seconds)r4rZ initializerrprr6typetupler1splitr:rzrZsolverQrrrrr2ZsnapshotSolutionFromScenariosZpprintSolution) rz start_timerrqrZname_valr5rZrYrbrrcrcrd runschuripopt s@       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.environrr SystemExitcoderrrrr)r#ZpyomorzZ_excrcrcrdmain=s rrq__main__)N)N)N)>rr.sysr4r?Zpyutilib.subprocessrBZ pyomo.corerZ pyomo.optrrrrZ!pyomo.pysp.util.configured_objectrZpyomo.pysp.util.configrr r r r r rrZpyomo.pysp.util.miscrrZpyomo.pysp.scenariotree.managerrrrrZ&pyomo.pysp.scenariotree.manager_solverrZpyomo.pysp.solvers.spsolverrrZ'pyomo.pysp.solvers.spsolvershellcommandrZpyomo.pysp.convert.schuripoptrrrZ six.movesrrpickler/abspath__file__rrrerorprrrZregister_solverrexitrcrcrcrds<  (       u *0 "