B u ``@sdZddlZddlZddlZddlZddlmZddlmZ ddl m Z ddl m Z mZddlmZddlmZmZdd lmZmZdd lmZdd lmZdd lmZmZmZmZm Z m!Z!m"Z"m#Z#dd l$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddl(m,Z,m-Z-ddl.m/Z/m0Z0ej12e3Z4ddZ5Gddde6Z7Gddde7Z8Gddde7Z9Gddde7Z:Gddde7Z;Gdd d e7Zd%d&Z?Gd'd(d(e6Z@dS))z EmbeddedSP,N) ComponentMap)current) ComponentUID)BlockSortComponents)Var) Objective_ObjectiveData) Constraint_ConstraintData) SOSConstraint) _ParamData)locate_annotationsStageCostAnnotationVariableStageAnnotationStochasticDataAnnotation$StochasticConstraintBoundsAnnotation"StochasticConstraintBodyAnnotationStochasticObjectiveAnnotation"StochasticVariableBoundsAnnotation)tree_structure)CreateAbstractScenarioTreeModel)InvocationType)ScenarioTreeInstanceFactory)ScenarioTreeManagerClientSerialScenarioTreeManagerClientPyro)xrangezipcCs4|j}|dk stx|D]\}}|||_qWdS)N)instanceAssertionErrorZfind_component_onvalue)ZworkerscenariodatarZcuidvalr$9/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/pysp/embeddedsp.py _update_data;s r&c@s eZdZdZddZddZdS) Distributionr$cOstdS)N)NotImplementedError)selfargskwdsr$r$r% expectationIszDistribution.expectationcOstdS)N)r()r)r*r+r$r$r%sampleKszDistribution.sampleN)__name__ __module__ __qualname__ __slots__r,r-r$r$r$r%r'Gsr'c@s.eZdZdZdZd ddZddZdd ZdS) TableDistributionz Table distribution. A probability weighted table of discrete values. If no weights are provided, the probability of each value is considered uniform. )valuesweightsNcCst|dkrtdt||_|dkrDdt|jgt|j|_n t||_t|jt|jkrjtdtt|jddkrtddS)NrzEmpty tables are not allowedg?z'Different number of weights than valuesgư>zWeights do not sum to 1)len ValueErrortupler3r4abssum)r)r3r4r$r$r%__init__Ws   zTableDistribution.__init__cCstddt|j|jDS)Ncss|]\}}||VqdS)Nr$).0r weightr$r$r% esz0TableDistribution.expectation..)r:rr3r4)r)r$r$r%r,dszTableDistribution.expectationcCsBtdd}d}x,t|j|jD]\}}||7}||kr Pq W|S)Nrr5g)randomuniformrr3r4)r)xZcummr r=r$r$r%r-hs zTableDistribution.sample)N)r.r/r0__doc__Z_TableDistribution__slotsr;r,r-r$r$r$r%r2Ns  r2c@s,eZdZdZdZddZddZddZd S) UniformDistributionzi Uniform distribution. A random number in the range [a, b) or [a, b] depending on rounding. )abcCs||ks t||_||_dS)N)rrDrE)r)rDrEr$r$r%r;xs zUniformDistribution.__init__cCs|j|jdS)Ng@)rErD)r)r$r$r%r,}szUniformDistribution.expectationcCst|j|jS)N)r?r@rDrE)r)r$r$r%r-szUniformDistribution.sampleN)r.r/r0rBr1r;r,r-r$r$r$r%rCqs rCc@s2eZdZdZdZejfddZddZddZ d S) NormalDistributiona Normal distribution. The parameters represent the mean (mu) and the standard deviation (sigma). The probability density function is: e^((x-mu)^2 / -2(sigma^2)) pdf(x) = ---------------------------- sqrt(2 * pi * sigma^2) )musigma_samplercCs||_||_||_dS)N)rGrHrI)r)rGrHsamplerr$r$r%r;szNormalDistribution.__init__cCs|jS)N)rG)r)r$r$r%r,szNormalDistribution.expectationcCs||j|jS)N)rIrGrH)r)r$r$r%r-szNormalDistribution.sampleN) r.r/r0rBr1r? normalvariater;r,r-r$r$r$r%rFs  rFc@s2eZdZdZdZejfddZddZddZ d S) LogNormalDistributiona Log normal distribution. A variable x has a log-normal distribution if log(x) is normally distributed. The parameters represent the mean (mu) and the standard deviation (sigma > 0) of the underlying normal distribution. The probability density function is: e^((ln(x)-mu)^2 / -2(sigma^2)) pdf(x) = ---------------------------- sigma * x * sqrt(2 * pi) )rGrHrIcCs"|dks t||_||_||_dS)Nr)rrGrHrI)r)rGrHrJr$r$r%r;s zLogNormalDistribution.__init__cCst|j|jddS)Ng@)mathexprGrH)r)r$r$r%r,sz!LogNormalDistribution.expectationcCs||j|jS)N)rIrGrH)r)r$r$r%r-szLogNormalDistribution.sampleN) r.r/r0rBr1r?lognormvariater;r,r-r$r$r$r%rLs rLc@s2eZdZdZdZejfddZddZddZ d S) GammaDistributiona Gamma distribution. Conditions on the parameters shape > 0 and scale > 0. The probability density function is: x^(shape-1) * e^(-x/scale) pdf(x) = ---------------------------- scale^shape * GammaFn(shape) )shapescalerIcCs.|dks t|dkst||_||_||_dS)Nr)rrRrSrI)r)rSrRrJr$r$r%r;s   zGammaDistribution.__init__cCs |j|jS)N)rRrS)r)r$r$r%r,szGammaDistribution.expectationcCs||j|jS)N)rIrRrS)r)r$r$r%r-szGammaDistribution.sampleN) r.r/r0rBr1r? gammavariater;r,r-r$r$r$r%rQs  rQc@s2eZdZdZdZejfddZddZddZ d S) BetaDistributionz Beta distribution. Conditions on the parameters alpha > 0 and beta > 0. The probability density function is: x^(alpha-1) * (1-x)^(beta-1) pdf(x) = ----------------------------- BetaFn(alpha, beta) )alphabetarIcCs.|dks t|dkst||_||_||_dS)Nr)rrVrWrI)r)rVrWrJr$r$r%r;s   zBetaDistribution.__init__cCs|jt|j|jS)N)rVfloatrW)r)r$r$r%r,szBetaDistribution.expectationcCs||j|jS)N)rIrVrW)r)r$r$r%r-szBetaDistribution.sampleN) r.r/r0rBr1r? betavariater;r,r-r$r$r$r%rUs  rUc Cst|tdd}t|dkr*tdtjnt|dks:t|dd}t|}t|dkrftdt| dddd}t | d ddd}|d krx2| D]&\}\}}|d krtd |j |fqWi}g|d<g|d <x|j td d tjd D]x}||d\} }| dkr6| d kr6td|j | f| dkrT|d||fq| d ksbt|d ||fqWt} x6| D]*\}} x| D]\}}||f| |<qWqW|| |fS)Nr5) max_allowedrz8Reference model is missing variable stage annotation: %sz3At least one variable stage assignment is required.cSs|dS)Nrr$)rAr$r$r%z&_map_variable_stages..)keycSs|dS)Nrr$)rAr$r$r%r[r\rMz~Embedded stochastic programs must be two-stage (for now), but variable with name '%s' has been annotated with stage number: %sT)active descend_intosort)rMFzhInvalid stage annotation for variable with name '%s'. Stage assignment must be 1 or 2. Current value: %s)rrr6r7r.rrexpand_entriesminr3maxitemsnamecomponent_data_objectsrrZalphabetizeComponentAndIndexgetappend) modelZvariable_stage_annotationZvariable_stage_assignmentsZmin_stagenumberZmax_stagenumbervarstagenumderivedstage_to_variables_mapZ stagenumbervariable_to_stage_mapZ stagevarsr$r$r%_map_variable_stagess`      rocCst|tdd}t|dkr*tdtjnt|dks:t|dd}t|}t|dkrftdx0|D](}t|t s~t| rltd|j qlW|S)Nr5)rZrz9Reference model is missing stochastic data annotation: %sz/At least one stochastic data entry is required.zStochastic data entry with name '%s' is not mutable. All stochastic data parameters must be initialized with the mutable keyword set to True.) rrr6r7r.rrra isinstancer Z is_constantre)riZstochastic_data_annotationstochastic_data paramdatar$r$r%_extract_stochastic_data,s*      rsc@seZdZeddZeddZddZddZed d Z ed d Z ed dZ dddZ ddZ ddZdddZdddZdS) EmbeddedSPcCs(i}xt|D]}||t|<qW|S)N)EXPRZidentify_mutable_parametersid)rOansparamr$r$r%_collect_mutable_parametersIsz&EmbeddedSP._collect_mutable_parameterscCs(i}xt|D]}||t|<qW|S)N)ruZidentify_variablesrv)rOrwrjr$r$r%_collect_variablesPszEmbeddedSP._collect_variablescCsd|_d|_d|_i|_i|_d|_t|_t|_t|_ t|_ t|_ t|_ t|_ t|_t|_t|_t|_t|_t|_t|tstd||_t|j|_t|j\|_|_|_tt|j|_|jddksttj|jtt j!d|_g}x*|jD] }|"j#tk r |$|q Wx|D]}|j|=q4Wt%|jt&dd}t'|dkrtt(dt&j)nt'|dkst|dd}t|*}d}d}x4|+D](\} } | dkr| }n| dkr| }qW|dkrt(d|dkrt(d ||k st||_,||_-t.} x|D]6}||jkr|j 7|g$| |j | $|qWt'|j | dkr|j | =n | 8| t|9| j56}g|j| <x2|D]*}|j7|g$| |j| $|qWt'|j| dkr<|j| =q6}t|4|j?6}t'|r|8|t'|st'|st'|r*|j8|t@t'|pt'|t@t'|p"t'|d i}x0tAB|||D]}||jkr>||tC|<q>Wt'|dkrg|j|<x6|6D]*}|j 7|g$||j|$|qWt|9|j>6}g|j |<x2|D]*}|j 7|g$||j |$|qWqWx|jj0tDd d d D]|}xt|ED]h\}}t|4|6}||jkrht(d|jF|jF|jFf|j 7|g$||j 7|g$|q*WqWtG}xT|jj0td dD]>}t|4|jH6}t|4|jI6}t'|dkst'|dkr(|j8|t@t'|dkt@t'|dkd g|j|<x>|D]6}||jkr8|j7|g$||j|$|q8Wt'|j|dkr|j|=g|j|<x>|D]6}||jkr|j7|g$||j|$|qWt'|j|dkr|j|=qWt't%|jt:dkrt(dt't%|jt;dkr6t(dt't%|jt.dkrTt(d| jJr|tK|jdrnttL|jd| |jJrtK|jdrttL|jd||jJrtK|jdrttL|jd||jJrtK|jdrttL|jd|dS)Nz+reference model input must be a Pyomo modelrr5)ctypeZ repr_version)rZz4Reference model is missing stage cost annotation: %srMz/Missing stage cost annotation for time stage: 1z/Missing stage cost annotation for time stage: 2T)r^r_z>Reference model can not contain more than one active objective)lbubzSOSConstraints with stochastic data are currently not supported in embedded stochastic programs. The SOSConstraint component '%s' has a weight term for variable '%s' that references stochastic parameter '%s')r_zSReference model can not contain a StochasticConstraintBoundsAnnotation declaration.zQReference model can not contain a StochasticConstraintBodyAnnotation declaration.zLReference model can not contain a StochasticObjectiveAnnotation declaration.z/.pyspembeddedsp_stochastic_objective_annotationz5.pyspembeddedsp_stochastic_constraint_body_annotationz7.pyspembeddedsp_stochastic_constraint_bounds_annotationz5.pyspembeddedsp_stochastic_variable_bounds_annotation)Mreference_modelZ objective time_stagesrmrnrqrZvariable_to_objectives_mapobjective_to_variables_mapZvariable_to_constraints_mapconstraint_to_variables_mapZ!stochastic_data_to_objectives_map objective_to_stochastic_data_mapZ"stochastic_data_to_constraints_map!constraint_to_stochastic_data_mapZ#stochastic_data_to_variables_lb_map"variable_to_stochastic_data_lb_mapZ#stochastic_data_to_variables_ub_map"variable_to_stochastic_data_ub_mapZvariable_symbolsrpr TypeErrorrsro_variable_stage_assignmentsr8sortedrrZgenerate_cuid_string_maprrZCUID_repr_versionZparent_componentr{rhrrr6r7r.rard _stage1_cost _stage2_costr enumeraterfrZsenseZobjective_senseryexprr3 setdefaultZdeclarerzrrr lowerbodyupperbool itertoolschainrvr Z get_itemsrerr|r}Zhas_declarationshasattrsetattr)r)r~Zkeys_to_deleterjr]Zstage_cost_annotationZstage_cost_assignmentsZ stage1_costZ stage2_costcdatarkZsto_objZobjcntrobjZ obj_paramsrrZ obj_variablesZ sto_conboundsZ sto_conbodyconZ lower_paramsZ body_paramsZ upper_paramsZall_stochastic_paramsrxZbody_variablesZsosconr=Z weight_paramsZ sto_varboundsr$r$r%r;Ws                                                      zEmbeddedSP.__init__cCsz|dks tt}ddg}dg}g}x:td|dD](}|dt||dt|q4W|jd|||did }d|jd<d |jd<d t|}xjtd|dD]X}dt|}dt|} d|j|<|j d ||j | ||j|<||j | <qW|j j|jd<|jj|jd<xT|jD]F\} \} } d t| } | s^|j|  | jn|j|  | jq,W|S) Nrt1t2rootr5ns)ZStagesZNodesZ Scenarios)r"g?t)rrrrhstrZcreate_instanceZ NodeStageZConditionalProbabilityrXZChildrenaddclearZScenarioLeafNoderreZ StageCostrrrdZStageVariablesZStageDerivedVariables)r)sizeZstmZ_stages_nodesZ _scenariosir=Z node_nameZ scen_namerjrkrlZ stage_namer$r$r%_create_scenario_tree_models@         z&EmbeddedSP._create_scenario_tree_modelcCst|jdkS)z>Returns whether the SP has a stochastic data in the objective.r)r6r)r)r$r$r%has_stochastic_objectivesz#EmbeddedSP.has_stochastic_objectivecCst|jdkS)zJReturns whether the SP has stochastic data in the body of any constraints.r)r6r)r)r$r$r%has_stochastic_constraintssz%EmbeddedSP.has_stochastic_constraintscCst|jdkpt|jdkS)zJReturns whether the SP has stochastic data in the bounds of any variables.r)r6rr)r)r$r$r%has_stochastic_variable_boundssz)EmbeddedSP.has_stochastic_variable_boundsFc Cst|j}t|j}t|tr8||jkr,|}|j|}n6t|tr\||jkrP|}|j |}nt | | }||krx@|D]4}|j |\}}|r|r|}n t||}||kr|Pq|Wd}|S)a\ Determine the time stage that an object belongs in. Object types recognized are variables, constraints, expressions, and objectives. For variables, the time stage is determined by the user annotations on the reference model. For objectives, constraints, and expressions, the time stage is determined by the existance of the variables and stochastic data that inside any expressions. The time stage is computed as the maximum time stage of any variables or stochastic data that are encountered. Fixed variables are treated as data belonging to the same time stage. Args: obj: The object to classify. derived_last_stage (bool): Indicates whether derived variables within a time stage should be treated as if they belong to the final time stage (where non-anticipativity is not enforced). When the value is True, derived variables will be treated like variables in final time stage. The default is False, meaning that the derived status of variables will not be considered in the computation. Returns: The implied time stage for the object. The first time stage starts at 1. r5)rbrrcrpr rrr rrr8rzr3rn) r)rZderived_last_stageZstageZ laststageZvars_rjZvarstagerlr$r$r%compute_time_stages*"          zEmbeddedSP.compute_time_stagecKs|dks t|j}||}t|j|d}t}x|D]}||||<q.model_callback)rir)r)rrrrrrr~r) r)rr+rrrrr]rr$)r)r%generate_sample_sp5s     zEmbeddedSP.generate_sample_spcCs4x |jD]\}}||_q W|r0|jSdS)N)rqrdr-r r~r)r)rrxrr$r$r%r-HszEmbeddedSP.samplecCs4x |jD]\}}||_q W|r0|jSdS)N)rqrdr,r r~r)r)rrxrr$r$r%set_expected_valueNszEmbeddedSP.set_expected_valueN)F)F)F)r.r/r0 staticmethodryrzr;rpropertyrrrrrrr-rr$r$r$r%rtGs  @&    @( rt)A__all__osrr?rNZpyomo.common.collectionsrZpyomo.core.exprrruZpyomo.core.baserZpyomo.core.base.blockrrZpyomo.core.base.varrZpyomo.core.base.objectiverr Zpyomo.core.base.constraintr r Zpyomo.core.base.sosr Zpyomo.core.base.paramr Zpyomo.pysp.annotationsrrrrrrrrZpyomo.pysp.scenariotreerZ,pyomo.pysp.scenariotree.tree_structure_modelrZpyomo.pysp.scenariotree.managerrZ(pyomo.pysp.scenariotree.instance_factoryrrrZ six.movesrrpathabspath__file__rr&objectr'r2rCrFrLrQrUrorsrtr$r$r$r% s@      (      #@