B _ނ@sdZddlZddlZddlZddlZddlmZddlmZ ddl Z ddl m Z ddlmZmZddlmZmZmZddlmZmZdd lmZmZdd lmZmZdd lmZdd l m Z dd l!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1ddl.m2Z2m3Z3ddl4m5Z5m6Z6ej78e9Z:ddZ;Gddde<Z=Gddde=Z>Gddde=Z?Gddde=Z@Gddde=ZAGd d!d!e=ZBGd"d#d#e=ZCd$d%ZDd&d'ZEGd(d)d)e<ZFdS)*z EmbeddedSP,N) ComponentMap)current) ComponentUID)is_fixed is_constant)BlockSortComponentsgenerate_cuid_names)Var_VarData) Objective_ObjectiveData) Constraint_ConstraintData) SOSConstraint) _ParamData)locate_annotationsStageCostAnnotationVariableStageAnnotationStochasticDataAnnotation$StochasticConstraintBoundsAnnotation"StochasticConstraintBodyAnnotationStochasticObjectiveAnnotation"StochasticVariableBoundsAnnotation) ScenarioTree)CreateAbstractScenarioTreeModel)InvocationType)ScenarioTreeInstanceFactory)ScenarioTreeManagerClientSerialScenarioTreeManagerClientPyro)xrangezipcCs4|j}|dk stx|D]\}}|||_qWdS)N)instanceAssertionErrorZfind_componentvalue)Zworkerscenariodatar"Zcuidvalr(9/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/pysp/embeddedsp.py _update_data>s r*c@s eZdZdZddZddZdS) Distributionr(cOstdS)N)NotImplementedError)selfargskwdsr(r(r) expectationLszDistribution.expectationcOstdS)N)r,)r-r.r/r(r(r)sampleNszDistribution.sampleN)__name__ __module__ __qualname__ __slots__r0r1r(r(r(r)r+Jsr+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 ValueErrortupler7r8abssum)r-r7r8r(r(r)__init__Zs   zTableDistribution.__init__cCstddt|j|jDS)Ncss|]\}}||VqdS)Nr().0r$weightr(r(r) hsz0TableDistribution.expectation..)r>r!r7r8)r-r(r(r)r0gszTableDistribution.expectationcCsBtdd}d}x,t|j|jD]\}}||7}||kr Pq W|S)Nrr9g)randomuniformr!r7r8)r-xZcummr$rAr(r(r)r1ks zTableDistribution.sample)N)r2r3r4__doc__Z_TableDistribution__slotsr?r0r1r(r(r(r)r6Qs  r6c@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)r#rHrI)r-rHrIr(r(r)r?{s zUniformDistribution.__init__cCs|j|jdS)Ng@)rIrH)r-r(r(r)r0szUniformDistribution.expectationcCst|j|jS)N)rCrDrHrI)r-r(r(r)r1szUniformDistribution.sampleN)r2r3r4rFr5r?r0r1r(r(r(r)rGts rGc@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)rKrLrM)r-rKrLsamplerr(r(r)r?szNormalDistribution.__init__cCs|jS)N)rK)r-r(r(r)r0szNormalDistribution.expectationcCs||j|jS)N)rMrKrL)r-r(r(r)r1szNormalDistribution.sampleN) r2r3r4rFr5rC normalvariater?r0r1r(r(r(r)rJs  rJc@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) )rKrLrMcCs"|dks t||_||_||_dS)Nr)r#rKrLrM)r-rKrLrNr(r(r)r?s zLogNormalDistribution.__init__cCst|j|jddS)Ng@)mathexprKrL)r-r(r(r)r0sz!LogNormalDistribution.expectationcCs||j|jS)N)rMrKrL)r-r(r(r)r1szLogNormalDistribution.sampleN) r2r3r4rFr5rClognormvariater?r0r1r(r(r(r)rPs rPc@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) )shapescalerMcCs.|dks t|dkst||_||_||_dS)Nr)r#rVrWrM)r-rWrVrNr(r(r)r?s   zGammaDistribution.__init__cCs |j|jS)N)rVrW)r-r(r(r)r0szGammaDistribution.expectationcCs||j|jS)N)rMrVrW)r-r(r(r)r1szGammaDistribution.sampleN) r2r3r4rFr5rC gammavariater?r0r1r(r(r(r)rUs  rUc@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) )alphabetarMcCs.|dks t|dkst||_||_||_dS)Nr)r#rZr[rM)r-rZr[rNr(r(r)r?s   zBetaDistribution.__init__cCs|jt|j|jS)N)rZfloatr[)r-r(r(r)r0szBetaDistribution.expectationcCs||j|jS)N)rMrZr[)r-r(r(r)r1szBetaDistribution.sampleN) r2r3r4rFr5rC betavariater?r0r1r(r(r(r)rYs  rYc 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)Nr9) max_allowedrz8Reference model is missing variable stage annotation: %sz3At least one variable stage assignment is required.cSs|dS)Nrr()rEr(r(r)z&_map_variable_stages..)keycSs|dS)Nrr()rEr(r(r)r_r`rQz~Embedded stochastic programs must be two-stage (for now), but variable with name '%s' has been annotated with stage number: %sT)active descend_intosort)rQFzhInvalid stage annotation for variable with name '%s'. Stage assignment must be 1 or 2. Current value: %s)rrr:r;r2r#rexpand_entriesminr7maxitemsnamecomponent_data_objectsr rZalphabetizeComponentAndIndexgetappend) modelZvariable_stage_annotationZvariable_stage_assignmentsZmin_stagenumberZmax_stagenumbervarstagenumderivedstage_to_variables_mapZ stagenumbervariable_to_stage_mapZ stagevarsr(r(r)_map_variable_stagess`      rscCst|tdd}t|dkr*tdtjnt|dks:t|dd}t|}t|dkrftdx0|D](}t|t s~t| rltd|j qlW|S)Nr9)r^rz9Reference 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.) rrr:r;r2r#rre isinstancerrri)rmZstochastic_data_annotationstochastic_data paramdatar(r(r)_extract_stochastic_data/s*      rwc@seZdZeddZeddZddZddZed d Z ed d Z ed dZ dddZ ddZ ddZdddZdddZdS) EmbeddedSPcCs(i}xt|D]}||t|<qW|S)N)EXPRZidentify_mutable_parametersid)rSansparamr(r(r)_collect_mutable_parametersLsz&EmbeddedSP._collect_mutable_parameterscCs(i}xt|D]}||t|<qW|S)N)ryZidentify_variablesrz)rSr{rnr(r(r)_collect_variablesSszEmbeddedSP._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|_|jddkstt|jtd|_g}x*|jD] }|j tk r|!|qWx|D]}|j|=q.Wt"|jt#dd}t$|dkrnt%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 4|g!| |j | !|qWt$|j | dkr|j | =n | 5| t|6| j23}g|j| <x2|D]*}|j4|g!| |j| !|q Wt$|j| dkr6|j| =q6Wt7}t8}x|jj-t9d d d D]}t|1|j:3}t|1|j;3}t|1|j<3}t$|r|5|t$|st$|st$|r$|j5|t=t$|pt$|t=t$|pt$|d i}x0t>?|||D]}||jkr8||t@|<q8Wt$|dkrg|j|<x6|3D]*}|j 4|g!||j|!|q|Wt|6|j;3}g|j |<x2|D]*}|j 4|g!||j |!|qWq|Wx|jj-tAd d d D]|}xt|BD]h\}}t|1|3}||jkrbt%d|jC|jC|jCf|j 4|g!||j 4|g!|q$WqWtD}xT|jj-td dD]>}t|1|jE3}t|1|jF3}t$|dkst$|dkr"|j5|t=t$|dkt=t$|dkd g|j|<x>|D]6}||jkr2|j4|g!||j|!|q2Wt$|j|dkr|j|=g|j|<x>|D]6}||jkr|j4|g!||j|!|qWt$|j|dkr|j|=qWt$t"|jt7dkrt%dt$t"|jt8dkr0t%dt$t"|jt+dkrNt%d| jGrvtH|jdrhttI|jd| |jGrtH|jdrttI|jd||jGrtH|jdrttI|jd||jGrtH|jdrttI|jd|dS)Nz+reference model input must be a Pyomo modelrr9)ctype)r^z4Reference model is missing stage cost annotation: %srQz/Missing stage cost annotation for time stage: 1z/Missing stage cost annotation for time stage: 2T)rbrcz>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')rczSReference 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)Jreference_modelZ objective time_stagesrqrrrurZvariable_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_symbolsrtr TypeErrorrwrs_variable_stage_assignmentsr<sortedr#r r Zparent_componentrrlrrr:r;r2rerh _stage1_cost _stage2_costr enumeraterjr ZsenseZobjective_senser}exprr7 setdefaultZdeclarer~rrrlowerbodyupperbool itertoolschainrzrZ get_itemsrirrrZhas_declarationshasattrsetattr)r-rZkeys_to_deleternraZstage_cost_annotationZstage_cost_assignmentsZ stage1_costZ stage2_costcdataroZsto_objZobjcntrobjZ obj_paramsrvZ obj_variablesZ sto_conboundsZ sto_conbodyconZ lower_paramsZ body_paramsZ upper_paramsZall_stochastic_paramsr|Zbody_variablesZsosconrAZ weight_paramsZ sto_varboundsr(r(r)r?Zs                                                      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) Nrt1t2rootr9ns)ZStagesZNodesZ Scenarios)r&g?t)r#rr rlstrZcreate_instanceZ NodeStageZConditionalProbabilityr\ZChildrenaddclearZScenarioLeafNoderriZ StageCostrrrhZStageVariablesZStageDerivedVariables)r-sizeZstmZ_stages_nodesZ _scenariosirAZ node_nameZ scen_namernrorpZ 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)r:r)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)r:r)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)r:rr)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. r9)rfrrgrtrrrr rrr<r~r7rr) r-rZderived_last_stageZstageZ laststageZvars_rnZvarstagerpr(r(r)compute_time_stages*"          zEmbeddedSP.compute_time_stagecKs|dks t|j}||}t|j|d}t}x|D]}||||<q.model_callback)rmr)r)r#rrrrrrr) r-rr/rrrrrarr()r-r)generate_sample_sp7s     zEmbeddedSP.generate_sample_spcCs4x |jD]\}}||_q W|r0|jSdS)N)rurhr1r$rr)r-rr|rr(r(r)r1JszEmbeddedSP.samplecCs4x |jD]\}}||_q W|r0|jSdS)N)rurhr0r$rr)r-rr|rr(r(r)set_expected_valuePszEmbeddedSP.set_expected_valueN)F)F)F)r2r3r4 staticmethodr}r~r?rpropertyrrrrrrr1rr(r(r(r)rxJs  ?&    @( rx)G__all__osrrCrRZpyomo.common.collectionsrZpyomo.core.exprrryZpyomo.core.base.paramZpyomoZpyomo.core.baserZpyomo.core.base.numvaluerrZpyomo.core.base.blockrrr Zpyomo.core.base.varr r Zpyomo.core.base.objectiver r Zpyomo.core.base.constraintrrZpyomo.core.base.sosrrZpyomo.pysp.annotationsrrrrrrrrZ&pyomo.pysp.scenariotree.tree_structurerZ,pyomo.pysp.scenariotree.tree_structure_modelrZpyomo.pysp.scenariotree.managerrZ(pyomo.pysp.scenariotree.instance_factoryrrrZ six.movesr r!pathabspath__file__rr*objectr+r6rGrJrPrUrYrsrwrxr(r(r(r) sD     (      #@