$title	A Simple Partial Equilibrium Trade Model with Corner Solutions

set	r	Regions /r1*r50/;
alias (r,rr);

parameter	cost(r)		Production cost
		c(r,rr)		Export cost
		alpha(r)	Demand coefficient;

*	Generate a random dataset:

cost(r)	= uniform(0.75,1.5);
c(r,rr) = uniform(0.25,0.75);
alpha(r) = uniform(1,5);

positive
variables	p(r)	Market price
		x(r,r)	Trade flows
		y(r)	Production;

equations eq_x, eq_y, mkt;

eq_x(r,rr)..	p(r) + c(r,rr) =g= p(rr);

eq_y(r)..	cost(r) =g= p(r);

mkt(r)..	y(r) + sum(rr, x(rr,r)-x(r,rr)) =e= alpha(r) * p(r)**(-2);

p.lo(r) = 0.01;
p.l(r) = 1;
y.l(r) = 1;

*	The model statement indicates complementarity between 
*	variables and equations.  Zero profit conditions for
*	trade and production are associated with quantities 
*	shipped and produced.  Market clearance conditions are
*	associated with market prices:

model peq /eq_x.x, eq_y.y, mkt.p/;

*	Eliminate degeneracy:

x.fx(r,r) = 0;

solve peq using mcp;

parameter	equil	Equilibrium values;
equil(r,"y") = y.l(r);
equil(r,"p") = p.l(r);
equil(r,"export") = sum(rr, x.l(r,rr));
equil(r,"import") = sum(rr, x.l(rr,r));
display equil;


---- 52 PARAMETER equil Equilibrium values y p export import r1 6.226 0.879 1.417 r2 1.229 1.798 r3 1.157 1.917 r4 3.162 0.976 r5 3.229 0.969 r6 5.157 0.918 r7 4.748 1.012 r8 1.165 1.389 r9 21.634 0.800 16.592 r10 2.157 1.125 r11 1.097 3.739 r12 1.100 4.011 r13 1.108 1.742 r14 1.177 3.611 r15 6.160 0.848 r16 1.146 1.417 r17 6.034 0.870 r18 5.311 0.938 r19 1.049 2.387 r20 1.043 3.135 r21 4.298 1.020 r22 1.225 1.014 r23 5.112 0.849 r24 8.839 0.863 2.283 r25 3.072 1.192 r26 1.052 3.001 r27 2.092 0.923 r28 1.051 2.774 r29 1.097 3.620 r30 2.216 0.978 r31 18.598 0.833 12.101 r32 2.813 1.127 r33 10.160 0.870 3.619 r34 1.065 3.023 r35 2.316 0.949 r36 1.885 0.964 r37 1.170 1.821 r38 1.088 0.973 r39 1.121 2.283 r40 1.075 4.316 r41 2.649 1.060 r42 5.389 0.838 r43 2.264 0.986 r44 22.181 0.785 16.296 r45 1.688 1.004 r46 7.005 0.887 1.389 r47 1.129 2.014 r48 1.149 2.320 r49 1.137 2.406 r50 5.017 0.973