How can I fix Error 149 in Gams?(Uncontrolled set entered as constant) - gams-math

I have this situation in GAMS:
sets
i index of resource location /i1*i6/
j index of disaster location /j1*j7/
;
...
binary variable x(i,j);
parameter
M(j) /j1 5,j2 4,j3 6,j4 7,j5 6,j6 2,j7 1/
ch(i) /i1 10,i2 5,i3 10,i4 15,i5 6,i6 12/
;
...
equations
...
co8(i)
;
co8(i)..M(j)=l=sum(j,ch(i)*x(i,j));
and co8(i) give me the error 149 Uncontrolled set entered as constant.
I searched,but I did not find solution.
How can I fix it?
thanks

The j in M(j) is not controlled. So, it depends on what you want to do, how to fix this. E.g. if you want a sum over all j, you should add that sum (sum(j,M(j))). Or do you want this equation for every j? Then adjust the declaration and definition accordingly.

Related

Solving GAMS optimization for a specific range within a set

I'm trying to solve an optimization problem on GAMS, but only for a subset. Right now, i have a code that optimizes for all the elements of set t:
t time in hours / 1 * 8760 /
How do I run the optimization only for t from 1 to 3500, AND from 6500 to 8760 (for example)? FYI, all of my parameters data (from .csv) that i imported to GAMS have 8760 values. Here's a simple objective function to use:
eObjFunc.. vZ =e= sum(t, v1(t));
The simplest way I could think of is to edit all my .csv data so they only have the t values that I care for. In this case, define set t = 1 to 3500, 6500 to 8760. Then, just run the optimization.
However, I'm sure there's a smarter way to do this without having to modify my .csv data...
Really appreciate your help!
You can define subsets like this (assuming t is defined already as 1*8760):
Set tt(t) /1*3500, 6500*8760/;
And then use tt instead of t in your equation:
eObjFunc.. vZ =e= sum(tt, v1(tt));

GAMS- Domain Violation for set

I got two types of error in my GAMS code.
1- error **** 171 Domain violation for set
2- error 148, Dimension different - The symbol is referenced with more/less
**** indices as declared
I don't know where I am making a mistake.
Px1 is my equation name, representing the power of x, so represent as "px"
Px(x,t,w) is defined variable
Sets:
d2 'number of extreme points in the feasible region' /d2*d2/;
coeff 'combination coefficient of the feasible region of CHP unit' /a*e/;
x ’chp units’ /x1*x1/;
t ’time periods’ /t1*t24/;
w ’scenarios’ /w1*w20/;
Px1(x,t,w).. Px(x,t,w)=e=sum((d2),Px(x,d2)*coeff(x,t,w,d2));
Px1(x,t,w).. Px(x,t,w)=e=sum((d2),Px(x,d2)*coeff(x,t,w,d2));
You use Px here in two different ways: First, Px(x,t,w), which seems to match your declaration, but then as part of the sum as Px(x,d2), which must be wrong and seems to cause both error messages. That should probably be a different symbol?

How do I sum up over an uncontrolled set?

I have to develelop a mcp model using GAMS. I am not quite experienced with this program but the error compilation website was not quite helpful either.
I have tried varying the indices but this would only change the error from uncontrolled set to controlled set. I did also try using the Alias function but either I did it wrong or it did not work at all.
Variables
lambda(p) shadow price
;
POSITIVE VARIABLES
R(t,p) production
S(t,p) Stock at time t
;
RES_resource_lambda(p)..
-(SUM(t, R(t,p)) - S(t,p)) =g= 0
;
The problem is about the last t in the equation:
RES_resource_lambda(p)..
-(SUM(t, R(t,p)) - S(t,p)) =g= 0
;
The t in R(t,p) is controlled by the SUM, but nothing controls the t in S(t,p). You need to specify, what you want to do with this one, e.g. add it to the SUM as well.

Gnuplot summation issue

I am trying to make a plot of a simple mutation accumulation process on a binary tree...
My technical problem in gnuplot, is that is that I want to plot the possibility of getting 2 mutations on a specific lineage on the graph, here is the equation which determines it:
P_{2 mutation} = sum[k=0:n] (m/(2**(k+1)/(1-(1/2)**k)))(1-exp(-muk))
(dont bother with the formula im not sure that this is the correct one yet :))
where n is the number of levels of the binary tree, mu is the mutation rate, and m is the number of previously randomly thrown mutations onto the graphs edges...
I want to make a plot which is this possibility depending on the levels of the binary tree...
Therefore I wrote a script which is something like this:
set term pngcairo size 800,600
set title "Két mutáció megjelenésének valószínűsége, egy n szintű bináris fa egyik sejtvonalában"
set xlabel"szintek száma (n)"
set ylabel"Két mutáció megjelenésének valószínűsége (P_{2^{lin})"
set xrange[1:10]
set yrange[0:1]
set output '2mutvalsz.png'
set multiplot
do for[i=1:10]{
mu = 0.1+(i*0.1)
m = 4
f(x)=(x/((2**(x+1))*(1-(0.5)**x)))
if(m<floor(f(x)))
{
p(x)=sum [k=0:floor(x)](m*(1/((2**(x+1))*(1-(0.5)**x))))*(1-exp(-mu*k))
}
else
{
p(x)=1
}
plot p(x) lt i lw 1
}
unset multiplot
set output
So my problem is, that I dont know if it is correct to do what I do in the
if statement...
What I want is to behold the statement m< f(x) where f(x) is the number of edges in respect of n, which is an integer value therefore I use floor(f(x)), and sum through the x values (which are the number of levels what has to be an integer too... so floor(x), like a heavyside function to make the x axis discrete) in the sum...
And also I get an error message:
gnuplot> load '2mutvalsz.plt'
line 27: undefined variable: x
where line 27 is the end of the do for loop...
So my question is that is it a correct way to make a summation integer the x values and of course why I get the error message...
Thank you, and I hope everything is clear...
The error message is produced because the if statement in your script is interpreted when Gnuplot loads the script - it tries to evaluate the condition of the if statement and since the variable x is not defined, it produces the mentioned message.
You could put everything together using the ternary operator as:
p(x)=( m<floor(f(x)) )?( sum [k=0:floor(x)](m*(1/((2**(x+1))*(1-(0.5)**x))))*(1-exp(-mu*k)) ):1;
However, since the function f(x) is on the imposed x-range of [0,1] less than 1, the condition m<floor(f(x)) will be always false.

fitting a number within two bounds

I'm working on a program that generates pseudorandom numbers for a user based on their inputted seed, start and end range. I've written my own modulus based generator based on Lehmer's random number generator algorithm. YES I KNOW modulus based random calculations are biased, but for it's use this method is more than adequate.
Anyway, whilst I can generate a string of random numbers from the given seed in VBA, I can't find anything online with a formula or code showing how that number can be scaled down to fit within the supplied upper and lower bound. I'm hoping someone here knows a formula for this, or knows of a website I've missed that covers this sort of process (I don't even know what it would be called - scaling?)
Thanks for your time! In case it's useful or anyone's interested, here's my VBA code generating the seed-based number:
random = ((CDec(1664525) * t1) * seed + 1013904223) 't1 is the incremental count for each requested number
random = random - (Int(random / 2 ^ 23) * 2 ^ 21)
Thanks for your help!
EDIT: Just to point out, the 'scaling' cannot use the rand function, which I've seen done before, since the final numbers need to be the same each time that seed is used!
#Kevin is right I just need to add:
Linear interpolation for range change
so if you have number x on interval <x0,x1>
and want to change it to y on interval <y0,y1>
then use this formula:
y=y0+((x-x0)*(y1-y0)/(x1-x0));
it is the formula for 2D line and also base for DDA algorithms ...
What if your x range is unknown ?
then simply bound it to something known
for example x&65535 will change the x range to <0,65535>
of coarse only if the original x range was higher then that ...
What if dynamic x range is smaller then dynamic y range ?
ie |x1-x0|<|y1-y0|
the equation still works but you will be missing certain numbers in y range
so the interval will have gaps
to avoid that you have to increase effective range of x
for example like this x=(rand()&255)|((rand()&255)<<8)
so you will use more random numbers per each call
do not worry the seed stuff will be still working ...