B _oL@sddlmZmZmZmZmZmZmZmZm Z m Z m Z ddl m Z mZddZddZddZd d Zd d Zd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Z d'd(Z!d)d*Z"d+d,Z#d-d.Z$d/d0Z%d1d2Z&d3d4Z'd5S)6) Block ConcreteModel Constraint ObjectiveParamSetVar inequalityRangeSetAny Expression)Disjunct DisjunctioncCsXt}tdd|_tdd|_dd}tddg|d|_t|jd|jdgd |_|S) zLSingle two-term disjunction which has all of ==, <=, and >= constraints ))bounds) cSsH|}|r2t|jdkd|_t|jdkd|_nt|jdkd|_dS)Nr)exprr)modelrac1xc2c)disjunctflagmr:/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/gdp/tests/models.pyd_rule s zmakeTwoTermDisj..d_ruler)rule)r)rrrrr dr disjunction)rr!rrr makeTwoTermDisjs  r&cCsdt}tdd|_tdd|_tdd|_dd}tddg|d |_t|jd|jdgd |_|S) zcSingle two-term disjunction which has all of ==, <=, and >= and one nonlinear constraint. )rr)r)r")icSsf|}|rFt|jdkd|_t|jdkd|_td|jdfd|_nt|j|jddkd|_dS)Nr)rr") rrrrwrc3yr)rrrrrr r!"s z)makeTwoTermDisj_Nonlinear..d_rulerr")r#)r) rrr+rr-r r$rr%)rr!rrr makeTwoTermDisj_Nonlinears   r.cCstt}tddgd|_t|j|_t|_dd}t|d|j_dd}t|d|j_ t |jj|jj gd |j_ |S) zSingle two-term disjunction with IndexedConstraints on both disjuncts. Does not bound the variables, so cannot be transformed by hull at all and requires specifying m values in bigm. r"r) initializecs(|fdd}tj|d|_dS)Ncsj|dkS)Nr)r)r$s)rrr c_rule<szFmakeTwoTermDisj_IndexedConstraints..disj1_rule..c_rule)r#)rrr0r)rr1r)rr disj1_rule9s z6makeTwoTermDisj_IndexedConstraints..disj1_rule)r#cs(|fdd}tj|d|_dS)Ncsj|dkS)Nr))r)r$r0)rrr r1DszFmakeTwoTermDisj_IndexedConstraints..disj2_rule..c_rule)r#)rrr0r)rr1r)rr disj2_ruleAs z6makeTwoTermDisj_IndexedConstraints..disj2_rule)r) rrr0rrrbr Z simpledisj1 simpledisj2rr%)rr2r3rrr "makeTwoTermDisj_IndexedConstraints/s r6cCst}tddgd|_t|jdddd|_t|jdddd|_dd }t|j|d |_d d }td dg|d|_ t |j d |j dgd|_ |S)zLSingle two-term disjunction with IndexedConstraints on both disjuncts. r"r)r/r)r"rrcSs|j||j|fS)N)lbsubs)rr0rrr bounds_ruleTszCmakeTwoTermDisj_IndexedConstraints_BoundedVars..bounds_rule)rcsJ|fdd}fdd}|r6tj|d|_ntj|d|_dS)Ncsj|dkS)Nr)r)r$r0)rrr true_rule[szQmakeTwoTermDisj_IndexedConstraints_BoundedVars..d_rule..true_rulecsj|dkS)Nr)r)r$r0)rrr false_rule^szRmakeTwoTermDisj_IndexedConstraints_BoundedVars..d_rule..false_rule)r#)rrr0r)rrr;r<r)rr r!Xs   z>makeTwoTermDisj_IndexedConstraints_BoundedVars..d_ruler)r#)r) rrr0rr8r9rrr rrr%)rr:r!rrr .makeTwoTermDisj_IndexedConstraints_BoundedVarsLs r=cCst}tdd|_t|_t|jdkd|j_t|_tdd|j_t|j|jjdkd|j_t |j|jgd|_ t |j|jjd|_ |S)zTwo-term disjunction which declares a local variable y on one of the disjuncts, which is used in the objective function as well. Used to test that we will treat y as global in the transformations, despite where it is declared. )rr))rr")r)r"r)r)) rrrr Zdisj1rconsdisj2r-rr%rZ objective)rrrr localVarhs r@cCsbt}tddgd|_t|jdd|_dd}tdddg|j|d |_d d }t|j|d |_|S) zThree-term indexed disjunctionr"r)r/)rr)rcSsf|}|dkr(t|j|dkd|_n:|dkrHt|j|dkd|_nttd|j|dd|_dS)Nr)rr"rrr)rrrrr )rrr0rrrr r!s z(makeThreeTermIndexedDisj..d_ruler)r#cSs(|jd|f|jd|f|jd|fgS)Nrr"r)r)rr0rrr disj_rulesz+makeThreeTermIndexedDisj..disj_rule) rrr0rrr rrr%)rr!rArrr makeThreeTermIndexedDisjsrBcsftdddg_tjdd_ddfdd }tj|d _tfd d jDd _S)z@Three-term disjunction with indexed constraints on the disjunctsr"rr))r )rcSs|}|j||kS)N)rr)r4irrrr r1sz4makeThreeTermDisj_IndexedConstraints..c_rulecs$|}t|jd|d|_dS)N)r#)rrIr)r$jr)r1rr r!sz4makeThreeTermDisj_IndexedConstraints..d_rule)r#csg|]}j|qSr)r$).0rD)rrr sz8makeThreeTermDisj_IndexedConstraints..)r)rrErrr r$rr%)r!r)r1rr $makeThreeTermDisj_IndexedConstraintss  rIcCspt}tdddgd|_tddgd|_t|jdd|_d d }t|j|j|d |_d d }t|j|d |_ |S)zTwo-term indexed disjunctionr"rr))r/rr4)irC)rcSsH|}|dkr&t|j|dkd|_|dkrDt|j|dkd|_dS)Nrr)rr4r)rrrZcons_aZcons_b)r$rDkrrrr disjunct_rules z4makeTwoTermIndexedDisjunction..disjunct_rule)r#csfddjDS)Ncsg|]}j|fqSr)r)rGrJ)rDrrr rHszDmakeTwoTermIndexedDisjunction..disj_rule..)B)rrDr)rDrr rAsz0makeTwoTermIndexedDisjunction..disj_rule) rrArLrrr rrr%)rrKrArrr makeTwoTermIndexedDisjunctionsrNcCsbt}tdddgd|_t|jdd|_dd}t|jd dg|d |_d d }t|j|d |_|S) zTTwo-term indexed disjunction. Adds nothing to above--exists for historic reasonsr"rr))r/)id)rcSs>|}|r$t|j|dkd|_nt|j|dkd|_dS)Nr7)rr))rrrr)r$r0rrrrr rKsz@makeTwoTermIndexedDisjunction_BoundedVars..disjunct_ruler)r#csfdddDS)Ncsg|]}j|fqSr)r)rGr)rr0rr rHszWmakeTwoTermIndexedDisjunction_BoundedVars..disjunction_rule..)rr"r)rr0r)rr0r disjunction_ruleszCmakeTwoTermIndexedDisjunction_BoundedVars..disjunction_rule) rrr0rrr rrr%)rrKrPrrr )makeTwoTermIndexedDisjunction_BoundedVarssrQcCsBt}tdd|_|ddgdd}|ddgdd}|S) zDTwo-term indexed disjunction where one of the two indices is skipped)rrC)rrr"cSs|}t|j|kd|_dS)N)r)rrrr>)r$rDrrrr disjunctssz3makeIndexedDisjunction_SkipIndex..disjunctscSs"|dkrtjS|j||jdgS)Nr)rZSkiprR)rrDrrr disjunctionssz6makeIndexedDisjunction_SkipIndex..disjunctions)rrrr r)rrRrSrrr makeIndexedDisjunction_SkipIndexs  rTcCs|t}tddgd|_tddgd|_t|j|jdd|_dd }td dg|j|j|d |_d d }t|j|j|d |_ |S)z/Two-term indexed disjunction with tuple indicesr"r)r/rMrL)rr)rcSsF|}|r(t|j||fdkd|_nt|j||fdkd|_dS)Nr)rr)rrrr)rrr0trrrr r!sz2makeTwoTermMultiIndexedDisjunction..d_ruler)r#cSs |jd||f|jd||fgS)Nrr")r)rr0rUrrr rAsz5makeTwoTermMultiIndexedDisjunction..disj_rule) rrr0rUrrr rrr%)rr!rArrr "makeTwoTermMultiIndexedDisjunctionsrVcCsHt}t|_tdd|_|jddgdd}|jdd}|S) z%Two-term SimpleDisjunction on a block)rr)rrr"cSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr))rr)rrrr)rrrrrr rsz(makeTwoTermDisjOnBlock..disjunctcSs|jd|jdgS)Nrr")r)rrrr r%sz+makeTwoTermDisjOnBlock..disjunction)rrr4rrr r)rrr%rrr makeTwoTermDisjOnBlocks  rWcCs@dd}t|d|_dd}t|d|_t|j|jgd|_|S)NcSs|}t|jdkd|_dS)Nr))r)rrrr)rrrrr simpdisj_rulesz,add_disj_not_on_block..simpdisj_rule)r#cSs|}t|jdkd|_dS)Ng @)r)rrrr)rrrrr simpledisj2_rulesz/add_disj_not_on_block..simpledisj2_rule)r)r Z simpledisjr5r disjunction2)rrXrYrrr add_disj_not_on_block s   r[csRttddgd_tjdd_jddgdd}d d }tj|d _tddg_ td dj d_ fd d}tddg|d j d_ dd}tdg|d j d_ tddj d_ tj d_tj dj dkdj dj_tj d_tj dj dkdj dj_tj djj djgdj d_ S)zpTwo disjunctions (one indexed an one not), each on a separate BlockData of an IndexedBlock of length 2 r"r)r/)rF)rrcSs>|}|s$t|j|dkd|_nt|j|dkd|_dS)Nr)rr)rrrr)rr0rrrrr disjunct1!sz1makeDisjunctionsOnIndexedBlock..disjunct1csfdddDS)Ncsg|]}j|fqSr)r])rGr)rr0rr rH*szMmakeDisjunctionsOnIndexedBlock..disjunction1_rule..)rr"r)rr0r)rr0r disjunction1_rule)sz9makeDisjunctionsOnIndexedBlock..disjunction1_rule)r#)rcs:|stjdjdkd|_ntjdjdkd|_dS)Nr)r)rr4rr)rr)rrr disjunct2_rule0sz6makeDisjunctionsOnIndexedBlock..disjunct2_rulecSs|jd|jdgS)Nrr")r)r4rDrrr r%7sz3makeDisjunctionsOnIndexedBlock..disjunction)r(r))r)rrr0rrr r disjunction1rr4rrr%r-Z disjunct0rrr])r]r^r`r%r)rr makeDisjunctionsOnIndexedBlocks(   $rbcCsXt}tdd|_tdd|_dd}tddg|d|_t|jd|jdgd |_|S) zmSimpleDisjunction where one of the Disjuncts contains three different blocks: two simple and one indexed)ri)r)ri cSs|}|rt|_t|jdkd|j_|dt|jdkdt|j_t|jdkd|jj_tdg|_ t|jdkd|j d_nt|jdkd|_dS)Nr)rzb.cr r"P) rrr4rrrZ add_componentr-Z anotherblockZbb)r$rrrrr rALs  z.makeTwoTermDisj_BlockOnDisj..disj_rulerr")r#)r)rrrr-r Zevilrr%)rrArrr makeTwoTermDisj_BlockOnDisjEs   recCs|t}tdd|_tdd|_tdd|_dd}tddg|d |_d d }t|d |_t|j|jd|jdgd |_ |S) aThree-term SimpleDisjunction built from two IndexedDisjuncts and one SimpleDisjunct. The SimpleDisjunct and one of the DisjunctDatas each contain a nested SimpleDisjunction (the disjuncts of which are declared on the same disjunct as the disjunction). (makeNestedDisjunctions_NestedDisjuncts is a much simpler model. All this adds is that it has a nested disjunction on a DisjunctData as well as on a SimpleDisjunct. So mostly it exists for historical reasons.) )ir)r)rrC)rcSsd|}|rNdd}tddg|d|_|dgdd}t|jdkd |_nt|jdkd |_dS) NcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr)rrzr)rrrrrr innerdisj_ruleoszEmakeNestedDisjunctions..disjunct_rule..innerdisj_rulerr")r#cSs|jd|jdgS)Nrr") innerdisjunct)r4rDrrr innerdisjunctionwszGmakeNestedDisjunctions..disjunct_rule..innerdisjunctionr)r)rr rirrrrr)rrrrhrjrrr rKlsz-makeNestedDisjunctions..disjunct_rulerr")r#csH||fdd}|fdd}t|j|jgd|_dS)Ncstjdkd|_dS)Nr)r)rrr)r)rrr innerdisjunct0szGmakeNestedDisjunctions..simpledisj_rule..innerdisjunct0cstjdkd|_dS)Nr)r)rrr)r)rrr innerdisjunct1szGmakeNestedDisjunctions..simpledisj_rule..innerdisjunct1)r)rr rrkrlrj)rrkrlr)rr simpledisj_rules z/makeNestedDisjunctions..simpledisj_rule)r) rrrrgrr rZsimpledisjunctrr%)rrKrmrrr makeNestedDisjunctions]s     rncCst}tdd|_t|jd|_t|_t|jdkd|j_t|_ t|jdkd|j _t|_ t|jdkd|j _t|_ t|jdkd|j _t |j|j gd|_ t |j |j gd|j_ |S)zTwo-term SimpleDisjunction where one of the disjuncts contains a nested SimpleDisjunction, the disjuncts of which are declared on the model)rr)r)rr"g?g333333?g?)rrrrobjr d1rrd2d3d4rdisj)rrrr $makeNestedDisjunctions_FlatDisjunctss rucCst}tdd|_t|jd|_t|_t|jdkd|j_t|_ t|jdkd|j _t|j_ t|jdkd|jj _t|j_ t|jdkd|jj _t |j|j gd|_ t |jj |jj gd|j_|S)zSame as makeNestedDisjunctions_FlatDisjuncts except that the disjuncts of the nested disjunction are declared on the parent disjunct.)rr)r)rr"g?g333333?g?)rrrrror rprrrqrrrsrrtr?)rrrr &makeNestedDisjunctions_NestedDisjunctss   rvcsttdd_dd}tddg|d_fdd }tddg|d_tjdjdgd _tjdjdgd _S) z)Two SimpleDisjunctions on the same model.)i2)rcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr)rrrr)rrrrrr d1_rulesz*makeTwoSimpleDisjunctions..d1_rulerr")r#cs.|stjdkd|_ntjdkd|_dS)N)rrO)rrr)rr)rrr d2_rulesz*makeTwoSimpleDisjunctions..d2_rule)r) rrrr r] disjunct2rrarZ)rxrzr)rr makeTwoSimpleDisjunctionss  r|csttdd_dd}tddg|d_fdd }tddg|d_tjdjdgd _tjdjdgd _jd S) zaThis is not a transformable model! Two SimpleDisjunctions which have a shared disjunct. )irw)rcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr)rrrr)rrrrrr rxsz3makeDisjunctInMultipleDisjunctions..d1_rulerr")r#cs.|stjdkd|_ntjdkd|_dS)Nry)rrO)rrr)rr)rrr rzsz3makeDisjunctInMultipleDisjunctions..d2_rule)r) rrrr r]r{rrarZZ deactivate)rxrzr)rr "makeDisjunctInMultipleDisjunctionss  r}cCsLt}tdd|_dd}tddg|d|_t|jd|jdgd|_|S) aDNot a transformable model (because of disjuncts shared between disjunctions): A SimpleDisjunction where one of the disjuncts contains two SimpleDisjunctions with the same Disjuncts. This is a lazy way to test that we complain about untransformed disjunctions we encounter while transforming a disjunct. )rr')rcSsv|}|r`dd}tddg|d|_t|jd|jdgd|_t|jd|jdgd|_nt|jdkd|_dS)NcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr)rrrr)r$rrrrr rhszOmakeDuplicatedNestedDisjunction..outerdisj_rule..innerdisj_rulerr")r#)rr') rr rirrjZduplicateddisjunctionrrr)r$rrrhrrr outerdisj_rules  z7makeDuplicatedNestedDisjunction..outerdisj_rulerr")r#)r)rrrr Z outerdisjunctrr%)rr~rrr makeDuplicatedNestedDisjunctions  rcs\ttdd_t_tdj_tfdddj_t_ t jj gd_ S)zNTwo-term SimpleDisjunction where one of the disjuncts contains a RangeSet)rr")rr"cs jdkS)Nr")r)_)rrr z*makeDisjunctWithRangeSet..)r#)r) rrrr rpr r0rrrqrrtrr)rr makeDisjunctWithRangeSets  rcsdttdd_t_tjddj_tfdddj_t_ t jj gd_ S)zTwo-term SimpleDisjunction where one of the disjuncts contains an Expression. This is used to make sure that we correctly handle types we hit in disjunct.component_objects(active=True))rr")rr)rcs jdkS)Nr")r)r)rrr r#rz,makeDisjunctWithExpression..)r#) rrrr rpr errrqrrtrr)rr makeDisjunctWithExpressions rcCst}tdd|_t|jd|_tddgd|_t|j|_t |jdkd|jd_ t |jdkd|jd_ t|j|_ t |jdkd|j d_ t |jdkd|j d_ t |jd|j dgd|_ t |jd|j dgd|_|S) zTwo SimpleDisjunctions, where each are disjunctions of DisjunctDatas. This adds nothing to makeTwoSimpleDisjunctions but exists for convenience because it has the same mathematical meaning as makeAnyIndexedDisjunctionOfDisjunctDatas )irO)r)rr"r)r/rr))rrrrroridxr firstTermrr> secondTermrr%rZ)rrrr makeDisjunctionOfDisjunctDatas(s   rcCst}tdd|_t|jd|_tddgd|_t|j|_t |jdkd|jd_ t |jdkd|jd_ t|j|_ t |jdkd|j d_ t |jdkd|j d_ t t |_|jd|j dg|jd<|jd|j dg|jd<|S) aGAn IndexedDisjunction indexed by Any, with two two-term DisjunctionDatas build from DisjunctDatas. Identical mathematically to makeDisjunctionOfDisjunctDatas. Used to test that the right things happen for a case where soemone implements an algorithm which iteratively generates disjuncts and retransforms)irO)r)rr"r)r/rr))rrrrrorrr rrr>rrr r%)rrrr (makeAnyIndexedDisjunctionOfDisjunctDatas?s    rN)(Z pyomo.corerrrrrrrr r r r Z pyomo.gdpr rr&r.r6r=r@rBrIrNrQrTrVrWr[rbrernrurvr|r}rrrrrrrrr s44 ,6