! Bard MPEC(Math Program with Equilibrium constraints) model;
!  Three players each have their own objective function.
  Player 0 is special in that he gets to make his decisions
  first, however, his profit depends upon what decisions
  the other 2 players make;
! Keywords: MPEC, Equilibrium, Game theory, 
   Complementarity Constraints, Stackelberg game;
! Player 0, the leader, controls the x variables;
  MAX = (200 - y11 - y21)*(y11 + y21) + (160-y12-y22)*(y12+y22);
    x11 + x12 + x21 + x22 <= 40;
    x11 <= 10;  x12 <= 5;
    x21 <= 15;  x22 <= 20;

! Player 1 controls only y11 and y12, based
   on the x11 and x12 that player 0 gave him.
! Min = (y11-4)^2 + ( y12 - 13)^2;
! 1st order optimality conditions for y11 and y12;
    rc11 = 2*(y11-4) + .4*LM11 + .6*LM12 + LM13;
    rc12 = 2*(y12 -13) + .7*LM11 + .3*LM12 + LM14;
   .4*y11 + .7*y12 + s11 = x11;
   .6*y11 + .3*y12 + s12 = x12;
      y11          + s13 = 20;
               y12 + s14 = 20;
 ! Complementarity conditions at optimum for Player 1;
 rc11*y11 = 0; ! If opportunity(reduced) cost > 0...;
 rc12*y12 = 0; !  then do not use any of y;
 s11*LM11 = 0; ! If we surplus > 0, the price = 0;
 s12*LM12 = 0;
 s13*LM13 = 0;
 s14*LM14 = 0;

! Player 2's problem. He controls only y21 and y22.
  ! Min = (y21 - 35)^2 + ( y22 - 2)^2;
! 1st order optimality conditions for y21 and y22;
    rc21 = 2*(y21-35) + .4*LM21 + .6*LM22 + LM23;
    rc22 = 2*(y22 -2) + .7*LM21 + .3*LM22 + LM24;
   .4*y21 + .7*y22 + s21 = x21;
   .6*y21 + .3*y22 + s22 = x22;
      y21          + s23 = 40; 
               y22 + s24 = 40;
 ! Complementarity conditions at optimum for Player 2;
 rc21*y21 = 0;
 rc22*y22 = 0;
 s21*LM21 = 0;
 s22*LM22 = 0;
 s23*LM23 = 0;
 s24*LM24 = 0;