Defining vaiables as vector

Problems with syntax of GAMS
Post Reply
AMIIT
User
User
Posts: 7
Joined: 1 month ago

Defining vaiables as vector

Post by AMIIT » 1 month ago

Hi all.
I'm quite new to GAMS and trying to solve an interesting problem. A part of it consists of defining a specific vector. I want to define a vector that contains 8 variables in it. For example, the vector A with 8 variables as x1, x2,...x8. The variables should be able to take values depending on conditions that I provide later in the code. How to define such a vector? Also if possible, can this be applied to matrices as well?

abhosekar
Moderator
Moderator
Posts: 182
Joined: 8 months ago

Re: Defining vaiables as vector

Post by abhosekar » 1 month ago

You want to define one dimensions and two dimensional variables. GAMS allows you to do this.
First you need to define sets and define variables with the help of those.
In your case,

set i/1*8/;
variable x(i);

will do the job. For multidimensional variables

set
i /1*8/
j /j1*j5/;

variable x(i, j);

You can use this for any variables. To access individiual variables, you can use set elements in quotes such as x('1'), or x('1', 'j1') and so on.

- Atharv

AMIIT
User
User
Posts: 7
Joined: 1 month ago

Re: Defining vaiables as vector

Post by AMIIT » 1 month ago

Thank you for your reply. But I'm still not clear, as in, if I define x(i) as you mentioned, first of all, how would I go about assigning values to each variables when I need it? Second, I need to multiply this vector with a matrix containing numbers after the vector gets its values. So any suggestions on how to do that?

abhosekar
Moderator
Moderator
Posts: 182
Joined: 8 months ago

Re: Defining vaiables as vector

Post by abhosekar » 1 month ago

Fair questions. You have to first understand what are variable values in terms of optimization? Before you solve( there are no values, it does not make sense to ‘assign values to variables’, you never do that. When you solve, values get assigned.
You can provide initial guess or level values using .l suffix.

Secondly, you say multiplying variables by matrix once they get values. This refers to something you do after your solve statement. Here you would use variablename.l * parameter.

Matrix is data ( you can look at parameters and tables in GAMS) you can have multidimensional parameters as well.

Your questions are fundamental and I would suggest looking at gams tutorials https://www.gams.com/latest/docs/UG_MAI ... l_Examples

- Atharv

AMIIT
User
User
Posts: 7
Joined: 1 month ago

Re: Defining vaiables as vector

Post by AMIIT » 1 month ago

Thank you. A lot of things have been cleared. Let me try again.

Also, there is a slight misunderstanding on what exactly it is that I'm trying to do.
Essentially, I need an 8*8 matrix whose diagonal elements are the 8 variables that I define. I then have to define a minimizing equation where the mentioned matrix would be multiplied with a predefined matrix and one constraint would make 3 out of the 8 variables equal to 1 and the rest 0. So what I mean is, I need to find the combination of these variables that gives minimum value for that equation (say x1, x3, x5 are1 and rest 0).

I want to define such a problem. This is why i wanted what I asked before.

abhosekar
Moderator
Moderator
Posts: 182
Joined: 8 months ago

Re: Defining vaiables as vector

Post by abhosekar » 1 month ago

Okay, I don't understand why you need 8x8 matrix if you are only interested in its diagonal elements. I would suggest only using x(i). Further, why would you multiply it by a matrix. You multiply 8x8 matrix with 8x1 variable, you get 8x1 vector. Which, to an optimization algorithm means that you have 8 objectives. Is this what you want to solve? You should think about your objective a bit more.

On the other hand, if you multiply 1x8 data vector by 8x1 variable then you get one element (almost like an inner product). This is what you traditionally solve using optimization. You should try to get an expression (no matter what dimension matrices you multiply) that leads to one element instead of a matrix output.

Few points regarding all other aspects of your problem as they are super trivial. You can create sets and subsets as follows.
set
i/1*4/
ia(i) /1,3/;

While defining constraints you can use $ condition to restrict equations
con1 $(ia(i)).. x(i) =e= 1;
con1 $(not ia(i)).. x(i) =e= 0;

- Atharv

AMIIT
User
User
Posts: 7
Joined: 1 month ago

Re: Defining vaiables as vector

Post by AMIIT » 1 month ago

Hey, thank you for the answer. I have attached an image of the equation that I am trying to solve. The C matrix is the predefined matrix. The Q matrix is a 8*8 matrix whose diagonal elements are 8 binary variables. I need the minimum trace and the combination of binary variables that give it. The constraint that I will be giving is that any 3 of the variables can be 1, i.e. sum(Variables)=3. This is what I want to model.

Image
Attachments
im.PNG
im.PNG (3.2 KiB) Viewed 925 times

abhosekar
Moderator
Moderator
Posts: 182
Joined: 8 months ago

Re: Defining vaiables as vector

Post by abhosekar » 1 month ago

Few points:
You cannot do invert operation (not GAMS problem but in general in optimization unless you can simplify it). You must simplify your objective or need to know certain properties of the matrix you want to invert.
You are still thinking in terms of matrix terms when you should be thinking in terms of the true expansion. You do not need a 'matrix' Q. You just need a one dimensional variable that represents diagonal elements. When you write your objective function, you will write it in the expanded form and not in the matrix form.
For rest of the problem, to write trace, you simply
alias (i, ii);
sum((i,ii) $(ord(i) ne ord(ii), ....);

Your constraint is simple so there is nothing to comment on it.
- Atharv

AMIIT
User
User
Posts: 7
Joined: 1 month ago

Re: Defining vaiables as vector

Post by AMIIT » 1 month ago

Okay thank you. A lot of things are clear right now.
I am attaching a code I ran previously, where i manually entered values to the C and Q matrix. Would something like this work? I found help in GAMS forum posts long time ago.

Code: Select all

positive variables
F1, F2, F3, F4, F5, F6, F7, F8;

Variables
Z;

Set
    i flows /a, b, c, d, e, f, g, h/
    j cmaa /x, y, z/;
        
Alias (i,k)
    (i,k2)
    (i,k3);
Alias  (j,k4)
    (j,k5)
    (j,k6)
    (j,w)
    (j,u);

    
Parameter
C(i, j)/

a.x 1, a.y 0, a.z 0
b.x 1, b.y 1, b.z -1
c.x 1, c.y 1, c.z -1
d.x 1, d.y 1, d.z -1
e.x 0, e.y 1, e.z 0
f.x 1, f.y 0, f.z -1
g.x 0, g.y 1, g.z -1
h.x 0, h.y 0, h.z 1
/;

display C;

Parameter
Q(i, i)/

a.a 1, a.b 0, a.c 0, a.d 0, a.e 0, a.f 0, a.g 0, a.h 0
b.a 0, b.b 0, b.c 0, b.d 0, b.e 0, b.f 0, b.g 0, b.h 0
c.a 0, c.b 0, c.c 0, c.d 0, c.e 0, c.f 0, c.g 0, c.h 0
d.a 0, d.b 0, d.c 0, d.d 0, d.e 0, d.f 0, d.g 0, d.h 0
e.a 0, e.b 0, e.c 0, e.d 0, e.e 1, e.f 0, e.g 0, e.h 0
f.a 0, f.b 0, f.c 0, f.d 0, f.e 0, f.f 0, f.g 0, f.h 0
g.a 0, g.b 0, g.c 0, g.d 0, g.e 0, g.f 0, g.g 0, g.h 0
h.a 0, h.b 0, h.c 0, h.d 0, h.e 0, h.f 0, h.g 0, h.h 1

/;

display Q;

parameter Ct(j,i);

Ct(j,i) = C(i,j);

display Ct;

Parameter A(w,u);

loop(k,  A(w,u) = sum((k2,k3),Ct(w,k3)*Q(k3,k2)*C(k2,u)));

display A;

Parameter invA(j,j);

execute_unload 'a.gdx', j, A;
execute '=invert.exe a.gdx j A b.gdx invA';
execute_load 'b.gdx', invA;

display invA;

parameter F(i,i);

loop(k6, F(i,i) = sum((k4,k5), C(i,k5)*invA(k5,k4)*Ct(k4,i)));

display F;

scalar D;
D = sum(i,F(i,i))

display D;  
Kindly see if this would work in some way, as expanding the whole matrix seems to be very tedious.

abhosekar
Moderator
Moderator
Posts: 182
Joined: 8 months ago

Re: Defining vaiables as vector

Post by abhosekar » 1 month ago

The example you attach has absolutely nothing to do with your question. In the example, you invert a matrix A= ct*Q*c. However, all of these (Ct and Q) are parameters. This is a big big difference.The moment you say that Q is a variable, you have to do inverse repeatedly and since inverse is an operation that cannot be written in equation form, you cannot use equation oriented optimization for this. I cannot reiterate the same sentence in multiple different ways. I am not telling you to actually expand the matrix, but to 'think' in expanded form. When you say Q is a matrix with diagonal as variables, I think it has 64 variables which does not make sense to me. Therefore, you have to think in terms of a vector (you can always have identity matrix times the vector to get the diagonal matrix). But anyway, with variables inside an inverse, I don't think you have any easy solution. You have to re-think your objective function and get variables outside of inverse and then I can help you.

- Atharv

Post Reply