B u `|(@sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z e dZ Gdd d eZGd d d eZGd d d eZddZie_ddZee_dS))SPSolverResultsSPSolverSPSolverFactoryN) UndefinedData) ComponentUID) EmbeddedSP)StringIOz pyomo.pyspc@s*eZdZddZejfddZddZdS)SPSolverStatuscCsd|_d|_d|_dS)N)namestatustermination_condition)selfr?/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/pysp/solvers/spsolver.py__init__)szSPSolverStatus.__init__cCst|}tt|}d}xHt|D]<\}}|||dkrJ|d|d|t||fq&Wx$|D]}|d|t||fqlWdS)N)r r r r z%s: %sz %s: %s)varssortedlistkeys enumerateremovewritegetattr)r ostreamattrsnamesfirstZcntr rrrpprint.s   zSPSolverStatus.pprintcCst}|j|d|S)N)r)rrgetvalue)r tmprrr__str__<s zSPSolverStatus.__str__N)__name__ __module__ __qualname__rsysstdoutrr!rrrrr (sr c@s*eZdZddZejfddZddZdS)rcCs*d|_d|_d|_d|_d|_t|_dS)NF) objectiveboundr xhat_loadedxhatr solver)r rrrrCs zSPSolverResults.__init__cCst|}tt|}d}x.|D]&}|||d|t||fq"W|dx$|D]}|d|t||fq\Wt}|jj |d|dx"| D]}|d|qWdS)N)r r'r(r)z%s: %s r+)rzsolver:z ) rrrrrrrrr+rr splitlines)r rrrrr r linerrrrKs      zSPSolverResults.pprintcCst}|j|d|S)N)r)rrr)r r rrrr!]s zSPSolverResults.__str__N)r"r#r$rr%r&rr!rrrrrAsrc@s@eZdZeddZeddZddZeddZd d Zd S) rcCstdS)zEReset all options on the solver object to their default valueN)NotImplementedError)r rrrset_options_to_defaultdszSPSolver.set_options_to_defaultcCstdS)zAccess the solver optionsN)r.)r rrroptionsjszSPSolver.optionscOstdS)N)r.)r argskwdsrrr _solve_imploszSPSolver._solve_implcCstdS)zThe registered solver nameN)r.)r rrrr rsz SPSolver.namec OsJt}|dd}|dd}|j}|dk r~|x&|D]}|jdd|j|<q@Wx|D]\} } | |j| <qhWx|jD]} d|j| _ qWzDt |t rd} t |j } d} d}x|j ddD]8}x2|j|D]$\}}|s| d 7} |r|d 7}qWqWn|j}t |j} t |j} d} d}x^|jddD]L}xD|jD]:}| t |j7} x$|jD]}||sh|d 7}qhWqNWqBW|d drtd td td dtd td| |rtdt |jtd| td| || |f|j|f||}t}|||j_|j|j_|jdksZt |jtrd|jj|_d|_ |dk r|j!dk r|j!}xN|D]F}||}x6|D].}t"|#|}|$s|||_d|_%qWqWd|_ |`!Wd|jj&dd|dk rD|j}|x8|D],}||}|j|j d|j|<qWX|S)a Solve a stochastic program. Args: sp: The stochastic program to solve. reference_model: A pyomo model with at least the set of non-anticipative variable objects that were declared on th e scenario tree of the stochastic program. If this keyword is changed from its default value of None, the non leaf-stage variable values in the solution will be stored into the variable objects on the reference model. Otherwise, the results object that is returned will contain a solution dictionary called 'xhat' that stores the solution nested by tree node name. options: Ephemeral solver options that will temporarily overwrite any matching options currently set for the solver. output_solver_log (bool): Stream the solver output during the solve. *args: Passed to the derived solver class (see the _solve_impl method). **kwds: Passed to the derived solver class (see the _solve_impl method). Returns: A results object with information about the solution. reference_modelNr0F)Z accessValuez rZoutput_solver_logrz--------------------zProblem Statisticsz0Total number of scenarios.................: %10sz0Total number of scenario bundles..........: %10sz0Total number of time stages...............: %10szTotal number of non-anticipative variables: %10s continuous: %10s discrete: %10sT)error)'timepopr0r/Z user_valuesvaluer itemsgetZ _userAccessed isinstancerlenZ time_stagesZstage_to_variables_mapZ is_continuous scenario_treeZ scenariosZstagesZnodesZ_standard_variable_idsZis_variable_discreteprintcenterZcontains_bundlesZbundlesr3r+Z pysp_timer rr r)r*rZfind_componentZis_expression_typestaleZ check_usage)r spr1r2startr4Z tmp_optionsZ orig_optionsoptkeyvalZ num_scenariosZ num_stagesZnum_na_variablesZnum_na_continuous_variablesZstagevarZderivedr@Z tree_nodeZid_resultsstopr*Z tree_obj_nameZtree_obj_solutionZcurrent_optionscurrentrrrsolvews                       zSPSolver.solveN) r"r#r$propertyr/r0r3r rMrrrrrbs    rcOs.|tjkrtj|}|||Std|dS)Nz4No SPSolver object has been registered with name: %s)r_registered_solvers ValueError)Z solver_namer1r2type_rrrrs    rcCs(t|tstd|tf|tj|<dS)NzMCan not register SP solver type '%s' because it is not derived from type '%s') issubclassr TypeErrorrrO)r rQrrr_register_solvers  rT)__all__r%r9loggingZ pyomo.optrZ pyomo.corerZpyomo.pysp.embeddedsprsixr getLoggerloggerobjectr rrrrOrTZregister_solverrrrr s      !