Summation with parameter

Hello everybody,

I am trying to write the code of UTADIS method. In this code I could not write a summation function.

In the function I have an index s for the summation. However the upper bound of the summation depends on a parameter which is r(j,k). And, I sum w(j,s). Related mathematical formulation can be seen in the red part of the pictiure.

In this formulation, s represent the intervals and r(j,k) represents the interval that alternative k belongs to criterion j. Also w(j,s) is utility value corresponding to interval s and criterion j.

How can write such a summation expression?

Please help me.

Thank a lot :slight_smile: :slight_smile: :slight_smile:
image.png

Hi
You can use the dollar sign to restrict the summation:

sum((i,j,k)$r(j,k), ...

If this is not what you are looking for, please send your code with the summation, parameter and the sets defined, even if it doesn’t work, so it is easier for us to help you.
Cheers
Renger

Hi,

Thanks for your answer. I will use it in another part of my code.

In my problem:

Indexes:
s intervals for each criteria (1,2,…,pp(j)) → [I write this part as " s /1*pp(j)/ " it gives error 764 ]
j number of criteria
k training data set elements

Parameters:
p(j) number of breakpoints
pp(j) number of intervals for criterion j which is (p(j)-1)
r(j,k) interval for alternative k in criteria j
a(j,k) value for alternative k on criteria j

Decision Variable:
w(j,s) utility value for interval s and criterion j

s is an index but its upper bound is depend on parameter pp(j) and r(j,k). Both pp(j) and r(j,k) give interval value which is actually s. But the representation does not contain each other. Therefore I could not write the summation.

I have same problem in the below constraint also. If j changes, the upper bound of the summation also changes. But I can not write the relateed code for this problem. (Note: In the picture upper bound is p(j)-1, but I write it with another parameter pp(j).)
image.png

Hi

sum(j$(ord(j) q), sum(s$(ord(s) < pp(j)-1), ...

I am not sure if you really need to have s defined as a set based on pp(j). If you define s over all possible intervals and use the $-conditions in the summation, all should be fine.

However, you could also use an explicit mapping (look for mapping in the Gams documentation)

set mapS_J(s,j)  Mapping from intervals s to condition j
/1.1, 2.1
* map for criterion 1 the intervals 1 and 2
 1.2, 2.2, 3.2
...;
/

Now you can use the mapping in the sums sum(mapS_J, …).

Hope this helps

Cheers
Renger

Hi,

First of all , thanks a lot for your answers.

In my code, I defined map like below:

mapK_J_S(k,j,s) Allowed combinations of k and j and s /1.1.0 , 1.2.0 , 1.3.0 , 1.4.1 ,2.1.0 , 2.2.0 , 2.3.1 , 2.4.1 , 3.1.0 , 3.2.1 , 3.3.0 , 3.4.0 , 4.1.0 , 4.2.1 , 4.3.0 , 4.4.1 , 5.1.1 , 5.2.0 , 5.3.1 , 5.4.1 , 6.1.1 , 6.2.1 , 6.3.1 , 6.4.0 , 7.1.2 , 7.2.0 , 7.3.0 , 7.4.0 , 8.1.2 , 8.2.0 , 8.3.1 , 8.4.1 ,9.1.2 , 9.2.1 , 9.3.0 , 9.4.1 /

In the mapping s is the r(j,k) values.
And I use the mapping in equation like below:

class1(k)… sum((j,s)$(mapK_J_S(k,j,s)),w(j,s)) - u(‘1’)+ epsplus(k) =g= sigma1;

However, code generates the w(j,s) values for only the specific s value which is defined in map.
For example:
For k= 9 the equation should be w(1,1)+w(1,2)+w(2,1)+w(4,1) - u(‘1’)+ epsplus(k) =g= sigma1;
However the code gives only w(1,2)+w(2,1)+w(4,1) - u(‘1’)+ epsplus(k) =g= sigma1. It did not sum s values from 1 to 2 for j=1.

How can I fix my code? Do you have any idea?

Hi
The element 9.1.1 is not a member of the mapping, so it is not accounted for. You only have 9.1.2 , 9.2.1 , 9.3.0 , 9.4.1, so you should get w(1,2)+w(2,1)+ w(3,0) + w(4,1). It looks like w(3,0) is zero, so it drops out in the listing.
Cheers
Renger

Hi,

The first code is worked :slight_smile: :slight_smile: Thanks a lot for your help :slight_smile: :slight_smile:

And I have one more question. I hope it will be the last one.

For the whole constraint in the picture I write the below code part however for the red squared part give errors.
In this case, I am trying to restict the index (s) of a parameter (w(j,s)) with another parameter (r(j,k))
And also s and t are aliased indexes.

Do you have any idea how can I solve this problem?

class1(k)… sum(j, sum(s$(ord(s)<=r(k,j)-1), w(j,s))) + sum(j,w(j,s)(ord(s)=r(k,j))*((a(j,k)-aa(j,t)(ord(t)=r(j,k)))/(aa(j,t)(ord(t)=r(j,k)-1)-aa(j,t)(ord(t)=r(j,k))))) - u(‘1’) + epsplus(k) =g= sigma1;

Thanks a lot again :slight_smile:
image.png

Hi
Could you post the code or send it tome, so I can reproduce the error and debug it?
Cheers
Renger

Hi
You are on the right track:

sum(j, sum(s$(ord(s)<=r(k,j)-1), w(j,s))) + sum(j,w(j,s)$(ord(s)=r(k,j))*((a(j,k)-aa(j,t)$(ord(t)=r(j,k)))/(aa(j,t)$(ord(t)=r(j,k)-1)-aa(j,t)$(ord(t)=r(j,k)))))  - u('1') + epsplus(k)  =g=  sigma1;

however, you get an error “Uncontrolled set entered as constant” below sum(j,w(j,s)$(ord(s). You should not only sum over j, but also over s, e.g.

  sum((j,s), w(j,s)$(ord(s).

There remain errors (for example, you use a(k,j) although it is declared as a(j,k), but I leave them to you.
Cheers
Renger

Enjoy modeling even more: The lazy economist

Hi Renger,

Thanks a lot for your answer. The model did not give any syntax error after your correction.

But now, the code exits with code 3 which is error for division by 0. However, I checked whole code, the paranthesis, defined parameters and their values many times. Any of them should not cause such a division. And also I write constraints for k=1 and k=3 by hand. The constraint for k=1 is generated properly, while the constraint for k=3, is not generated properly. In the previous answer, you stated there exist some additional error. I think I corrected them, but may be I missed one error. And that error causes wrong constraint generations. The final version of the constraints is below. The only part I changed in the previous code is these constraints.
Could you check them? Is there any other error which I could not recognize?

Thanks a lot again and again.

class1(k)(mapK_I(k,'1')).. sum(j, sum(s(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s),w(j,s)(ord(s)=r(k,j))*((a(k,j)-aa(j,s)(ord(s)=r(k,j)))/(aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j))))) - u(‘1’) + epsplus(k) =g= 0.0001 ;
class2plus(k)(mapK_I(k,'2')).. sum(j, sum(s(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s),w(j,s)(ord(s)=r(k,j))*((a(k,j)-aa(j,s)(ord(s)=r(k,j)))/(aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j))))) - u(‘2’) + epsplus(k) =g= 0.0001 ;
class2minus(k)(mapK_I(k,'2')).. sum(j, sum(s(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s),w(j,s)(ord(s)=r(k,j))*((a(k,j)-aa(j,s)(ord(s)=r(k,j)))/(aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j))))) - u(‘1’) - epsminus(k) =l= -0.0001 ;
class3(k)(mapK_I(k,'3')).. sum(j, sum(s(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s),w(j,s)(ord(s)=r(k,j))*((a(k,j)-aa(j,s)(ord(s)=r(k,j)))/(aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j))))) - u(‘2’) - epsminus(k) =l= -0.0001 ;

I can’t check your code without the initialization of your variables (the code to initialize the variable w for example).
Cheers
Renger

Hi Renger,

I sent my code to you in DM. Could you check it? I tried different index values (for k=1 and k=3) to check the generated constraints but they gave different results. Therefore I could not understand the problem in code and constraints. And also I can not understand the division by zero error. It should not give such an error.

I hope you can understand the problem of code. Please help me.
Thanks a lot again.

Hello again,

I worked on the code again. And I noticed while checking all indices in a constraint; which is j and s; the constraint generates denominator as 0. Therefore, the code gives error of division by zero . To get rid of this problem, I should write the denominator part not equal to zero as a condition. But, I could not write this condition.

I think, the condition should be " aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j)) " part is not equal to zero.

And also this condition will be in below constraint. But I can not place it.

class1(k)(mapK_I(k,'1')).. sum(j, sum(s(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s),w(j,s)(ord(s)=r(k,j))*((a(k,j)-aa(j,s)(ord(s)=r(k,j)))/(aa(j,s)(ord(s)=(r(k,j)-1))-aa(j,s)(ord(s)=r(k,j))))) - u(‘1’) + epsplus(k) =g= 0.0001 ;

How can I write such a condition and where this condition should be in the constraint?

Please, can you help me for this conditional code part?

Thanks a lot.

Hi
You can define a parameter like this:

parameter cond(k,j,s);
cond(k,j,s)= (aa(j,s)$(ord(s)=(r(k
      ,j)-1))-aa(j,s)$(ord(s)=r(k,j)));

and then rewrite the equation as

class1(k)$(mapK_I(k,'1'))..         sum(j, sum(s$(ord(s)<=r(k,j)-1), w(j,s))) + sum((j,s)$cond(k,j,s),w(j,s)$(ord(s)=r(k,j))*((a(k,j)-aa(j,s)$(ord(s)=r(k,j)))/(aa(j,s)$(ord(s)=(r(k,j)-1))-aa(j,s)$(ord(s)=r(k,j)))))  - u('1') + epsplus(k)  =g=  0.0001 ;

Same thing for the other equations.
Cheers
Renger