# [closed] Translate AMPL code to GAMS

 -4 Following is my model written in AMPL language. Could anyone kindly translate it to GAMS language? Thanks in advance! param m := 22; param h := 11; set A := 1 .. m-1; set B := 1 .. h-1; var x{A}; var y{A}; var t >= -3.1416, <= 3.1416; maximize obj: x[h]; subject to gauss_r {r in B} : sum{j in 1 .. 2*r-1} (x[j]*x[2*r-j]-y[j]*y[2*r-j]) + sum{j in 2*r+1 .. m-1} (x[j]*x[2*r+m-j]-y[j]*y[2*r+m-j]) = 2*x[2*r]; gauss_i {r in B} : sum{j in 1 .. 2*r-1} (x[j]*y[2*r-j]+y[j]*x[2*r-j]) + sum{j in 2*r+1 .. m-1} (x[j]*y[2*r+m-j]+y[j]*x[2*r+m-j]) = 2*y[2*r]; conjugate_r {r in B} : x[r] - x[m-r] = 0; conjugate_i {r in B} : y[r] + y[m-r] = 0; modulus {j in A} : x[j]*x[j] + y[j]*y[j] = x[h]*x[h]; DH_r {r in B} : x[r]*x[h+r] - y[r]*y[h+r] - x[h]*x[2*r]*cos(r*t) + x[h]*y[2*r]*sin(r*t) = 0; DH_i {r in B} : x[r]*y[h+r] + y[r]*x[h+r] - x[h]*y[2*r]*cos(r*t) - x[h]*x[2*r]*sin(r*t) = 0; t_r : cos(h*t) = 1;  asked 27 Jul '12, 00:55 Binzhou Xia 10●1●3 accept rate: 0% fbahr ♦ 4.6k●7●16 Here are my modified models in AMPL and GAMS. (I now treat t to be a parameter taking values 0, ... ,h-1, both for simplifying the model and the further information it will provide.) Any suggestions on the modeling and/or the selection of solvers are appreciated. AMPL language param m := 22; param h := 11; param pi := 4 * atan(1); param t := 0; set A := 1 .. m-1; set B := 1 .. h-1; var x{A}; var y{A}; maximize obj: x[h]; subject to gaussr {r in B} : sum{j in 1 .. 2*r-1} (x[j]*x[2*r-j]-y[j]*y[2*r-j]) + sum{j in 2*r+1 .. m-1} (x[j]*x[2*r+m-j]-y[j]*y[2*r+m-j]) = 2*x[2*r]; gaussi {r in B} : sum{j in 1 .. 2*r-1} (x[j]*y[2*r-j]+y[j]*x[2*r-j]) + sum{j in 2*r+1 .. m-1} (x[j]*y[2*r+m-j]+y[j]*x[2*r+m-j]) = 2*y[2*r]; conjugater {r in B} : x[r] - x[m-r] = 0; conjugatei {r in B} : y[r] + y[m-r] = 0; modulus {j in A} : x[j]*x[j] + y[j]*y[j] = x[h]*x[h]; DHr {r in B} : x[r]*x[h+r] - y[r]*y[h+r] = x[h]*x[2*r]*cos(2*r*t*pi/h) - x[h]*y[2*r]*sin(2*r*t*pi/h); DHi {r in B} : x[r]*y[h+r] + y[r]*x[h+r] = x[h]*y[2*r]*cos(2*r*t*pi/h) + x[h]*x[2*r]*sin(2*r*t*pi/h);  GAMS language Scalar m /22/; Scalar h /11/; Scalar t /0/; Set A /1 * 21/; Alias(A,B); Alias(A,C); Alias(A,D); Alias(A,E); Variables x(A) y(A) w; Equations gaussr(A,B) gaussi(A,B) conjugater(A,B) conjugatei(A,B) modulus(A,B) DHr(A,B,C,D) DHi(A,B,C,D) xh(A); gaussr(A,B)$(2*ord(A)=ord(B)).. sum((C,D)$(ord(C)+ord(D)=ord(B)),x(C)*x(D)-y(C)*y(D))+sum((C,D)$(ord(C)+ord(D)=ord(B)+m),x(C)*x(D)-y(C)*y(D)) =e= 2*x(B); gaussi(A,B)$(2*ord(A)=ord(B)).. sum((C,D)$(ord(C)+ord(D)=ord(B)),x(C)*y(D)+y(C)*x(D))+sum((C,D)$(ord(C)+ord(D)=ord(B)+m),x(C)*y(D)+y(C)*x(D)) =e= 2*y(B); conjugater(A,B)$(ord(A)+ord(B)=m).. x(A)-x(B) =e= 0; conjugatei(A,B)$(ord(A)+ord(B)=m).. y(A)+y(B) =e= 0; modulus(A,B)$(ord(B)=h).. x(A)*x(A)+y(A)*y(A) =e= x(B)*x(B); DHr(A,B,C,D)$(ord(A)+h=ord(B) and ord(C)=h and 2*ord(A)=ord(D)).. x(A)*x(B)-y(A)*y(B) =e= x(C)*x(D)*cos(ord(C)*t*pi/h)-x(C)*y(D)*sin(ord(C)*t*pi/h); DHi(A,B,C,D)$(ord(A)+h=ord(B) and ord(C)=h and 2*ord(A)=ord(D)).. x(A)*y(B)+y(A)*x(B) =e= x(C)*y(D)*cos(ord(C)*t*pi/h)+x(C)*x(D)*sin(ord(C)*t*pi/h); xh(A)$(ord(A)=h).. x(A) =e= w; Model ds /all/; Solve ds using nlp maximizing w;  (31 Jul '12, 10:27) Binzhou Xia

### The question has been closed for the following reason "The question is answered, right answer was accepted" by fbahr 15 Jan '14, 14:16

 4 AMPL's param matches GAMS' SCALAR construct, e.g.: SCALAR M / 22 / ; SCALAR H / 11 / ;  Translation of AMPL sets and vars is pretty straight forward, e.g.: SET A / 1 * 21 / ; !!< *edit*, was "SET A / 1 * M-1 /"  and VARIABLES X( A ) T ;  [Edit: Whoops, my bad... you can't use scalars in set definitions – since members of sets are considered to be strings, and so boundary defining elements are expected to be. In more detail discussed in the comments sections below.] Domain-restrictions on variables are imposed using .LO, .UP: T.LO = -3.1416 ; T.UP = 3.1416 ;  Conditions are  only a little more complicated, e.g.: ALIAS(A,F) ; !!< *edit*, again: see additional comments given below EQUATION MODULUS( A,F ); MODULUS( A,F )$(ord(F) = H).. X(A)*X(A) + Y(A)*Y(A) =E= X(F)*X(F);  And, finally, the objective function: MODEL TEST /ALL/ ; SOLVE TEST USING LP MINIMIZING X('11') ;  Useful resource: answered 27 Jul '12, 05:03 fbahr ♦ 4.6k●7●16 accept rate: 13% Thanks a lot! Can I operate the indices by multiplication in GAMS the same as in AMPL like sum{j in 1 .. 2*r-1} and x[2*r-j] in the constraints gauss_r {r in B}? (27 Jul '12, 05:29) Binzhou Xia In GAMS, all members of sets are considered to be strings (for instance, SET A / 1 * 3 / ; will give you a set {'1', '2', '3'}), hence: you can't really apply integer arithmetics to set members (+ and - are interpreted as element-wise right/left shifts, i.e., '1' + 2 is element '3', not the scalar 3). So you need to do something clever using conditionals: (28 Jul '12, 14:22) fbahr ♦ SET B / 1 * .. / ; ALIAS(B,C) ; !!< virtual 'copies' of set B; necessary since sets ALIAS(B,D) ; !! can be bound only exactly once per scope ALIAS(B,E) ; EQUATION gauss_r(B,C) ; EQUATION gauss_r(B,C)$(ord(C) = 2*ord(B)).. \ sum((D,E)$(ord(D) <= 2*ord(B)-1 and ord(E) = 2*ord(B)-ord(D)), \ (x(D)*x(E)-y(D)*y(E))) \ + ... \ =E= 2*x(C);  (28 Jul '12, 14:23) fbahr ♦ ord gives the "position" of a set member in the set's definition; so when evaluating ord(B) for the members of set B = {'1', '2', '3'}, we'll get scalars $$\in$$ {1, 2, 3}. EQUATION gauss_r(B,C) says: generate instances of gauss_r for all combinations of members (b,c) of sets B and C. A conditional $( ... ) restricts the set of feasible member combinations to those that match a given condition; for instance: $(ord(C) = 2*ord(B)) asks for combinations where the index of the element from C is twice the index of the element from B. (28 Jul '12, 14:29) fbahr ♦ Why do there come a error message "171 Domain violation for set" under x(D)*x(E)-y(D)*y(E) and 2*x(C)? (29 Jul '12, 11:29) Binzhou Xia x and y are defined over set A; but accessed – in gauss_r – using members of set B. The only way to allow for this is by defining B as subset of A. Instead of SET B / 1 * .. / ; you'll have to use: SET B(A); !!< declaring that B is a subset of A B(A)$(ord(A) < H) = yes; !!< = {'1,'2',..,'10'}  (29 Jul '12, 12:54) fbahr ♦ On second thought, since gauss_r needs to access all members of set A (by means of "set" D), you probably want to get rid of set B anyway – and use A in its place. (29 Jul '12, 16:32) fbahr ♦ showing 5 of 7 show 2 more comments
 4 You may want to add to the GAMS model: Variables x(A) y(A) w; positive variables x,y;  By default variables are free in GAMS. Now it should solve fairly quickly with a global solver like Baron or lindoglobal. Glomiqo may be another candidate (we had to use an updated version for this solver). Otherwise you may want to think about good bounds on x and y. For global solvers these bounds are very important. ---------------------------------------------------------------- Erwin Kalvelagen Amsterdam Optimization Modeling Group erwin@amsterdamoptimization.com http://amsterdamoptimization.com ----------------------------------------------------------------  answered 01 Aug '12, 14:39 erwin 401●1●3 accept rate: 10% An approach to bounding x(A), y(A) and w (we can assume w to be positive.) is to substitute them by x(A)/w, y(A)/w and 1/w respectively and get new x(A),y(A)\in [-1,1]. Although this may lead the minimum of new w to 0, it seems the trivial minimum will not be reached unless m is congruent to 2 mod 8. So the transformation should work for m=22 posted above. Is there any open access to Glomiqo? (02 Aug '12, 23:59) Binzhou Xia Glomiqo is a commercial software... (03 Aug '12, 08:23) Binzhou Xia
 3 See also: AMPL-GAMS conversion, by Erwin Kalvelagen answered 30 Jul '12, 14:23 yeesian 846●2●10 accept rate: 3% I got it! Then how to represent \pi in AMPL and GAMS? (31 Jul '12, 00:07) Binzhou Xia 1 For AMPL: you might want to consult the AMPL Modelling Language googlegroups. Here's a possible answer. For GAMS: consult their user guide(pdf). You'll find it on pg57 (31 Jul '12, 00:19) yeesian

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Tags:

×51
×29