******************************************************************** ******************************************************************** This chapter is from the book: Castillo, E., Conejo A.J., Pedregal, P., Garc\'{\i}a, R. and Alguacil, N. (2002). Building and Solving Mathematical Programming Models in Engineering and Science, Pure and Applied Mathematics Series, Wiley, New York. Copyright © 2002 by John Wiley and Sons, Inc. This material is used by permission of John Wiley and Sons, Inc. ******************************************************************** ******************************************************************** $title THE TRANSPORTATION PROBLEM ** First, indices are declared and defined. ** Index I is used to refer to the three origins. ** Index J is used to refer to the three destinations. ** Note how the symbol `*' is used to list sets members in a compact way. SETS I index of shipping origins /I1*I3/ J index of shipping destinations /J1*J3/; ** Vectors of data (U(I) and V(J)) are defined as parameters ** Data are assigned to vector elements PARAMETERS U(I) the amount of good to be shipped from origin I /I1 2 I2 3 I3 4/ V(J) the amount of good to be received in destination J /J1 5 J2 2 J3 2/; ** The C(I,J) data matrix is defined as a table ** Data are assigned to matrix elements TABLE C(I,J) cost of sending a unit from origin I to destination J J1 J2 J3 I1 1 2 3 I2 2 1 2 I3 3 2 1; ** The optimization variables are declared. ** First, the objective function variable (z) is declared. ** Next, the remaining variables with controlling indices are declared. VARIABLES z objective function variable x(I,J) the amount of product to be shipped from origin I to destination J; ** The types of variables are given in the following sentence. ** In the transportation problem, all the variables are positive except ** the objective function variable. POSITIVE VARIABLE x(I,J); ** The objective function equation is declared. ** The remaining six equations are declared, in a compact way, ** as two index-dependent equations. EQUATIONS COST objective function equation SHIP(I) shipping equation RECEIVE(J) receiving equation; ** The following sentences formulate the above declared equations. ** All the constraints are equality constraints (=E=). ** The first one defines the objective function equation as a summation. ** The second equation group represents three ** different single equations depending on index I. ** The left-hand-side is a sum in J of the unknowns x(I,J), and ** the right-hand-side is a previously defined vector of data. ** Similarly to SHIP constraints, the RECEIVE constraints are defined. COST .. z =E= SUM((I,J), C(I,J)*x(I,J)) ; SHIP(I) .. SUM(J, x(I,J)) =E= U(I) ; RECEIVE(J) .. SUM(I, x(I,J)) =E= V(J) ; ** The next sentence names the model and list its constraints. MODEL transport /COST,SHIP,RECEIVE/; ** The next sentence directs GAMS to solve the transportation model using ** a linear programming solver lp to minimize the objective function. SOLVE transport USING lp MINIMIZING z; ******************************************************************** ******************************************************************** $title PRODUCTION_SCHEDULING PROBLEM ** The index T is declared. It has 5 elements which are defined ** using the symbol `*'. SET T The month index /0*4/; ** Once the set T has been defined, the data vector Y(T) is ** declared and all its elements are assigned except ** the first one (element `0'), which, by default, is set to 0. PARAMETER Y(T) demand in month T /1 2 2 3 3 6 4 1/ A(T) B(T) C(T); ** A unit value is assigned to all elements of variables A, B and C ** (using index T). A(T)=1; B(T)=1; C(T)=1; ** The optimization variables are declared. ** First, the objective function variable (z) is declared. ** This variable does not depend on any ** index because it is a single-valued variable. ** The remaining two variables depend on the index T, so, they ** are declared as functions of it. VARIABLES z objective function variable x(T) number of units produced in month T s(T) number of units in storage in month T; ** With the exception of the objective function variable, ** which, by default, is not restricted in sign, the other ** two variables in this problem are defined as positive. POSITIVE VARIABLES x(T),s(T); ** The initial value for variable s(T) is set in the next sentence. ** In month `0', the variable s(T) takes on value 2. s.fx(`0')=2; ** The objective function, and five constraints, INOUT equations, are declared. EQUATIONS COST objective function INOUT(T) input and output balance; ** Once the equations have been declared, they need to be defined. ** The first equation is the objective function. ** This equation has a conditional ($) sum. ** Only the last four members of the ordered index T are ** considered in the sum when using `$(ord(T) gt 1)'. ** Next, we define the four INOUT equations. ** These equations were declared as functions of index T. ** Since the index T-1 appears in the term s(T-1) in the right hand side ** of equation, it is necessary to begin with the second member of the set T. ** To state that equations INOUT do not model the `month 0', ** the `$(ord(T) gt 1)' conditional statement is used after its name. COST..z=E= SUM(T$(ord(T) gt 1), A(T)*Y(T)-B(T)*x(T)-C(T)*s(T)); INOUT(T)$(ord(T) gt 1)..s(T)=E= s(T-1)+x(T)-Y(T); ** The next sentence names the model and states that all the previously ** declared and defined constraints must be included in the optimization ** problem. MODEL scheduling /ALL/; ** The next sentence directs GAMS to solve the scheduling model using linear ** programming and maximizing the objective function variable z. SOLVE scheduling USING lp MAXIMIZING z; ******************************************************************** ******************************************************************** $title DIET PROBLEM ** The sets of indices are defined: ** Index I is used to refer to the four nutrients. ** Index J is used to refer to the five foods. SET I set of nutrients /DN,DP,Ca,Ph/ J set of foods /Corn,Oats,Milo,Bran,Linseed/; ** Vectors of data are defined as parameters. ** Data are assigned to vector elements. PARAMETERS B(I) the minimum required amount of nutrient I /DN 74.2 DP 14.7 Ca 0.14 Ph 0.55/ C(J) cost of one unit of food J /Corn 1 Oats 0.5 Milo 2 Bran 1.2 Linseed 3/; ** The matrix of data is defined as a table. ** Data are assigned to matrix elements. TABLE A(I,J) the amount of nutrient I in one unit of food J Corn Oats Milo Bran Linseed DN 78.6 70.1 80.1 67.2 77.0 DP 6.5 9.4 8.8 13.7 30.4 Ca 0.02 0.09 0.03 0.14 0.41 Ph 0.27 0.34 0.30 1.29 0.86; ** The next step declares the optimization variables. ** First, the objective function variable is declared. ** Then, the remaining variables with their associated indices are declared. VARIABLES z objective function variable x(J) the amount of food J to be purchased ; ** The types of variables are established in the next sentence. ** In the diet problem, all the variables except ** the objective-function variable are positive. POSITIVE VARIABLE x(J); ** The problem constraints are declared. ** First, the objective function constraint (COST). ** Next, the four remaining constraints (NUTFOOD) are ** declared as functions of index I. EQUATIONS COST objective function NUTFOOD(I) nutrients and food relation; ** The objective function is an equality constraint (=E=). ** The NUTFOOD constraints are inequality constraints (=G=). COST .. z =E= SUM(J, C(J)*x(J)); NUTFOOD(I) .. SUM(J, A(I,J)*x(J)) =G= B(I); ** The following sentences define the diet model with all the ** declared constraints and direct GAMS to solve the problem. MODEL diet /ALL/; SOLVE diet USING lp MINIMIZING z; ******************************************************************** ******************************************************************** $title NETWORK FLOW PROBLEM ** First, sets are declared: ** Set I has four elements. ** Subset CONEX is defined as a subset of set I. ** The subset CONEX establishes the valid connections between nodes I. SET I set of nodes in the network /I1*I4/ CONEX(I,I) set of node connections /I1.I2,I1.I3,I1.I4,I2.I4,I3.I4/; ** The set of nodes I must be duplicated to refer to ** its different elements in the same constraint. ALIAS(I,J) ** Vectors of data are defined as parameters. ** Data are assigned to vector elements. ** FMAX(I,J) is a data matrix, declared as a parameter, where all ** its elements have the same value (4). This is a compact way ** to declare a matrix (instead of using a TABLE). PARAMETERS F(I) the input or output flow at node I /I1 7 I2 -4 I3 -1 I4 -2/ FMAX(I,J) maximum flow capacity of conduction going from I to J; FMAX(I,J)=4; ** Optimization variables are declared. VARIABLES z objective function variable x(I,J) the flow going from node I to node J; ** Limits are stated on variables using previously defined data. x.lo(I,J)=-FMAX(I,J); x.up(I,J)=FMAX(I,J); ** Constraints are declared. EQUATIONS COST objective function BALANCE(I) conservation of flow conditions; ** The objective function is a function of flows between connected nodes. ** This requirement is stated using the $CONEX(I,J) condition. ** The four BALANCE equations only consider the flow between ** connected nodes and again the $CONEX(I,J) condition is necessary. COST .. z =E= SUM(CONEX(I,J),x(I,J)) ; BALANCE(I) .. SUM(J$CONEX(I,J),x(I,J))-SUM(J$CONEX(J,I),x(J,I)) =E= F(I) ; ** The next two sentences define the netflow model, considering all the above ** constraints, and direct GAMS to solve the problem using the lp solver. MODEL netflow /ALL/; SOLVE netflow USING lp MINIMIZING z; ******************************************************************** ******************************************************************** $title WATER SUPPLY NETWORK (linear) ** Sets are declared in first place: ** Set I has four elements. ** CONEX is defined as a subset of set I. ** The subset CONEX establishes the valid connections between nodes of I. SET I set of nodes in the network /I1*I4/ CONEX(I,I) set of connections between nodes /I1.I2,I1.I3,I1.I4,I2.I4,I3.I4/; ** The set of nodes I should be duplicated to refer to its ** different elements within the same constraint. ALIAS(I,J) ** Vectors of data are defined as parameters. ** Data are assigned to each vector element. ** FMAX(I,J) is a data matrix declared as a parameter. ** All its elements have the same value (8). ** This is a compact way to declare a matrix (instead of using a TABLE). PARAMETERS X(I,J) total flow going from node I to node J F(I) the input or output flow at node I /I1 20 I2 -3 I3 -10 I4 -7/ FMAX(I,J) maximum flow capacity of conduction going from I to J; FMAX(I,J)=8; ** Optimization variables are declared. VARIABLES z objective function variable xp(I,J) positive part of x xn(I,J) negative part of x; ** The flow from I to J is a positive variable. POSITIVE VARIABLES xp(I,J),xn(I,J); ** Constraints are declared. EQUATIONS COST objective function BALANCE(I) conservation of flow conditions UPLIMIT(I,J) upper limit of flow going from node I to J LOLIMIT(I,J) lower limit of flow going from node I to J; ** The objective function is the sum of the absolute values of the flows ** between all connected nodes. This requirement is stated using the ** $CONEX(I,J) condition. ** The four BALANCE equations only consider the flow between ** two connected nodes and again the $CONEX(I,J) condition is necessary. ** The flow going from node I to node J is limited by the maximum flow ** capacity of conduction using the UPLIMIT(I,J) and LOLIMIT(I,J) constraints. COST .. z =E= SUM((I,J)$CONEX(I,J),xp(I,J)-xn(I,J)) ; BALANCE(I) .. SUM(J$CONEX(I,J),xp(I,J))-SUM(J$CONEX(J,I),xp(J,I))- SUM(J$CONEX(I,J),xn(I,J))+SUM(J$CONEX(J,I),xn(J,I)) =E= F(I) ; UPLIMIT(I,J)$(CONEX(I,J)) .. xp(I,J) - xn(I,J) =l= FMAX(I,J); LOLIMIT(I,J)$(CONEX(I,J)) .. xp(I,J) - xn(I,J) =g= -FMAX(I,J); ** The next two sentences define the model with all the above constraints, ** and direct GAMS to solve the problem using a linear programming solver. MODEL wsn /ALL/; SOLVE wsn USING lp MINIMIZING z; ** The next sentence assigns to x(I,J) the difference between ** its positive part and its negative part. This is used to ** model in a linear way, the absolute value function. x(I,J)=xp.L(I,J)-xn.L(I,J); ** To print in the listing file (*.lst) the value of ** x(I,J) the next sentence is used. DISPLAY x; ******************************************************************** ******************************************************************** $title THE PORTFOLIO PROBLEM ** Set I is declared with three elements. SET I set of stocks /A1,A2,A3/; ** The set of stocks I should be duplicated in ** order to refer to its different elements in ** the same constraint. ALIAS(I,J); SCALARS r percentage /0.25/ s percentage /0.03/; TABLE data(I,*) B V D W * ($) ($) ($) A1 75 20 0 18 A2 100 20 3 23 A3 35 100 5 102; VARIABLES z objective function variable x(I) number of shares of stock I; POSITIVE VARIABLE x(I); x.lo(I)=-data(I,'B'); EQUATIONS COST objective function NOCHANGE no change in the current value INFLATION future value must be 3\% greater than the current value BALANCE(I) to avoid excessive reliance on a single stock; COST .. z =E= SUM(I,data(I,'D')*(x(I)+data(I,'B'))) ; NOCHANGE .. SUM(I,data(I,'V')*x(I)) =E= 0; INFLATION .. SUM(I,data(I,'W')*(x(I)+data(I,'B')))=G= (1+s)*SUM(I,data(I,'V')*data(I,'B')); BALANCE(I).. r*SUM(J,data(J,'V')*(x(J)+data(J,'B')))=L= data(I,'V')*(x(I)+data(I,'B')); MODEL portfolio /ALL/; SOLVE portfolio USING lp MAXIMIZING z; ******************************************************************** ******************************************************************** $title SCAFFOLDING PROBLEM (LINEAR) ** The indexes are defined in first place: ** Index B is used to refer to the three beams. ** Index R is used to refer to the six ropes. ** Index L is used to refer to the two loads. SET B set of beams /B1*B3/ R set of ropes /RA,RB,RC,RD,RE,RF/ L set of loads /L1,L2/ UPP(B,R) / B1.(RA,RB) B2.(RC,RD) B3.(RE,RF) / DOWN(B,R) / B1.(RC,RD) B2.(RF) / LOAD(B,L) / B1.L1 B3.L2 /; ** Vectors of data are defined as parameters. ** Data are assigned to vector elements. PARAMETER LMAX(R) maximum load for ropes / (RA,RB) 300 (RC,RD) 200 (RE,RF) 100 /; PARAMETER DL(L) coordinates of load L / L1 7 L2 5 /; PARAMETER DR(R) coordinates of rope R / RA 2 RB 12 RC 4 RD 12 RE 0 RF 10 /; VARIABLES z objective function variable x(L) the applied load t(R) tension on rope R ; t.up(R) = LMAX(R); ** Problem constraints are declared. EQUATIONS COST objective function FORCES(B) force equilibrium equation MOMENT(B) moment equilibrium equation; COST .. z =E= SUM(L, x(L)) ; FORCES(B).. SUM(R$UPP(B,R),t(R))=E= SUM(L$LOAD(B,L),x(L))+ SUM(R$DOWN(B,R),t(R)); MOMENT(B).. SUM(R$UPP(B,R),DR(R)*t(R))=E=SUM(L$LOAD(B,L), DL(L)*x(L))+SUM(R$DOWN(B,R),DR(R)*t(R)); MODEL scaffold /COST,FORCES,MOMENT/; SOLVE scaffold USING lp MAXIMIZING z; ******************************************************************** ******************************************************************** $title THE ECONOMIC DISPATCH PROBLEM ** The sets G and N are defined. ** Next, the set MAP is defined as a subset of sets G and N. ** The subset MAP establishes the valid pairs of G and N elements. SETS G index of generators /G1*G2/ N index of buses /N1*N3/ MAP(G,N) associates generators with buses /G1.N1,G2.N2/; ** The data is provided below. The first table contains the columns of data ** for every generator. The names of these columns have not been previously ** declared as elements of a set, so they are referred to using the symbol `*'. ** The same occurs in the second table. TABLE GDATA(G,*) generator input data PMIN PMAX COST * (kW) (kW) (E/kWh) G1 0.15 0.6 6 G2 0.10 0.4 7; TABLE LDATA(N,N,*) line input data SUS LIMIT * (S) (kW) N1.N2 2.5 0.3 N1.N3 3.5 0.5 N2.N3 3.0 0.4; ** To state that only bus 3 has an electric load, the ** parameter below is declared and defined. PARAMETER LOAD(N) load at bus N / N3 0.85 / ** The optimization variables are declared. VARIABLES z objective function variable p(G) output power for generator G d(N) angle at bus N; ** Bounds on variables, using previously defined data, are given below. p.lo(G)=GDATA(G,'PMIN'); p.up(G)=GDATA(G,'PMAX'); ** Bus 3 is considered as the reference bus, therefore ** its corresponding angle is fixed to zero. d.fx('N3')=0; ** Different constraints are declared. EQUATIONS COST objective function MAXPOW(N,N) maximum line power limit MŠNPOW(N,N) minimum line power limit LOADBAL(N) load balance equation; ** The set of buses N should be `duplicated' to refer ** to different elements in the same constraint. ALIAS(N,NP); ** The definition of equations begins below. ** The objective function equation is defined as a summation. ** The `cost' index value is used to refer to the column `cost' of the ** GDATA table. ** Next, the inequality constraints MAXPOW and MINPOW are defined. ** Every equation relates two different buses, therefoÚe they depend on ** two sets, ** through N and NP that represent the same set with different names. ** The last equation group LOADBAL is defined per bus. ** To ensure that a generator G is connected to bus N the ** sum is conditioned by $MAP(G,N). COST.. z =e= SUM(G,GDATA(G,'COST')*p(G)); MAXPOW(N,NP).. LDATA(N,NP,'SUS')*(d(N)-d(NP))=l= LDATA(N,NP,'LIMIT'); MINPOW(N,NP).. LDATA(N,NP,'SUS')*(d(N)-d(NP))=g=-LDATA(N,NP,'LIMIT'); LOADBAL(N).. SUM(G$MAP(G,N),p(G))+SUM(NP,LDATA(N,NP,'SU²')*(d(N)-d(NP))+ LDATA(NP,N,'SUS')*(d(N)-d(NP)))=e=LOAD(N); ** The next sentences define the economic dispatch model ** with all the declared constraints and direct GAMS to solve the problem. MODEL ed /COST,MAXPOW,MINPOW,LOADBAL/; SOLVE ed USING lp MINIMIZING z; ******************************************************************** ******************************************************************** $title 0-1 KNAPSACK PROBLEM. ** This knapsack problem is used to determine the maximum loading of a freighter. ** The main characteristic is that A(j)=C(j) for this problem. ** This fact can be taken into account to shorten the GAMS file, ** but the formulation is general for any knapsack problem. ** The next sentence change the termination criterion ** based on the relative gap of the current solution. ** Note that integer programs work with real programs ** and we need to indicate the program what error is ** admitted to consider a real number as an integer number ** If we use a standard value then GAMS stops before ** an optimal solution is achieved. OPTION OPTCR=1e-10; SET J set of containers /c1*c10/; ** Vectors of data A and C are defined as parameters. PARAMETERS C(J) benefit of container J /c1 100 c2 155 c3 50 c4 112 c5 70 c6 80 c7 60 c8 118 c9 110 c10 55/ A(J) weight of container J; A(J) = C(J); SCALAR B maximum capacity of the freighter /700/; ** Optimization variables are declared. VARIABLES z objective function variable x(J) binary choice; ** x(J) has the value 1 if the container J is shipped, and 0 otherwise. BINARY VARIABLE x; ** Constraints are declared. EQUATIONS COST objective function CAPA is the loading of the freighter; ** The objective function is the sum of the weights ** of all the containers that are shipped. ** The CAPA equations consider that the loading cannot exceed ** the maximum capacity of the freighter. COST .. z=E= SUM(J,C(J)*x(J)); CAPA .. SUM(J,A(J)* x(J)) =L= B; ** The next two sentences define the model, ** considering all the above constraints, and direct GAMS to solve ** the problem using an integer linear programming solver. MODEL knapsack /ALL/; SOLVE knapsack USING mip MAXIMIZING z; ******************************************************************** ******************************************************************** $Title Symptoms SETS D set of diseases /D1*D5/ S set of symptoms /S1*S8/; ALIAS(D,DP); SCALAR A discrepancy level /1/; TABLE C(D,S) S1 S2 S3 S4 S5 S6 S7 S8 D1 2 3 1 1 1 2 1 2 D2 1 1 1 1 3 1 2 1 D3 3 4 2 3 2 2 3 2 D4 2 2 2 2 2 1 2 3 D5 1 1 1 2 1 1 1 2; PARAMETER DD(D,DP,S) discrepancy measure; loop((D,DP,S), if((C(D,S) ne C(DP,S)), DD(D,DP,S)=1; else DD(D,DP,S)=0; ); ); BINARY VARIABLE x(S); FREE VARIABLE z; EQUATIONS SELECT number of selected symptoms Suff(D,DP) symptoms are sufficient; SELECT.. z =e= SUM(S,x(S)); Suff(D,DP)$(ord(D) ne ord(DP)).. SUM(S,x(S)*DD(D,DP,S)) =g= A; MODEL symptoms /ALL/; SOLVE symptoms USING mip MINIMIZING z; ******************************************************************** ******************************************************************** $title ACADEMY SETS I number of actual members /1*20/ J number of candidates /1*8/ DIN(J) S the number of different scores that can be assigned /1*4/; ALIAS(J,J1); PARAMETER P(S) the $s$-th score /1 10 2 8 3 3 4 1/; TABLE N(I,J) score assigned to candidate J by actual member I 1 2 3 4 5 6 7 8 ******************************************************************** 1 3 10 8 1 2 1 10 8 3 3 1 3 10 8 4 3 10 8 1 5 3 8 10 1 6 1 10 8 3 7 10 8 3 1 8 3 10 1 8 9 8 3 10 1 10 3 10 1 8 11 8 1 10 3 12 10 13 10 8 14 10 1 3 8 15 3 10 8 1 16 10 1 8 3 17 1 3 10 8 18 1 3 8 10 19 1 10 3 8 20 8 1 10 3 ; SCALARS zmin,zmax; PARAMETER C(J) total score obtained by candidate J; C(J)=sum(I,N(I,J)); VARIABLES z function to be optimized ; BINARY VARIABLE x(I,J,S) if member I assigns score P(S) to candidate J takes on value 1. Otherwise 0.; EQUATIONS OBJ function to be optimized L1(I,J) Each member can assign at the most one score to each candidate L2(I,S) Each member I can assign score S to at most one candidate TOTALSCORE(J) totalscore given; ** The objective function equation depends on the candidate. ** For a specific candidate, the objective function to be ** optimized (minimized and maximized) is the number of members ** that assigns a determined score to this candidate. ** The dynamic set DIN allows to solve ** the same problem for different candidates. ** This set is updated at every iteration ** in the loop of candidates. ** The constraints are identical regardless ** of the candidate. OBJ(J)$DIN(J)..z=e=sum(I,sum(S,x(I,J,S))); L1(I,J)..sum(S,x(I,J,S))=l=1; L2(I,S)..sum(J,x(I,J,S))=l=1; TOTALSCORE(J)..sum(I,sum(S,P(S)*x(I,J,S)))=e=C(J); ** The model includes all the constraints. MODEL Academy /ALL/; ** The output file is open. file aux /academy.out/; put aux; ** First of all, the empty set is assigned to the dynamic set. DIN(J)=NO; loop(J1, ** Only the candidate specified by the loop controlling index is considered. DIN(J1)=YES; ** The problem is minimized for candidate J1. Solve Academy using mip Minimizing z; ** The solution is stored in scalar zmin. zmin=z.l; ** The problem is maximized for candidate J1. Solve Academy using mip Maximizing z; ** The solution is stored in scalar zmax. zmax=z.l; ** Both solutions are written in 'academy.out' file. put "J=",J1.tl:3," zmin= ",zmin:3:0," zmax= ",zmax:3:0/; ** Once the problem is optimized in both direction, the empty ** set is assigned to the dynamic set DIN(J1)=NO; ); ******************************************************************** ******************************************************************** $title CLASS TIMETABLE SETS C classrooms /c1*c3/ H hours /h1*h5/ S subjects /s1*s8/ I instructors /i1,i2/ B blocks /b1,b2/ SI(S,I) maps subjects and instructors /(s1,s2,s8).i1,(s3*s7).i2/ SB(S,B) maps subjects and blocks /(s1*s4).b1,(s5*s8).b2/; VARIABLE z; BINARY VARIABLE v(S,C,H); EQUATIONS cost compact the timetable const1(I) every instructor teaches all his (her) subjects const2(H,I) every instructor teaches at most 1 subject every hour const3(S) every subject is taught once const4(C,H) in every classroom-hour pair at most 1 subject is taught const5(H,B) at every hour at most 1 subject of any academic block is taught; cost.. SUM((S,C,H),(ord(C)+ord(H))*v(S,C,H)) =e= z ; const1(I).. SUM((S,C,H)$SI(S,I),v(S,C,H)) =e= SUM(S$SI(S,I),1); const2(H,I).. SUM((S,C)$SI(S,I),v(S,C,H)) =l= 1; const3(S).. SUM((C,H),v(S,C,H)) =e= 1; const4(C,H).. SUM(S,v(S,C,H)) =l= 1; const5(H,B).. SUM((S,C)$SB(S,B),v(S,C,H)) =l= 1; model timetable /all/; solve timetable using mip minimizing z; DISPLAY v.L; ******************************************************************** ******************************************************************** $Title MODEL OF DISCRETE LOCATION ** The next sentence change the termination criterion ** based on the relative gap of the current solution. ** If we use a standard value then GAMS stops before ** an optimal solution is achieved. OPTION OPTCR=1e-10; ** The indexes are defined in first place. ** Index I is used to refer to the seven cities. ** Index J is used to refer the six available locations ** for the industrial plants. SET I index of cities /C1*C7/ J index of locations /L1*L6/; PARAMETERS B(I) demand of a certain good in the city I /C1 1.5 C2 2.0 C3 3.0 C4 4.0 C5 2.5 C6 1.0 C7 2.0/ F(J) amortization cost of an industrial plant at location J U(J) maximum production capacity of an industrial plant placed at location J; F(J) = 10; U(J) = 6; ** The data about the benefit is shown below. The table contains ** the benefit of making a good at the plant located at J, and ** selling it in city I. TABLE C(J,I) benefits according to different locations C1 C2 C3 C4 C5 C6 C7 L1 4.0 4.5 2.5 0.5 1.0 0.5 -3.5 L2 4.0 4.5 2.5 4.2 3.5 1.5 -0.5 L3 3.5 5.0 4.0 3.5 4.5 1.5 0.0 L4 1.3 3.0 5.0 3.3 5.5 1.8 1.3 L5 0.5 1.0 1.5 5.0 4.0 5.5 3.0 L6 -1.0 0.0 1.5 3.3 4.0 4.5 2.0 ; ** The optimization variables are declared. VARIABLES z objective function variable x(I,J) the amount of good that is made at J and selling at I y(J) location variable. It is equal to 1 if the plant is open at J, and 0 otherwise. ** This is a mixed integer programming problem. The variables y(J) ** are binary variables and x(I,J) are positive variables. POSITIVE VARIABLE x; BINARY VARIABLE y; ** Different constraints are declared. EQUATIONS COST objective function SD(I) satisfying demand of city I CAPA(J) capacity of production of the plant I; ** The equation definition is stated below. ** The objective function is the sum of the benefit minus the investment costs. ** The constraint SD(I) is the satisfaction of the demand of the city I. ** The constraint CAPA(J) imposes that the production at the plant J cannot ** exceed its capacity. COST .. z=e= SUM((I,J),C(J,I)*x(I,J))-SUM(J,F(J)*y(J)); SD(I) .. SUM(J,x(I,J)) =e= B(I); CAPA(J).. SUM(I,x(I,J)) =l= U(J)*y(J); ** The next two sentences define the model, considering all the above ** constraints, and direct GAMS to solve the problem using an integer ** linear programming solver. MODEL loc /all/; SOLVE loc USING mip MAXIMIZING z; DISPLAY x.l; ******************************************************************** ******************************************************************** $title THE UNIT COMMITMENT PROBLEM ** Sets are declared first. SETS K index of periods of time /1*4/ J index of generators /1*3/ ** The data is given below. The first table contains different columns of data ** for every generator. The column names have not been previously declared ** as elements of a set, so they should be referenced using the symbol `*'. ** The same applies to the second table. TABLE GDATA(J,*) generator input data PMIN PMAX T S A B C E * (kW) (kW) (kW/h) (kW/h) (E) (E) (E) (E/kWh) 1 50 350 300 200 5 20 0.5 0.100 2 80 200 150 100 7 18 0.3 0.125 3 40 140 100 100 6 5 1.0 0.150; TABLE PDATA(K,*) data per period D R * (kW) (kW) 2 150 15 3 500 50 4 400 40; ** The optimization variables are declared. VARIABLES z objective function variable p(J,K) output power of generator j at period k v(J,K) is equal to 1 if generator j is committed in period k y(J,K) is equal to 1 if generator j is started-up at the beginning of period k s(J,K) is equal to 1 if generator j is shut-down in period k; ** The power is a positive variable. POSITIVE VARIABLES p(J,K); ** Status decisions are modeled using binary variables. BINARY VARIABLES v(J,K),y(J,K),s(J,K); ** Initial values are stated for some variables. v.fx(J,'1')=0; p.fx(J,'1')=0; ** Constraints are declared. EQUATIONS COST objective function PMAXLIM(J,K) maximum output power equation PMINLIM(J,K) minimum output power equation LOAD(K) load balance equation RESERVE(K) spinning reserve equation LOGIC(J,K) start-up shut-down and running logic RAMPUP(J,K) maximum up ramp rate limit RAMPDOWN(J,K) maximum down ramp rate limit; ** The objective function is an equality equation. ** The remaining constraints are defined for all periods K, ** except for the initial one. ** To model this exception the $(ord(K) GT 1) condition is included. COST.. z =e= SUM((K,J), GDATA(J,'A')*v(J,K)+GDATA(J,'B')*y(J,K)+ GDATA(J,'C')*s(J,K)+GDATA(J,'D')*p(J,K)); PMAXLIM(J,K)$(ord(K) GT 1).. p(J,K)=l=GDATA(J,'PMAX')*v(J,K); PMINLIM(J,K)$(ord(K) GT 1).. p(J,K)=g=GDATA(J,'PMIN')*v(J,K); LOAD(K)$(ord(K) GT 1).. SUM(J,p(J,K))=e=PDATA(K,'D'); RESERVE(K)$(ord(K) GT 1).. SUM(J,GDATA(J,'PMAX')*v(J,K))=g=PDATA(K,'D') +PDATA(K,'R'); LOGIC(J,K)$(ord(K) GT 1).. y(J,K)-s(J,K)=e=v(J,K)-v(J,K-1); RAMPUP(J,K)$(ord(K) GT 1).. p(J,K)-p(J,K-1)=l=GDATA(J,'S'); RAMPDOWN(J,K)$(ord(K) GT 1)..p(J,K-1)-p(J,K)=l=GDATA(J,'T'); ** The next sentences define the unit commitment model ** with all the declared constraints, and direct ** GAMS to solve the resulting problem using a mixed-integer solver. MODEL uc /ALL/; SOLVE uc USING mip MINIMIZING z; ******************************************************************** ******************************************************************** $title Postal package POSITIVE VARIABLES x,y,z; FREE VARIABLE obj; EQUATIONS VOL objective function DIM constraint on the dimension and weight; VOL.. obj =e= x*y*z; DIM.. z+2*x+2*y =l= 108; MODEL package /ALL/; x.l=1; y.l=1; z.l=1; SOLVE package USING nlp MAXIMIZING obj; ******************************************************************** ******************************************************************** $title Bulb SCALAR k proportionality constant /3/ r radius /30/; POSITIVE VARIABLE h; FREE VARIABLE z; EQUATION INTENSITY objective function; INTENSITY.. z =e= k*(SQRT(h)/((SQR(h)+SQR(r))**0.75)); MODEL bulb /all/; h.l=10; SOLVE bulb USING nlp MAXIMIZING z; ******************************************************************** ******************************************************************** $title Surface POSITIVE VARIABLES x,y,z; FREE VARIABLE obj; EQUATIONS DIST objective function ON constraint on the points; DIST.. obj =e= SQRT(SQR(x)+SQR(y)+SQR(z)); ON.. 1 =e= x*y*x; MODEL surface /all/; x.l=100; y.l=100; z.l=100; SOLVE surface USING nlp MINIMIZING obj; ******************************************************************** ******************************************************************** §title sand SCALAR k proportionality constant /1.5/ cu cubic units of sand /50/; POSITIVE VARIABLES x,y,z; FREE VARIABLE obj; EQUATIONS COST objective function; COST.. obj =e= k*(3*(2*x*y)+2*(2*x*z)+2*y*z)+2*(cu/(x*y*z)); MODEL sand /COST/; x.l=0.5; y.l=0.5; z.l=0.5; SOLVE sand USING nlp MINIMIZING obj; ******************************************************************** ******************************************************************** $title Cantilever beam SCALARS L length /1/ E YouÓg modulus /1e6/ F load at the tip /100/ S maximum allowable deflection /1 / gamma unit weight /100/; POSITIVE VARIABLES x,y; FREE VARIABLE obj; EQUATIONS WEIGHT objective function SMT constraint given by the strength of materials theory; WEIGHT.. obj =e= gamma*L*x*y; SMT.. (4*F*POWER(L,3))/(E*x*POWER(y,3))=l= S; MODEL beam /ALL/; x.lo=0.001; y.lo=0.001; x.l=1; y.l=1; SOLVE beam USING nlp MINIMIZING obj; ******************************************************************* ******************************************************************** $title Two-bar truss SCALARS Gamma unit weight of the bar material /1e2/ E Young modulus /1e6/ F load at the the fixed joint /15e3/ S0 maximum admissible stress /6e4/ D0 maximum admissible displacement of joint 3 /1e-1 / h truss height / 1/; PARAMETER K constant; K= F/(2*SQRT(2)*h); POSITIVE VARIABLES x,z; FREE VARIABLE obj; EQUATIONS W objective function D displacement of the joint 3 S1 stress at joint 1 S2 stress at joint 2; W.. obj =e= 2*Gamma*SQRT(SQR(x)+SQR(h))*z; D.. K*( (SQR(h)+SQR(x))**(3/2)) * SQRT(h**4+x**4) /(E*h*SQR(x)*z) =l= D0; S1.. K*((x+h)*SQRT(SQR(x)+SQR(h))) / (x*z) =l= S0; S2.. K*((h-x)*SQRT(SQR(x)+SQR(h))) / (x*z)=l= S0; MODEL truss /ALL/; x.lo=0.05; z.lo=0.001; x.l=100; z.l=100; SOLVE truss USING nlp MINIMIZING obj; ******************************************************************** ******************************************************************** $title Column SCALARS M mass supported by the column /100/ H height of the column /10/ D unit density of the material /100/ E Young's modulus of the material /1e6/ S maximum admissible stress /6e4/ G acceleration of gravity /9.8/ Pi /3.141592/; POSITIVE VARIABLES x,y; FREE VARIABLE z; EQUATIONS W objective function R1 compression stress constraint R2 buckling stress constraint; W.. z =e= D*H*x*y-SQRT((E*x*(y**3))/(4*(H**3)*(M+(33/140)*D*H*x*y))); R1.. (M*G)/(x*y) =l= S; R2.. (M*G)/(x*y) =l= (SQR(Pi)*E*SQR(y))/(48*SQR(H)); MODEL column /ALL/; x.lo=0.001; y.lo=0.001; x.l=10; y.lo=10; SOLVE column USING nlp MINIMIZING z; ******************************************************************** ******************************************************************** $title SCAFFOLDING (NON-LINEAR) ** The indexes are defined in first place. ** Index B is used to refer to the three beams. ** Index R is used to refer to the six ropes. ** Index L is used to refer to the two loads. SET B set of beams /B1*B3/ R set of ropes /RA,RB,RC,RD,RE,RF/ L set of loads /L1,L2/ UPP(B,R) /B1.(RA,RB) B2.(RC,RD) B3.(RE,RF)/ DOWN(B,R) /B1.(RC,RD) B2.(RF)/ LOAD(B,L) /B1.L1 B3.L2/; ** Vectors of data are defined as parameters. ** Data are assigned to each vector element. PARAMETER LMAX(R) maximum load for ropes / (RA,RB) 300 (RC,RD) 200 (RE,RF) 100/; PARAMETER dr(R) coordinates of rope R / RA 2 RB 12 RC 4 RD 12 RE 0 RF 10/; ** The next step is to declare the optimization variables. ** Firstly the objective function variable is declared. ** Then the remaining variables with controlling indexes ** are declared. VARIABLES z objective function variable x(L) the amount of food J to be purchased T(R) tension on rope R d(L) distance from the left end-points of beams; ** The bounds on variables are established in the next sentences. POSITIVE VARIABLE d; T.UP(R) = LMAX(R); EQUATIONS COST objective function FORCES(B) force equilibrium equation MOMENT(B) moment equilibrium equation; COST .. z =E= SUM(L, x(L)) ; FORCES(B).. SUM(R$UPP(B,R),T(R))=E= SUM(L$LOAD(B,L),x(L))+ SUM(R$DOWN(B,R),T(R)); MOMENT(B).. SUM(R$UPP(B,R),dr(R)*T(R))=E=SUM(L$LOAD(B,L), d(L)*x(L))+SUM(R$DOWN(B,R),dr(R)*T(R)); MODEL ropebeam /COST,FORCES,MOMENT/; SOLVE ropebeam USING nlp MAXIMIZING z; ******************************************************************** ******************************************************************** $title THE POWER CIRCUIT ESTIMATION PROBLEM ** The allowable buses are defined in the next sentence. SETS N index of buses /N1*N2/ ** The data is provided below. The first table contains several columns of data ** for every line. The names of these columns have not been previously declared ** as elements of a set, so they should be referenced using the symbol `*'. ** The same applies to the second table. TABLE LINE(N,N,*) line input data Z PHI * (Ohm) (degrees) N1.N2 0.15 90; TABLE BUS(N,*) bus measures V P Q * (V) (W) (VAr) N1 1.07 0.83 0.73 N2 1.01 0.81 0.58; ** Constant PI is used to convert degrees to radians. SCALAR PI /3.1416/; ** Optimization variables are declared. VARIABLES z objective function variable V(N) voltage magnitude at bus N d(N) voltage angle at bus N; ** Bus 2 is considered as the reference bus, therefore ** its corresponding angle is fixed to zero. D.FX('N2')=0; ** The set of buses N should be duplicated to refer to ** different elements in the same constraint. ALIAS(N,NP); ** The next sentence converts degrees to radians. LINE(N,NP,'PHI')=LINE(N,NP,'PHI')*PI/180; ** Data matrices Z and PHI are defined as symmetric using the ** $(ORD(N) GT ORD(NP)) condition on the sets N and NP. LINE(N,NP,'Z')$(ORD(N) GT ORD(NP))=LINE(NP,N,'Z'); LINE(N,NP,'PHI')$(ORD(N) GT ORD(NP))=LINE(NP,N,'PHI'); ** The objective-function constraint is declared in the following lines. EQUATION ERROR objective function; ** The objective function is defined using nonlinear functions ** SQR, COS and SIN. The $(LINE(N,NP,'Z') NE 0) condition is used ** to check if a line exists between buses N and NP. ERROR.. z =e= SUM(N, SQR(V(N)-BUS(N,'V')) +SUM(NP$(LINE(N,NP,'Z') NE 0), SQR(((1/LINE(N,NP,'Z'))* (SQR(V(N))*COS(LINE(N,NP,'PHI'))-V(N)*V(NP)*COS(d(N)- d(NP)+LINE(N,NP,'PHI'))))-BUS(N,'P'))+ SQR(((1/LINE(N,NP,'Z'))* (SQR(V(N))*SIN(LINE(N,NP,'PHI'))-V(N)*V(NP)*SIN(d(N)- d(NP)+LINE(N,NP,'PHI'))))-BUS(N,'Q')))); ** The next sentences define the estimation model, and direct GAMS ** to solve the problem using a nonlinear solver. MODEL estimation /ERROR/; SOLVE estimation USING nlp MINIMIZING z; ******************************************************************** ******************************************************************** $title THE OPTIMAL POWER FLOW PROBLEM ** Sets G and N are defined. ** The set MAP is defined as a subset of the product of sets G and N. ** The subset MAP establishes the valid pairs of the elements of G and N. SETS G index of generators /G1*G2/ N index of buses /N1*N3/ MAP(G,N) associates generators with buses /G1.N1,G2.N2/; ** The data are provided below. The first table contains different columns ** of data for every generator. The names of these columns have not been ** previously declared as elements of a set, so they should be referenced ** using the symbol `*'. ** The same applies to the second table. TABLE GDATA(G,*) generator input data PMIN PMAX QMIN QMAX COST * (W) (W) (VAr) (VAr) (E/Wh) G1 0.0 3.0 -1.0 2.0 6 G2 0.0 3.0 -1.0 2.0 7; TABLE LINE(N,N,*) line input data Y PHI * (Ohm) (rad) N1.N1 22.97 -1.338 N2.N2 21.93 -1.347 N3.N3 20.65 -1.362 N1.N2 12.13 1.816 N1.N3 10.85 1.789 N2.N3 9.81 1.768; TABLE BUS(N,*) VMIN VMAX PL QL * (V) (V) (W) (VAr) N1 0.95 1.13 N2 0.95 1.10 N3 0.95 1.10 4.5 1.5; ** Constant PI is used to state limits on bus angles. SCALAR PI /3.1416/; ** Optimization variables are declared. VARIABLES z objective function variable p(G) active power output for generator G q(G) reactive power output for generator G v(N) voltage magnitude at bus N d(N) voltage angle at bus N; ** Limits are stated on variables using previously defined data. p.lo(G)=GDATA(G,'PMIN'); p.up(G)=GDATA(G,'PMAX'); q.lo(G)=GDATA(G,'QMIN'); q.up(G)=GDATA(G,'QMAX'); v.lo(N)=BUS(N,'VMIN'); v.up(N)=BUS(N,'VMAX'); d.lo(N)=-PI; d.up(N)=PI; ** Bus 3 is considered as the reference bus, therefore ** its corresponding angle is fixed to zero. d.fx('N3')=0; ** The set of buses N must be duplicated to refer to different elements ** in the same constraint. ALIAS(N,NP); ** Data matrices Y and PHI are defined as symmetric using ** the $(ORD(N) GT ORD(NP)) condition on the sets N and NP. LINE(N,NP,'Y')$(ORD(N) GT ORD(NP))=LINE(NP,N,'Y'); LINE(N,NP,'PHI')$(ORD(N) GT ORD(NP))=LINE(NP,N,'PHI'); ** Constraints are declared. EQUATIONS COST objective function PBAL(N) active power balance constraints QBAL(N) reactive power balance constraints; ** The objective function is an equality constraint. ** The remaining constraints are defined using different ** nonlinear functions. To check if generator ** G is connected to bus N the sums are conditioned by $MAP(G,N). COST.. z =e= SUM(G,GDATA(G,'COST')*p(G)); PBAL(N).. SUM(G$MAP(G,N),p(G))-BUS(N,'PL')=e=v(N)* SUM(NP,LINE(N,NP,'Y')*v(NP)*COS(d(N)-d(NP) -LINE(N,NP,'PHI'))); QBAL(N).. SUM(G$MAP(G,N),q(G))-BUS(N,'QL')=e= v(N)* SUM(NP,LINE(N,NP,'Y')*v(NP)*SIN(d(N)-d(NP)-LINE(N,NP,'PHI'))); ** The sentences below define the optimal power flow problem with all its ** constraints, and direct GAMS to solve the problem using the nlp solver. MODEL opf /COST,PBAL,QBAL/; SOLVE opf USING nlp MINIMIZING z; ******************************************************************** ******************************************************************** $title WATER SUPPLY NETWORK (nonlinear) ** The sets are declared first. Set I has four elements. ** Subset CONEX is defined as a subset of set I. ** The subset CONEX establishes the valid connections between nodes I. SET I set of nodes in the network /I1*I4/ CONEX(I,I) set of node connections /I1.I2,I1.I3,I1.I4,I2.I4,I3.I4/; ** The set of nodes I should be duplicated to refer to its ** different elements in the same constraint. ALIAS(I,J) ** Vectors of data are defined as parameters. ** Data are assigned to each vector element. ** FMAX(I,J) is a data matrix declared as a parameter where ** all its elements have the same value (8). ** This is a compact way to declare a matrix (instead of using a TABLE). PARAMETERS F(I) the input or output flow at node I /I1 20 I2 -3 I3 -10 I4 -7/ FMAX(I,J) maximum flow capacity of conduction going from I to J; FMAX(I,J)=8; ** Optimization variables are declared. VARIABLES z objective function variable x(I,J) the flow going from node I to node J ** The flow from I to J is a positive magnitude. POSITIVE VARIABLE x(I,J); ** Limits are stated on variables using previously defined data. x.lo(I,J)=-FMAX(I,J); x.up(I,J)=FMAX(I,J); ** Constraints are declared. EQUATIONS COST objective function BALANCE(I) conservation of flow conditions; ** The objective function is the sum of the absolute values of flows between ** connected nodes. This requirement is stated by the $CONEX(I,J) condition. ** The four BALANCE equations only consider the flow between ** two connected nodes and again the $CONEX(I,J) condition is necessary. COST .. z =E= SUM((I,J)$CONEX(I,J),ABS(x(I,J))) ; BALANCE(I) .. SUM(J$CONEX(I,J),x(I,J))-SUM(J$CONEX(J,I),x(J,I))=E=F(I) ; ** The next two sentences define the model, with all the above constraints, ** and direct GAMS to solve the problem using a nonlinear programming ** (with discontinuous derivatives, dnlp) solver. MODEL wsn_dnlp /ALL/; SOLVE wsn_dnlp USING dnlp MINIMIZING z; ******************************************************************** ******************************************************************** $Title THE MATRIX BALANCING PROBLEM. ** The sets of indices are defined. ** Index I is used to refer to the rows of the updated matrix. ** Index J is used to refer to the columns of the updated matrix. SET I index associated with the rows of the matrix /R1*R4/ J index associated with the columns of the matrix /C1*C4/; ** The vectors of data are defined as parameters. ** Data are assigned to vector elements. PARAMETERS R(I) number of estimated trips from the origin I /R1 12000 R2 10500 R3 3800 R4 7700/ C(J) number of estimated trips to destination J /C1 6750 C2 7300 C3 10000 C4 9950/ ; ** The updated trip matrix is defined as a table. TABLE OT(I,J) observed (updated) trip matrix C1 C2 C3 C4 R1 60 275 571 R2 50 410 443 R3 123 61 47 R4 205 265 45 ; ** The next sentences declare the optimization variables. ** First, the objective function variable is declared. ** Then the remaining variables with their associated indices are declared. VARIABLES z error of good fit t(I,J) number of predicted trips from I to J; ** The types of variables are established in the next sentence. ** The variables in the origin-destination trip matrix, ** which define the amount of trips, are positive. POSITIVE VARIABLES t; ** The problem constraints are declared. ** First, the objective function constraint (COST). ** This function is a weighted least square function ** and gives the distance between observed and estimated trip matrices. ** The remaining constraints mean that the estimated matrix must ** have a certain sum of columns and rows. EQUATIONS COST objective function SO(I) satisfying origins SD(J) satisfying destinations; COST.. z=E= SUM((I,J)$(ORD(I) NE ORD(J)),POWER(t(I,J)-OT(I,J),2)/OT(I,J)); SO(I).. SUM(J$(ORD(I) NE ORD(J)),t(I,J)) =e= R(I); SD(J).. SUM(I$(ORD(I) NE ORD(J)),t(I,J)) =e= C(J); ** The following sentences define the matrix balancing problem ** with all the declared constraints and direct GAMS to solve the problem. MODEL mbp /ALL/; SOLVE mbp USING nlp MINIMIZING z; ** The following sentence gives the solution in a table format. DISPLAY t.l; ******************************************************************** ******************************************************************** $title The Traffic Assignment Problem 4 links x 3 node x 2 commodities SETS r routes /r1*r5/ a links /a1*a4/ w demands /w1*w2/; PARAMETERS D(w) Demand /w1 4000 w2 2500/ K(a) Capacity on link /a1 500 a2 400 a3 400 a4 500/ b(a) Congestion Parameter /a1 1. a2 1. a3 1. a4 1./ n(a) Congestion Parameter /a1 4. a2 4. a3 4. a4 4./ C0(a) Cost for empty link / a1 5 a2 7 a3 10 a4 2/ ; TABLE DELTAR_W(r,w) Incidence matrix route-demand w1 w2 r1 1 0 r2 1 0 r3 1 0 r4 0 1 r5 0 1 TABLE DELTAR_L(r,a) Incidence matrix route-link a1 a2 a3 a4 r1 1 0 0 0 r2 0 1 0 1 r3 0 0 1 1 r4 0 1 0 0 r5 0 0 1 0; VARIABLES H(r) Flow on route r F(a) low on link a Z Total time on transport network POSITIVE VARIABLE H, F; EQUATIONS COST objective function SD(w) satisfying demand CF(a) conservation flow; SD(w).. SUM(r,H(r)*DELTAR_W(r,w))=e= D(w); CF(a).. SUM(r,H(r)*DELTAR_L(r,a))=e= F(a); COST..Z=e=SUM(a,C0(a)*F(a)+(b(a)/(n(a)+1.))*(F(a)/K(a))**(n(a)+1.) ); MODEL TAP /all/; SOLVE TAP using nlp minimizing Z; DISPLAY H.L; DISPLAY F.L; ******************************************************************** *******************************************************************