B u `']@sTddlmZmZmZmZmZmZmZmZm Z m Z m Z m Z ddl mZmZddZddZddZd d Zd d Zd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Z d%d&Z!d'd(Z"d)d*Z#d+d,Z$d-d.Z%d/d0Z&d1d2Z'd3d4Z(d5d6Z)d7d8Z*d9d:Z+d;d<Z,d=d>Z-d?d@Z.dAdBZ/dCdDZ0dES)F) Block ConcreteModel Constraint ObjectiveParamSetVar inequalityRangeSetAny Expressionmaximize)Disjunct DisjunctioncCspt}tdd|_t|_t|jdkd|j_t|_t|jdkd|j_t |j|jgd|_ t |jd|_ |S)N)r )bounds)exprr) rrxrdisj1rZxTruedisj2ZxFalser disjunctionrobj)mr:/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/gdp/tests/models.pyoneVarDisj_2ptss rcCst}tdd|_t|_ttd|jdd|j_t|_ttd|jdd|j_t |j|jgd|_ t |j|jj d|_ |S)N)r)rrr)rr)rrrrrrr crrrrZ indicator_varr)rrrrtwoSegments_SawayaGrossmanns r cCsXt}tdd|_tdd|_dd}tddg|d|_t|jd|jdgd |_|S) zLSingle two-term disjunction which has all of ==, <=, and >= constraints )r)r) cSsH|}|r2t|jdkd|_t|jdkd|_nt|jdkd|_dS)Nr)rr!)modelrac1rc2r)disjunctflagrrrrd_rule's zmakeTwoTermDisj..d_rulerr)rule)r)rrr&rrdrr)rr+rrrmakeTwoTermDisj s  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)rrr) r%rrr'wr(c3yr)r)r*rrrrr+<s z)makeTwoTermDisj_Nonlinear..d_rulerr)r,)r) rrr2rr4rr-rr)rr+rrrmakeTwoTermDisj_Nonlinear3s   r5cCstt}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. rr) initializecs(|fdd}tj|d|_dS)Ncsj|dkS)Nr)r&)r-s)rrrc_ruleVszFmakeTwoTermDisj_IndexedConstraints..disj1_rule..c_rule)r,)r%rr7r)r)r8r)rr disj1_ruleSs z6makeTwoTermDisj_IndexedConstraints..disj1_rule)r,cs(|fdd}tj|d|_dS)Ncsj|dkS)Nr)r&)r-r7)rrrr8^szFmakeTwoTermDisj_IndexedConstraints..disj2_rule..c_rule)r,)r%rr7r)r)r8r)rr disj2_rule[s z6makeTwoTermDisj_IndexedConstraints..disj2_rule)r) rrr7rr&rbrZ simpledisj1 simpledisj2rr)rr9r:rrr"makeTwoTermDisj_IndexedConstraintsIs r=cCst}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. rr)r6r")rrr!cSs|j||j|fS)N)lbsubs)rr7rrr bounds_rulenszCmakeTwoTermDisj_IndexedConstraints_BoundedVars..bounds_rule)rcsJ|fdd}fdd}|r6tj|d|_ntj|d|_dS)Ncsj|dkS)Nr)r&)r-r7)rrr true_ruleuszQmakeTwoTermDisj_IndexedConstraints_BoundedVars..d_rule..true_rulecsj|dkS)Nr$)r&)r-r7)rrr false_rulexszRmakeTwoTermDisj_IndexedConstraints_BoundedVars..d_rule..false_rule)r,)r%rr7r)r)r*rBrCr)rrr+rs   z>makeTwoTermDisj_IndexedConstraints_BoundedVars..d_ruler)r,)r) rrr7rr?r@rr&rr)rr)rrAr+rrr.makeTwoTermDisj_IndexedConstraints_BoundedVarsfs rDcCst}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)rrr) rrrrrrconsrr4rrr objective)rrrrlocalVars rGcCsbt}tddgd|_t|jdd|_dd}tdddg|j|d |_d d }t|j|d |_|S) zThree-term indexed disjunctionrr)r6)rr!)rcSsf|}|dkr(t|j|dkd|_n:|dkrHt|j|dkd|_nttd|j|dd|_dS)Nr)rrr$rr")r%rr&rr )r)r*r7rrrrr+s z(makeThreeTermIndexedDisj..d_ruler)r,cSs(|jd|f|jd|f|jd|fgS)Nrrr)r))rr7rrr disj_rulesz+makeThreeTermIndexedDisj..disj_rule) rrr7rr&rr)rr)rr+rHrrrmakeThreeTermIndexedDisjsrIcCsht}tdd|_tdd|_dd}tddg|d|_dd }t|d|_t|jd |jd |_ |S) N)rr$)rcSsj|}|r:ttd|jdd|_ttd|jdd|_n,ttd|jdd|_ttd|jdd|_dS)Nrr)rrr")r%rr rr'r4r()r)r*rrrrr+s z%makeTwoTermDisj_boxes..d_rulerr)r,cSs|jd|jdgS)Nrr)r-)rrrrrHsz(makeTwoTermDisj_boxes..disj_ruler)r) rrrr4rr-rrrr)rr+rHrrrmakeTwoTermDisj_boxess   rJcsftdddg_tjdd_ddfdd }tj|d _tfd d jDd _S)z@Three-term disjunction with indexed constraints on the disjunctsrrr)rr)rcSs|}|j||kS)N)r%r)r;irrrrr8sz4makeThreeTermDisj_IndexedConstraints..c_rulecs$|}t|jd|d|_dS)N)r,)r%rIr)r-jr)r8rrr+sz4makeThreeTermDisj_IndexedConstraints..d_rule)r,csg|]}j|qSr)r-).0rK)rrr sz8makeThreeTermDisj_IndexedConstraints..)r)rrLrrrr-rr)r+r)r8rr$makeThreeTermDisj_IndexedConstraintss  rPcCspt}tdddgd|_tddgd|_t|jdd|_d d }t|j|j|d |_d d }t|j|d |_ |S)zTwo-term indexed disjunctionrrr)r6r&r;)ir)rcSsH|}|dkr&t|j|dkd|_|dkrDt|j|dkd|_dS)Nr&r$)rr;r)r%rrZcons_aZcons_b)r-rKkrrrr disjunct_rules z4makeTwoTermIndexedDisjunction..disjunct_rule)r,csfddjDS)Ncsg|]}j|fqSr)r))rNrQ)rKrrrrOszDmakeTwoTermIndexedDisjunction..disj_rule..)B)rrKr)rKrrrHsz0makeTwoTermIndexedDisjunction..disj_rule) rrArSrrrr)rr)rrRrHrrrmakeTwoTermIndexedDisjunctionsrUcCsbt}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 reasonsrrr)r6)id)rcSs>|}|r$t|j|dkd|_nt|j|dkd|_dS)Nr>)rr)r%rr&r)r-r7r*rrrrrRsz@makeTwoTermIndexedDisjunction_BoundedVars..disjunct_ruler)r,csfdddDS)Ncsg|]}j|fqSr)r))rNr*)rr7rrrOszWmakeTwoTermIndexedDisjunction_BoundedVars..disjunction_rule..)rrr)rr7r)rr7rdisjunction_ruleszCmakeTwoTermIndexedDisjunction_BoundedVars..disjunction_rule) rrr7rr&rr)rr)rrRrWrrr)makeTwoTermIndexedDisjunction_BoundedVarssrXcCsBt}tdd|_|ddgdd}|ddgdd}|S) zDTwo-term indexed disjunction where one of the two indices is skipped)rr)rrrcSs|}t|j|kd|_dS)N)r)r%rrrE)r-rKrrrr disjunctssz3makeIndexedDisjunction_SkipIndex..disjunctscSs"|dkrtjS|j||jdgS)Nr)rZSkiprY)rrKrrr disjunctions sz6makeIndexedDisjunction_SkipIndex..disjunctions)rrrrr)rrYrZrrr makeIndexedDisjunction_SkipIndexs  r[cCs|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 indicesrr)r6rTrS)rr!)rcSsF|}|r(t|j||fdkd|_nt|j||fdkd|_dS)Nr)rr$)r%rr&r)r)r*r7trrrrr+sz2makeTwoTermMultiIndexedDisjunction..d_ruler)r,cSs |jd||f|jd||fgS)Nrr)r))rr7r\rrrrH!sz5makeTwoTermMultiIndexedDisjunction..disj_rule) rrr7r\rr&rr)rr)rr+rHrrr"makeTwoTermMultiIndexedDisjunctionsr]cCsHt}t|_tdd|_|jddgdd}|jdd}|S) z%Two-term SimpleDisjunction on a block)rr$)rrrcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr)r%rr&r)r)r*rrrrr)-sz(makeTwoTermDisjOnBlock..disjunctcSs|jd|jdgS)Nrr)r))rrrrr5sz+makeTwoTermDisjOnBlock..disjunction)rrr;rr&rr)rr)rrrrmakeTwoTermDisjOnBlock's  r^cCs@dd}t|d|_dd}t|d|_t|j|jgd|_|S)NcSs|}t|jdkd|_dS)Nr)r)r%rr&r)r)rrrr simpdisj_rule<sz,add_disj_not_on_block..simpdisj_rule)r,cSs|}t|jdkd|_dS)Ng @)r)r%rr&r)r)rrrrsimpledisj2_rule@sz/add_disj_not_on_block..simpledisj2_rule)r)rZ simpledisjr<r disjunction2)rr_r`rrradd_disj_not_on_block;s   rbcsRttddgd_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 rr)r6)rF)rrcSs>|}|s$t|j|dkd|_nt|j|dkd|_dS)Nr)rr!)r%rr&r)r)r7r*rrrr disjunct1Osz1makeDisjunctionsOnIndexedBlock..disjunct1csfdddDS)Ncsg|]}j|fqSr)rd)rNr*)rr7rrrOXszMmakeDisjunctionsOnIndexedBlock..disjunction1_rule..)rrr)rr7r)rr7rdisjunction1_ruleWsz9makeDisjunctionsOnIndexedBlock..disjunction1_rule)r,)rcs:|stjdjdkd|_ntjdjdkd|_dS)Nr)r)rr;rr)r)r*)rrrdisjunct2_rule^sz6makeDisjunctionsOnIndexedBlock..disjunct2_rulecSs|jd|jdgS)Nrr)r))r;rKrrrresz3makeDisjunctionsOnIndexedBlock..disjunction)r0r)r)rrr7rr&rr disjunction1rr;rr)rr4Z disjunct0rrrd)rdrergrr)rrmakeDisjunctionsOnIndexedBlockGs(   $ricCsXt}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# rP) r%rr;rrrZ add_componentr4Z anotherblockZbb)r-r*rrrrrHzs  z.makeTwoTermDisj_BlockOnDisj..disj_rulerr)r,)r)rrrr4rZevilrr)rrHrrrmakeTwoTermDisj_BlockOnDisjss   rlcCs|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)rr)rcSsd|}|rNdd}tddg|d|_|dgdd}t|jdkd |_nt|jdkd |_dS) NcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr$)rr)r%rzr)r)r*rrrrinnerdisj_ruleszEmakeNestedDisjunctions..disjunct_rule..innerdisj_rulerr)r,cSs|jd|jdgS)Nrr) innerdisjunct)r;rKrrrinnerdisjunctionszGmakeNestedDisjunctions..disjunct_rule..innerdisjunctionr)r)r%rrprrr&rr)r)r*rrorqrrrrRsz-makeNestedDisjunctions..disjunct_rulerr)r,csH||fdd}|fdd}t|j|jgd|_dS)Ncstjdkd|_dS)Nr)r)rrr)r))rrrinnerdisjunct0szGmakeNestedDisjunctions..simpledisj_rule..innerdisjunct0cstjdkd|_dS)Nr")r)rrr)r))rrrinnerdisjunct1szGmakeNestedDisjunctions..simpledisj_rule..innerdisjunct1)r)r%rrrrrsrq)r)rrrsr)rrsimpledisj_rules z/makeNestedDisjunctions..simpledisj_rule)r) rrrrnr&rr)Zsimpledisjunctrr)rrRrtrrrmakeNestedDisjunctionss     rucCst}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)rrg?g333333?g?)rrrrrrd1rrd2d3d4rdisj)rrrr$makeNestedDisjunctions_FlatDisjunctss r{cCst}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)rrg?g333333?g?)rrrrrrrvrrrwrxryrrzr)rrrr&makeNestedDisjunctions_NestedDisjunctss   r|csttdd_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$)r%rr&r)r)r*rrrrd1_rulesz*makeTwoSimpleDisjunctions..d1_rulerr)r,cs.|stjdkd|_ntjdkd|_dS)N)rrV)rr&r)r)r*)rrrd2_rulesz*makeTwoSimpleDisjunctions..d2_rule)r) rrr&rrd disjunct2rrhra)r~rr)rrmakeTwoSimpleDisjunctionss  rcsttdd_dd}tddg|d_fdd }tddg|d_tjdjdgd _tjdjdgd _jd S) zaThis is not a transformable model! Two SimpleDisjunctions which have a shared disjunct. )ir})rcSs6|}|r t|jdkd|_nt|jdkd|_dS)Nr)rr$)r%rr&r)r)r*rrrrr~sz3makeDisjunctInMultipleDisjunctions..d1_rulerr)r,cs.|stjdkd|_ntjdkd|_dS)Nr)rrV)rr&r)r)r*)rrrrsz3makeDisjunctInMultipleDisjunctions..d2_rule)r) rrr&rrdrrrhraZ deactivate)r~rr)rr"makeDisjunctInMultipleDisjunctionss  rcCsLt}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)r%rrr)r-r*rrrrro*szOmakeDuplicatedNestedDisjunction..outerdisj_rule..innerdisj_rulerr)r,)rr/) r%rrprrqZduplicateddisjunctionrrr)r-r*rrorrrouterdisj_rule's  z7makeDuplicatedNestedDisjunction..outerdisj_rulerr)r,)r)rrrrZ outerdisjunctrr)rrrrrmakeDuplicatedNestedDisjunctions  rcs\ttdd_t_tdj_tfdddj_t_ t jj gd_ S)zNTwo-term SimpleDisjunction where one of the disjuncts contains a RangeSet)rr)rrcs jdkS)Nr)r)_)rrrCz*makeDisjunctWithRangeSet..)r,)r) rrrrrvr r7rrrwrrzrr)rrmakeDisjunctWithRangeSet<s  rcCst}tdd|_tdd|_t|_ttd|jdd|j_ttd|jdd|j_ t|_ ttd|jdd|j _ttd|jd d|j _ t |j|j gd|_ t |jd|jtd |_|S) N)r)rrr)rr!rr/r)rsense)rrrr4rrdrr constraintx constraintyrrrrr rF)rrrrgrossmann_oneDisjLs  rcCst}tdd|_tdd|_t|_ttd|jdd|j_ttd|jdd|j_ t|_ ttd|jd d|j _ttd|jd d|j _ t |j|j gd|_ t |jd|jtd |_|S) N)r)rrr)ruvxr)rr)rrrr4rrdrr rrrrrrr rF)rrrrto_break_constraint_tolerances^s  rcCst}t|_ttd|jdd|j_ttd|jdd|j_t|_ ttd|jdd|j _ttd|jd d|j _t |j|j gd|_ |S) Nrg@)rg@r/r#rjrg @) rrZ disjunct3rr rrr4rZ disjunct4rra)rrrrgrossmann_twoDisjpsrcCst}tdd|_tdd|_t|_t|jdd|jdddkd|j_t|_t|jdd|jdddkd|j_t |j|jgd|_ t |j|jt d |_ |S) N)rr/)r)rrrrr>)rr")rr)rrrr4rZ upper_circlerrEZ lower_circlerrrr r)rrrrtwoDisj_twoCircles_easys  **rcCst}t|_t|jdd|jdddkd|j_t|_t|jdd|jdddkd|j_t|j|jgd|_ |S)Nrr!r)rr$r) rrZ upper_circle2rrr4rEZ lower_circle2rra)rrrr fourCircless**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)rrrrrz,makeDisjunctWithExpression..)r,) rrrrrvr errrwrrzrr)rrmakeDisjunctWithExpressions 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 )irV)r)rrr)r6rr)rrrrrridxr firstTermrrE secondTermrrra)rrrrmakeDisjunctionOfDisjunctDatass   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)irV)r)rrr)r6rr)rrrrrrrrrrrErrr r)rrrr(makeAnyIndexedDisjunctionOfDisjunctDatass    rN)1Z pyomo.corerrrrrrrr r r r r Z pyomo.gdprrrr r.r5r=rDrGrIrJrPrUrXr[r]r^rbrirlrur{r|rrrrrrrrrrrrrrrrsD8  ,6