Dear all,
Let's say I have a parameter check(r). For some r it has assigned values and these values can be 0. For other r, check(r) has no assigned values.
For example, imagine that we have a set r with four elements 1*4 and the parameter check(r) is defined as following:
check(r) /1=0.1, 2=0, 3=-5/;
I want to use this parameter to define the condition "if check(r) has assigned value then...". The condition "check(r) ne 0" does not work for me, because the desired condition should return True even for check('2'). The only False value shoud be for check('4). How can I do it in GAMS?
Check that a parameter has an assigned value Topic is solved
Re: Check that a parameter has an assigned value
Default value for any parameter is 0. Therefore, there is no such thing as unassigned value when it comes to parameters.
You can make use of the nature of your data (if your data is positive, initialize your parameter to a negative value and use that as an indicator) or by changing zeros in your data to eps. Or by creating a subset that can keep track of assigned value.
- Atharv
You can make use of the nature of your data (if your data is positive, initialize your parameter to a negative value and use that as an indicator) or by changing zeros in your data to eps. Or by creating a subset that can keep track of assigned value.
- Atharv
Re: Check that a parameter has an assigned value
Thank you, abhosekar. I have created a map to overcome this issue.
How replasing 0 with EPS can help? The condition "ne 0" is False for both zero and EPS values. Is there a way to distinguish 0 and EPS?
How replasing 0 with EPS can help? The condition "ne 0" is False for both zero and EPS values. Is there a way to distinguish 0 and EPS?
Re: Check that a parameter has an assigned value
You are right, eps was a wrong choice. something relatively small like 1e-4 (not eps) would work. The point is to be able to distinguish it from data. Mapping is also a good idea.
- Atharv
- Atharv
Re: Check that a parameter has an assigned value
For numerical comparison eps and 0 behave identically. Eps is present in the data, 0 is not, so the following code hopefully explains this:
You can also check for eps with the mapval function (https://www.gams.com/latest/docs/UG_Par ... _22_mapVal). The following example assigns random integers and makes the zero to eps (by always adding eps). The assignment to count_eps counts how often we got an eps using the mapval function:
BTW, you can turn 0 into EPS in data statements (and when reading from GDX) with $onEps (https://www.gams.com/latest/docs/UG_Dol ... _21_option):
Hope this helps,
-Michael
Code: Select all
set i / i0*i2 /; parameter p(i) / i0 eps, i1 1, i2 2 /;
scalar card_p;
$eolcom !
card_p = card(p); display card_p; ! gives 3
card_p = sum(i$p(i),1); display card_p; ! gives 3
card_p = sum(i$(p(i)<>0),1); display card_p; ! gives 2
p('i0') = 0;
card_p = card(p); display card_p; ! gives 2
card_p = sum(i$p(i),1); display card_p; ! gives 2
card_p = sum(i$(p(i)<>0),1); display card_p; ! gives 2
Code: Select all
set i / i1*i50 /; parameter p(i);
p(i) = uniformInt(-5,5) + eps;
scalar count_eps;
count_eps = sum(i$(mapval(p(i))=mapval(eps)), 1)
display count_eps;
Code: Select all
$eolcom !
$onEps
Parameter check(*) /1=0.1, 2=0, 3=-5/;
display check; ! gives "1 0.100, 2 EPS, 3 -5.000"
-Michael
Re: Check that a parameter has an assigned value
Thanks a lot, bussieck. Your reference to mapval helps a lot.
So what I was looking for can be formulated as the condition: $(check(i) ne 0 or mapval(check(i)) eq 8). Certainly, the assigned zeroes in my parameter should be denoted as EPS to distinguish them from the unassigned values:
So what I was looking for can be formulated as the condition: $(check(i) ne 0 or mapval(check(i)) eq 8). Certainly, the assigned zeroes in my parameter should be denoted as EPS to distinguish them from the unassigned values:
Code: Select all
Parameter check(*) /1=0.1, 2=EPS, 3=-5/;
Re: Check that a parameter has an assigned value
Your "$(check(i) ne 0 or mapval(check(i)) eq 8)" is equivalent to just "$check(i)". -Michael