Domain error

Problems with modeling
Post Reply
rookie
User
User
Posts: 5
Joined: 2 years ago

Domain error

Post by rookie »

Hi,

I have two questions:
  • Supposing I have

    Code: Select all

    Sets i, j;
    Variable x(i);
    Equation eq1(j);
    eq1(j) .. sum(i$(ord(i) le 4), x(i)) + x(j) =e= 100;
    
    How could I use x(j) in an equation if I know both sets are equal?
  • Supposing I have

    Code: Select all

    Sets i, j;
    Variable x(i);
    Parameter y(j);
    Equation eq1(j);
    eq1(j) .. sum(i$(ord(i) le 4), x(i)) + x(y(j)) =e= 100;
    
    How could I use that x(y(j)) without giving domain error?
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain error

Post by abhosekar »

If you define two different sets i and j, there is no way to use them interchangeably. But if you define one set and declare the other as an alias, you can do it
For example
set i;
alias (i, j);

For your second question, what does it mean when you say x(y(j))? May be you are thinking in the wrong direction here. You should never need x(y(j)).

- Atharv
rookie
User
User
Posts: 5
Joined: 2 years ago

Re: Domain error

Post by rookie »

supposing that y(j) can take the same values as the set i, I would like to use it to index as x(y(j))
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain error

Post by abhosekar »

Fair question. You say that y(j) can take same "values" as set i. However, it should be noted that set i need not contain values. Set i could be /i1,i2,..i10/ for example. Sets are more like strings than values.

Think in terms of writing equations over a subset of j. The order of equation doesn't matter. Which set of j do you want to write equations for (i.e., what is the output of y(j))? If y(j) doesn't include all values of j, you can create a subset jsub(j) and assign all possible values to jsub(j). You can then define equation using jsub.
If y(j) takes all values of j, these constraints are exactly same as using x(j) and you don't need y(j).

This works because you write equation over j. You don't use 'j' anywhere else in the equation except for x(y(j)). You can write the same equation as follows:

eq1(j)$(jsub(j)).. sum(i$(ord(i) le 4), x(i)) + x(j) =e= 100;


- Atharv
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Domain error

Post by bussieck »

y(x(i)) is a construct unknown to classical mathematical programming (MP). Such constructs works in the context of constraint programming. If you have a constraint programming problem GAMS is not the a good candidate to formulate and solve such problems. If you have an MP problem you can can't use such constructs. There are ways to reformulate y(x(i)) into MP constructs. Let have z(i) = y(x(i)) with i=i1..in and x(i) in {1,2,3,4,5} for all i. Then you can transform the term z(i) = y(x(i)) into regular algebra as follows:

Code: Select all

set i / i1*i10 /, xi /1*5/;
integer variable x(i); binary variable b(i,xi), variable y(xi), z(i);
equation defz(i);
defz(i).. z(i) =e= sum(xi, y(xi)*b(i,xi));
defb(i).. sum(xi, b(i,xi)) =e= 1;
* followed by code that govern the variables x and y
You are left with a quadratic term of a continuous variable (y) and a binary variable (b) that you can linearize to get a MIP (in case the rest of your program is linear). If y is bounded then you can do this with bigM reformulations, otherwise you need SOS1 or indicator constraints. You should find some hints in the forum how to do that.

Good luck,
Michael
rookie
User
User
Posts: 5
Joined: 2 years ago

Re: Domain error

Post by rookie »

Thank you all!
Post Reply