\$title Simple bilevel model, both inner and outer problems have constraints (EMPBP03,SEQ=422) \$ontext This model compares the results obtained by EMP with the ones obtained using the corresponding hand-crafted model. Both, the outer minimization problem and the inner minimization problem contains constraints Contributor: Jan-H. Jagla, January 2009 \$offtext \$if not set nlpsolver \$set nlpsolver conopt *------------------------------------------------------------------------------- *Prepare reporting Parameter rep comparison; sets enames / outerobj,outereq1,innerobj,innereq1 / vnames / out,x1,x2,in,y1,y2,y3 / evnames / set.enames,set.vnames / suf / l /; \$gdxout "%gams.scrdir%repsets.%gams.scrext%" \$unload evnames suf \$gdxout \$onecho > "%gams.scrdir%repmaker.%gams.scrext%" set evnames, suf; \$gdxin "%gams.scrdir%repsets.%gams.scrext%" \$load evnames suf file fx /"%gams.scrdir%report.%gams.scrext%"/; put fx; loop((evnames,suf), \$escape = put "rep('" evnames.te(evnames) "','" suf.te(suf) "','%=1')" put " = " evnames.te(evnames) "." suf.te(suf) ";" /; \$escape % ); \$offecho \$call gams "%gams.scrdir%repmaker.%gams.scrext%" lo=0 *------------------------------------------------------------------------------- Equations outerobj outereq1 innerobj innereq1; Variables out in; Positive Variables x1 x2 y1 y2 y3; outerobj.. out =e= sqr(y1) + sqr(y3) - y1*y3 - 4*y2 - 7*x1 + 4*x2; outereq1.. x1 + x2 =l= 1; innerobj.. in =e= sqr(y1) + 0.5*sqr(y2) + 0.5*sqr(y3) + y1*y2 + (1-3*x1)*y1 + (1+x2)*y2; innereq1.. 2*y1 + y2 - y3 + x1 - 2*x2 + 2 =l= 0; model outer / outerobj, outereq1 / inner / innerobj, innereq1 / bilevel / outer, inner /; \$echo bilevel x1 x2 min in * innerobj innereq1 > "%emp.info%" option dnlp=%nlpsolver%; solve bilevel using emp mininizing out; abort\$(bilevel.solvestat <> %SOLVESTAT.NORMAL COMPLETION%) 'EMP solver status <> 1'; \$batinclude "%gams.scrdir%report.%gams.scrext%" emp *Verify solution *KKT conditions for inner minimization problem Negative Variables u2; Equations dLdy1,dLdy2,dLdy3; dLdy1.. ( - (2*y1 + y2 + 1 - 3*x1))/(-1) - u2*2 =N= 0; dLdy2.. ( - (y2 + y1 + 1 + x2))/(-1) - u2 =N= 0; dLdy3.. ( - y3)/(-1) + u2 =N= 0; Model manual / outer, innereq1.u2, dLdy1.y1, dLdy2.y2, dLdy3.y3 /; solve manual using mpec minimizing out; abort\$(manual.solvestat <> %SOLVESTAT.NORMAL COMPLETION%) 'Hand-made model solver status <> 1'; \$batinclude "%gams.scrdir%report.%gams.scrext%" manual *------------------------------------------------------------------------------- *Reporting Differences rep(evnames,suf,'diff') = abs(rep(evnames,suf,'emp') - rep(evnames,suf,'manual')); rep(evnames,suf,'diff')\$(rep(evnames,suf,'diff') < 1e-5) = 0; display rep; abort\$sum((evnames,suf),rep(evnames,suf,'diff')) 'Solutions differ';