0% found this document useful (0 votes)
351 views37 pages

General Algebraic Modeling Systems (Model Answers)

Here are the steps to manipulate the data: 1) Calculate total labor by sector: PARAMETER L(l); L(l) = sum(h, LABOR(l,h)); 2) Calculate total labor by household area: PARAMETER H(h); H(h) = sum(l, LABOR(l,h)); 3) Calculate total wage bill by household area: PARAMETER WB(h); WB(h) = H(h) * W(h); 4) Calculate aggregate wage bill: SCALAR TOTWB; TOTWB = sum(h, WB(h)); This manipulates

Uploaded by

nada abdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
351 views37 pages

General Algebraic Modeling Systems (Model Answers)

Here are the steps to manipulate the data: 1) Calculate total labor by sector: PARAMETER L(l); L(l) = sum(h, LABOR(l,h)); 2) Calculate total labor by household area: PARAMETER H(h); H(h) = sum(l, LABOR(l,h)); 3) Calculate total wage bill by household area: PARAMETER WB(h); WB(h) = H(h) * W(h); 4) Calculate aggregate wage bill: SCALAR TOTWB; TOTWB = sum(h, WB(h)); This manipulates

Uploaded by

nada abdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

General Algebraic

Modeling Systems
( Model Answers)
A. Set declaration and
Definition
Questions:
*********
1. Declare and define the following sets in
GAMS:
Set S = {a,b,c}
Set m  M ,
m = ξ{Egypt, Lebanon, Kuwait}
2. Include in the above example two
approaches describing or documenting the two
set.
SET
S First three letters / a, b, c / ;
______________________________________________
SET S First three letters /
a first letter
b second letter
c third letter / ;

SET
m subset of Arab Countries
/ Egypt , Lebanon, Kuwait / ;
______________________________________________
SET m subset of the Arab Countries /
Egypt Country of Egypt
Lebanon Country of Lebanon
Kuwait State of Kuwait /
;
3. Define the following sets in a compact GAMS form.
Set T = {1990,1992,1993,1994,1995}
Set MC = {MC1,MC2,…….MC10}
SETS
MACINES / MC1*MC10 /
GENERAL / A1BC*A20BC /
DIFFERENT / A01BC* A20BC / ;
4. Identify illegality in the following GAMS statement:
SET
Illegale1 / A15BC*A10BC /
Illegale2 / A1X1* A9X9 /
Illegale3 / A1* B9 /;

Illegale1: Set members should be organized in an ascending order


/A10BC*A15BC/
Illegale2: the base member …X1 is different from ….X9
/ A1X1* A9X1/ or / A1X9*A9X9/
Illegale3: No common base for repetition of set
/ A1 * A9 / or /B1* B9 /
5. A set of Egyptian universities (public and private) are defined as:
Set U = { Cairo, Alex, Ain-shams, British, October, German}

Develop a GAMS formulation for the set U as well as the subsets of


Public and private Universities ?.

_____________________________________________________

SET u Egyptian Universities


/ Cairo, Alex, Ain-shams, British, October, German /
u(pu) Public Universities
/ Cairo, Alex, Ain-shams /
u(Pr) Private Universities
/ British, October, German /
;
6. Develop GAMS statement for a two dimensional set for country
Ports mapping based on the following information:
Set c = { Egypt, Italy, Nederland}
Set p = {Alexandria, Port-Said, Venis, Genewa, Amesterdam, Roterdam}
_________________________________________________________

SET C countries
/ Egypt, Italy , Nederland /
P Ports
/ Alexandria, Port-Said, Venis, Genewa, Amesterdam, Roterdam /
*Two dimensional set to associate Ports with countries.
SET PTOC(P,C) Port to country relationship
/ Egypt . (Alexandria, Port-Said)
Italy . (Venis , Genewa )
Nederland . (Amesterdam, Roterdam )
/ ;
7. Multiple set names and sparse matrix. In the following input output
matrix, develop a mapping set to identify non empty cells of the
matrix using GAMS.
Agriculture Industry Services
Agriculture 10 20
Industry 30
Services 15 40
________________________________________________________

SET C Activities /Agriculture, Industry, Services /;


ALIAS(C,CP);
SET MAP(C,CP) mapping set /
Agriculture. Agriculture
Agriculture . Services
Industry. Industry
Services . Agriculture
Services . Industry / ;
B. Data Entry and
Management in GAMS
1. The demand in thousands of Honda Cars in three Egyptian cities is
defined as follows:
Cairo = 50, Alexandria = 45, and Aswan = 25
Develop two GAMS formulations to enter data using sets and
Parameter reserved words.

----------------------------------------------------------------------------------

Set I Demand for Cars / CAIRO,ALEX, ASWAN /

PARAMETER A(I) Demand in Markets


/ CAIRO 350
ALEX 600
ASWAN 200 /;
------------------------------
PARAMETER A(I) Demand in Markets

/ CAIRO = 350 , ALEX = 600, ASWAN = 200 /;


2. Given the following quantity of government and private sector
labors by household area (in Thousands).
Government Sector Private Sector
Urban Household area 400 1000
Rural Household Area 350 700
Enter the labor data in a GAMS model using both set, parameter
and Table GAMS reserved words.
-----------------------------------------------------------------------
SETS
l Labor by category / GOV, PRIV /
h Household type / URBAN, RURAL / ;
TABLE LABOR (l,h) number of workers
In thousands.

URBAN RURAL
GOV 400 350
PRV 1000 700 ;
2. Given the following quantity of government and private sector
labors by household area (in Thousands).
Government Sector Private Sector
Urban Household area 400 1000
Rural Household Area 350 700
Enter the labor data in a GAMS model using both set, parameter
and Table GAMS reserved words.
-----------------------------------------------------------------------
SETS
l Labor by category / GOV, PRIV /
h Household type / URBAN, RURAL / ;
PARAMETER A(l,h)
/ GOV. URBAN 400
GOV.RURAL 350
PRV. URBAN 1000
PRV.RURAL 700
/ ;
3. Given the following quantity of government and private sector labors by
household area and skill level (in Thousands) :
Government Sector Private Sector
Urban Household area(Skilled) 400 1000
Rural Household Area (skilled) 350 700
Urban Household area ( unskilled) 400 1000
Rural Household Area (unskilled) 350 700

Enter the multi-dimensional labor data in a GAMS model once using


Sets &parameter, and again using sets & table reseved GAMS words.
---------------------------------------------------------------------------------------------
SETS
l Labor category / GOV, PRIV/
s Labor by skill / SKILL, UNSKILL/
h Labor by household type/URBAN, RURAL/
;
TABLE labor(l,h,s) number by category

SKILL UNSKILL
GOV.URBAN 400 400
GOV.RURAL 350 350
PRV.URBAN 1000 1000
PRV.RURAL 700 700 ;
---------------------------------------------------------------------------

Parameter labor(l,h,s) number by category /


(GOV.URBAN). (SKILL,UNSKILL) 400
(GOV.RURAL). (SKILL,UNSKILL) 350
(PRV.URBAN). (SKILL,UNSKILL) 1000
(PRV.RURAL). (SKILL,UNSKILL) 700
/ ;
4. Given the time set t = 2000 to 2005 and the non-homogenious rows
Shown in the following matrix:

2000 2001 2002 2003 2004 2005


Mean 500 600 700 800 900 1000
Median 300 320 340 380 400 500
Variance 10 12 16 18 20 20
Maximum 800 900 1000 1200 1400 1800
Read the above matrix in GAMS format using set and table reserved
words.
--------------------------------------------------------------------------------
SET T /2000*2005/
TABLE FPR(*,T) Parameter values
2000 2001 2002 2003 2004 2005
Mean 500 600 700 800 900 1000
Median 300 320 340 380 400 500
Variance 10 12 16 18 20 20
Maximum 800 900 1000 1200 1400 1800
;
5. Re-write the following data entry problem using the
“ Parameter” reserved GAMS word instead of “Table”.
Sets
L Labor by category / GOV, PUB, PRIV /
H Household type / URBAN, RURAL /;
TABLE LABOR (L,H) number of workers in thousands.
URBAN RURAL
GOV 200 150
PUB 20 30
PRV 300 250 ;
------------------------------------------------------------------------------------
Sets
L Labor by category / GOV, PUB, PRIV /
H Household type / URBAN, RURAL /;
PARAMETER LABOR (L,H) labor by category and HH type
/ GOV.URBAN 200, GOV.RURAL 150
PUB.URBAN 20, PUB.RURAL 30
PRV.URBAN 300, PRV.RURAL 250 / ;
C. Data Manipulation and
Computations in GAMS
Part C: Data Manipulation & Computation in GAMS

1. Given the following data items in the form of matrix, vector and scalar.
Sets:
l Labor by Sector / GOV, PUB, PRIV /
h Household type / URBAN, RURAL / ;
TABLE LABOR (l,h) number of workers in thousands.
URBAN RURAL
GOV 200 150
PUB 20 30
PRV 300 250 ;
PARAMETER W(h) wage rate by household area /
URBAN 30 , RURAL 25 / ;
SCALAR GR average growth rate (in percent) of labor by sector
and Household Area / 2 / ;
a) Compute the new number of labor by Sector and Household area given the
annual growth rate GR in percent (%).
b) Compute the total wage income by sector using the labor matrix and the
wage rate by household category.
Solution:
********
Parameter NLAB(l,h) new labor number by category ;
NLAB(l,h) = GR* LABOR(l,h) ;
Parameter WLAB(l) Wage by labor sector ;
WLAB(l) = SUM (h, W(h) *NLAB(l,h)) ;
------------------------------------------------------------------------------------
2. Convert the following mathematical specifications or equations to a
GAMS Format:
TOTCm = ∑iCim
COUNT= ∑i∑j Aij
EMP= ∑t Lt Mt
OUTPUT= ∏i INPUT(i) **SHARE(i)
Using Sets , Parameters , Tables , SUM and PROD reserved
GAMS key words.
Solution:
**********
TOTC(m) = SUM(i, C(i,m) ) ;

COUNT = SUM((i, j) , A(ij)) ;

EMP = SUM(t, L(t) * M(t) ) ;

OUTPUT = PROD(i, (INPUT(i)**SHARE(i))) ;


-------------------------------------------------------------------------------

3. Define the following arithmetic functions in GAMS:


abs(x), ceil(x), exp(x), floor(x), log(x), log10(x),ax(x,y,…),
min(x,y,…), sqrt(x), sqr(x).
------------------------------------------------------------------------------
Expressions and Functions
Arithmetic Functions
abs(x)  absolute value, |x|
ceil(x)  ceiling of x, next higher integer
exp(x)  exponential
floor(x)  floor of x, next lower integer
log(x)  natural logarithm of x, ln x
log10(x)  logarithm base 10
max(x,y,…)  maximum of two or more arguments
min(x,y,…)  minimum of two or more arguments
sqrt(x)  square root of x
sqr(x)  square of x

Functions such as abs() and max()… are recognized as


nonlinear if applied on variables.
4.Define the following random variables generating functions in GAMS:
Exponential(m), Normal(μ,σ), Uniform(m,n)
-----------------------------------------------------------------------------------------

Probability Functions
Exponential(b)
Normal(μ,σ)
Uniform(m,n)
These are functions used to generate
random deviates – based on Mont Carlo
Methods – pertaining to a specific
probability density function
(negative exponential , Normal and
uniform ditributions).
5. Given the following sets and parameters:

SETS
L Labor by category / GOV, PUB, PRV /
S Labor by Skill / SKILLED, UNSKILL /
H Household type / URBAN, RURAL / ;
PARAMETER LABOR(L,S,H) labor by category skill and household type
/ GOV.SKILLED.URBAN 100, GOV.SKILLED.RURAL 150
PUB.SKILLED. URBAN 200, PUB.SKILLED.RURAL 120
PRV.SKILLED. URBAN 250, PRV.SKILLED.RURAL 180
GOV.UNSKILL.URBAN 100, GOV.UNSKILL.RURAL 150
PUB.SKILL. URBAN 200, PUB.SKILL.RURAL 120
PRV.UNSKILL. URBAN 250, PRV.UNSKILL.RURAL 180
/ ;
Use the two GAMS reserved words “DISPLAY” and “OPTION” to design
a transparent output report to display the LABOR parameter.
Solution:
********
Display “ Labor Number by Category”, LABOR;
Display “ ************************” ;
Option 3 , 2 , 1 ;
3 = Number of decimal points ******. ***
2 = Number of indexes appearing as Row
1 = Number of indexes Appearing as Columns
-----------------------------------------------------------------------
Labor Number by Category
************************
******* ******* ******* ********
*****.******
*****.****** 200.000
*****.******
Part D: Variables and Equations
1. Determine the type of decision variables in GAMS and the way of their
Declaration and bounding.
----------------------------------------------------------------------------------------
Variable types and bounds in GAMS

Keyword BOUNDS
LO UP
FREE (default) -INF +INF
POSITIVE 0 +INF
NEGATIVE -INF 0
BINARY 0 1
INTEGER 0 100
----------------------------------------------------------------------------------
VARIABLES
Z(P,I) Process Level
X(C,I,J) Shipment of final product
PHI Total Cost ;
Positive Variables Z,X ;
Free Variables PHI ; ( We can exclude this statement since free
variables is the default)
2. Explain the following GAMS statements:
Assignment Statement to bound Variables
Example 1:
X.UP(C,I,J) = 1000 ; PHI.UP = INF ;
XX.UP(“CAIRO”, “ALEX”) = 200 ;
Example 2:
C.LO(T) = 0.01 ;
Example 3:
C.L(T) = 4*CINIT(T)
Example 4:
C.FX(“Period-1”) = 1 ;
Example 5:
C.FX(“1985”) = 1 ; C.LO(T) = 0.01 ;
C.LO(T) = 0.01 ; C.FX(“1985”) = 1;
Other Examples:

Variable status ;

Positive Variable quantity (production, markets) ;

quantity.up(production,markets)=1000 ;

Binary Variable buydecision(stocks) ;

Binary Variable buydecision.l(stocks) = 1 ;

Display quantity.l, quantity.ub, quantity.m ;

Variable status.fx = 500 ;


General Remarks on Solution
Variable Suffix Description
**************************
.LO
Lower bound of the variable can be defined by modeler or takes default value,
Example: x.lo = 10;
.UP
Upper bound of the variable can be defined by modeler or takes default value,
Example: x.up = 1000;
.FX
Fixed value of the variable, modelers might need to fix a variable value within a
given run or model execution phase
.L
Activity level or current value of the variable and receives new values after model
execution
.M
Marginal or Dual value of a variable after model execution
3. Identify the differences between the following Equations in computing
the following cost in GAMS :
COST . . TC =E= C1+C2+C3 ;
COST . . TC =E= SUM(I, C(I)) ;
COST . . TC=E=C(“INN”)+C(“ORDER”)
+C(“short”) ;
LC . . SUM(I, L(I)*X(I)) =L= Lbar ;
________________________________________________________
The first one is simple or non indexed equations .
The second one uses the reserved word SUM to transform an indexed
variable to a simple equation.
The third one shows how to use specific elements of a set to define a simple
or non indexed equation.
The fourth one is an inequality “ less than or equal” and uses the sumation
capability to have a simple non indexed equation.
4. Explain the following specific equation types:
Equations
SB (i) Supply balance
DB (j) Demand balance
CA(i) ;

SB (i) . . SUM (j $ MAP (i, j), X (i, j)) =L= S (i) ;


DB (j) . . SUM (j $ MAP (i, j), X (i, j)) =G= D (i) ;
CA (i) . . SUM (i $ A (i), A (i)*X (i)) =L=m
CA (i) . . SUM (i $ A (i) NE 0), A (i)*X (i)) =L=m
---------------------------------------------------------------------------------
In the above set of equations , the SUM over i or j is restricted to be
carried out on a subset of the values of X and A which do not have “Zero”
Values.
5. Use the dollar sign ($) operator of GAMS to implement the following
algorithms:
a). Read the scalars X,Y. ((Y=2and X=1)
If X > 1.5 then X=2
Else X = 0
Solution:
Scalar X,Y;
Y=2; X=1;
X$(Y GT 1.5 ) = 2;
X$(Y LE 1.5) = 0 ;
Or:
X = 2$ (Y GT 1.5)
+ 0$ (Y LE 1.5);
----------------------------------------------------------------------
b) Read R(i) and S(i), i= 1,2, …..n
If S(i) = 0 then R(i) = inf
Else R(i) = 1/S(i)
Solution:
R(I) = INF $( S(I) EQ 0) + (1/S(I)) $( S(I) NE 0 ); OR:
R(I) = INF ; R(I) $ S(I) = 1/S(I) ;
c) Read S(i), V(i) , i=1,2,….n
If 2 <= S(i) <= 4 then S(i) = V(i)
Else S(i) = 0
Solution:

S(I) = V(I) $ (V(I) GE 2 AND V(I) LE 4) ;


------------------------------------------------------------------
d) Read A(i), i=1,2… and S(k),T(k), B(k) , k=1,2,…
If S(k) and T(k) NE 0
Then U(i) = SUM((k), A(i)*B(k))
Else U(i) = 0

Solution:

U(I) = SUM(K $ (S(K) $ T(K)), A(I)*B(K)) ;


2. Given the following GAMS program:
SETS R Regions
/ UPPER-EGY , LOWER-EGY /
S Cities
/ CAIRO, ALEX, ASWAN, LUXOR /
CORR(R, S) correspondence of Reg. & Cities
/ UPPER-EGY . (ASWAN, LUXOR)
LOWER-EGY . (CAIRO, ALEX) /

PARAMETERS
Y(R) Income of each region (m LE )
INCOME (s) Income of cities (m LE)
/ CAIRO 100 , ALEX 70
ASWAN 50 , LUXOR 30 /
For each region R compute the summation over the S cities such that
CORR(R,S) exists.
---------------------------------------------------------------------
Solution:
Y(R) = SUM(S $ CORR(R, S), INCOME (s)) ;
3. Given the following GAMS statements:
* Declaration and definition of set and model parameters
Set t / 2000 * 2005 /
Parameter
pop(t) / 2000 90 /
Growth(t) / 2000 2, 2001 2.1
2002 2.2, 2003 2.3
2004 2.4, 2005 2.5 / ;
Compute population size during the period (2000 – 2005) using
GAMS-LOOP statement, Given that pop(t) is the population size and
Growth(t) is the annual population growth Rate(%).
-----------------------------------------------------------
Solution:

Loop(t, pop(t+1) = pop(t) + Growth (t) ) ;


4. Develop the following algorithm using the GAMS if-elseif-else
Statement.
Mathematical Formulation (with $ Operator)
P( i ) $( f <= 0 ) = -1 ;
P( i ) $ (( f > 0) and ( f < 1 )) = P( i ) **2 ;
P( i ) $ (f > 1) = P( i ) **3 ;
--------------------------------------------------------------------------
Solution:

if ( f < = 0,
P( i ) = -1 ;

Elseif (( f > 0 ) and ( f < 1 )) ,

P( i ) = P( i ) **2 ;
Else
P( i ) = P ( i )**3 ;
);
Good Luck

Motaz Khorshid

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy