GAMS if-else syntax - algorithm

if (a(i,j) gt 0,
z(i,j) eq 0;
else
z(i,j) eq 1;
);
equations
kapasite_asker(i)
kapasite_silah(i,k)
talep_asker1(j)
talep_asker2(j)
talep_silah1(j)
talep_silah2(j)
atama_asker(i)
atama_silah(i)
agirlik(i,j)
risk
amac
;
kapasite_asker(i).. sum(j,a(i,j))=L=y(i)*c(i);
kapasite_silah(i,k).. sum(j,x(i,j,k))=L=m(i,k)*f(i);
talep_asker1(j).. sum(i,z(i,j))=E=1;
talep_asker2(j).. sum(i,a(i,j))=E=g(j);
talep_silah1(j).. sum(i,e(i,j))=E=1;
talep_silah2(j).. sum((i,k),x(i,j,k))=E=sum(k,n(j,k));
agirlik(i,j).. sum(k,x(i,j,k)*v(k))=L=t(i,j);
risk.. sum((i,j),(z(i,j)*r(i,j)+e(i,j)*r(i,j)))=L=25*sum((i,j),z(i,j)+e(i,j));
amac.. sum(i,f(i)+y(i))=E=p;
atama_asker(i).. sum(j,z(i,j))=L=11*y(i);
atama_silah(i).. sum(j,e(i,j))=L=11*f(i);
Error 143
A suffix is missing
Error 141
Symbol declared but no values have been assigned. Check for missing
data definition, assignment, data loading or implicit assignment
via a solve statement.
A wild shot: You may have spurious commas in the explanatory
text of a declaration. Check symbol reference list.
Error 149
Uncontrolled set entered as constant
Error 10
',' expected
Error 140
Unknown symbol
Error 36
'=' or '..' or ':=' or '$=' operator expected
rest of statement ignored
Error 409 in
Unrecognizable item skip to find a new statement
looking for a ';' or a key word to get started again
I'm new to GAMS. The code I wrote gives an error. 'a' in the code is an integer variable and z is a binary variable. How can I fix it? Or how do I connect these two variables? Thanks in advance.
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Since a and z are both variables, the if-else statement is not good to use for their assignment. You can use big-M method and add two constraints for such a logic:
-Mz <= a <= M(1-z) - eps, where M is a positive scalar, and eps is a sufficient small number by default.

Related

Why is x-- a valid ruby statement but it doesn't do anything?

I know ruby doesn't support integer increment x++ or decrement x-- as C does. But when I use it, it doesn't do anything and doesn't throw an error either. Why?
Edit:
Sorry the code I actually found was using --x, which is slightly different, but the question remains: Why?
x = 10
while --x > 0
y = x
end
In Ruby, operators are methods. --x, x++, x==, etc all can do wildly different things. -- and ++ are not themselves valid operators. They are combinations of operators.
In the case of your provided code, --x is the same as -(-x).
If x = 5, then -x == -5 and --x == 5.
---x would be -(-(-x)), and so on.
Similarly, x-- alone on a line is technically valid, depending on what the next line of code contains.
For example, the following method is valid:
def foo
x = 1
y = 10
x--
y
end
The last two lines of that method get interpreted as x - (-y) which calculates to 1 - (-10).
The result doesn't get assigned to any value, so the x-- line would appear to do nothing and the function would just return the result: 11.
You could even have nil on the last line of the function instead of y, and you wouldn't get a syntax error, but you would get a runtime error when the function is called. The error you would receive from x--nil is:
NoMethodError: undefined method `-#' for nil:NilClass
That means that -nil is invalid since nil does not define the method -#. The # indicates that - works as a unary operator. Another way to express --x by invoking the unary operators manually is x.-#.-#
x-- just on its own is not valid. It requires a Numeric object to follow it (or any object which implemented -#). That object can be on the next line. x== would work the same way.
But when I use it, it doesn't do anything and doesn't throw an error either.
It does:
ruby -ce 'x--'
# -e:1: syntax error, unexpected end-of-input
x-- is not valid Ruby syntax.

How to catch the "slice bounds out of range" error and write a handle for it

I read other questions about the "slice bounds of range" here in the stackoverflow, but none of them using the same context from this.
Then, none of the answers helped me.
In my use case, the "golang's syntax" of substring using [] doesn't return an error variable. It launches a runtime error using the "panic" instruction.
My goal is to avoid to reach on the "panic" instruction.
I need to treat this error and provide messages that describe with more details the context around the moment where this error had occurred.
Obs:
The content of the string variable which one I need to get the substring value is totally dynamic and the indexes that I have been using to get the substring value is equally calculated dynamically.
You need to do bounds checking on your indexes:
if j >= 0 && j <= len(str) {
y = str[:j]
}

I don't comprehend why Scilab is giving me "syntax error, unexpected ;"

Well, I got this Newton's Method for multiple variables code that was modified by me to function with 4 variables instead of 2, but I keep getting the error
Syntax error, unexpected ;
referencing line 9 which is the Jacobian matrix from the partial derivatives of the equations on function F. Every " " is another column and every ";" is another line.
clear;clc
function z=F(p1,p2,p3,p4)
z(1)=0.3*(500-p1)^0.5-0.2*(p1-p2)^0.5+0.2*(p1-p3)^0.5
z(2)=0.2*(p1-p2)^0.5-0.1*(p2-p4)^0.5+0.2*(p2-p3)^0.5
z(3)=0.1*(p1-p3)^0.5-0.2*(p3-p2)^0.5+0.1*(p3-p4)^0.5
z(4)=0.1*(p2-p4)^0.5+0.1*(p3-p4)^0.5-0.2*(p4)^0.5
endfunction
function z=J(p1,p2,p3,p4) //Jacobiano
z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p3)) 0;0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3) -0.2/(2*sqrt(p2-p3)) -0.1/(2*sqrt(p2-p4));0.1/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p3-p2)) -0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)) -0.1/(2*sqrt(p3-p4));0 0.1/(2*sqrt(p2-p4)) 0.1/(2*sqrt(p3-p4)) -0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction
The code doesn't end here, but the error is certainly in the format I used in the 4x4 Jacobian matrix or in the function itself, I just don't get why is wrong.
Appreciate any help.
You forgot to close a parenthesis in column 191 of line 9, and you also forgot an operator on column 319.
If you're creating a matrix in which the elements are results of operations, you'd better use commas instead of blank spaces to separate them. You should also be using ... to break the matrix definition in more than one line to improve readability. Your Jacobian function would be a lot easier to debug if it were written like this:
function z=J(p1,p2,p3,p4) //Jacobiano
z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)),...
0.2/(2*sqrt(p1-p2)),...
-0.2/(2*sqrt(p1-p3)),...
0;...
...
0.2/(2*sqrt(p1-p2)),...
-0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3),...
-0.2/(2*sqrt(p2-p3)),...
-0.1/(2*sqrt(p2-p4));...
...
0.1/(2*sqrt(p1-p3)),...
0.2/(2*sqrt(p3-p2)),...
-0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)),...
-0.1/(2*sqrt(p3-p4));...
...
0,...
0.1/(2*sqrt(p2-p4)),...
0.1/(2*sqrt(p3-p4)),...
-0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction
In this style, your mistakes can be easily spotted: notice the missing ) in line 8, and the missing / in line 14.

While loop throwing error at the start of while

I want to take the user's input of a positive integer where 1 < a < 10^6 and run a loop on it and then store it in a matrix which gets printed to the screen. However, my code is throwing a syntax error pointing to the letter "e" in while. Does anyone know why this error is appearing?
A = (while (a!=1)
If(rem(a,2)=0
floor(a^(1/2));
Else
floor(a^(3/2));
endwhile)
disp(A);
You're having several different problems in your code:
a while loop doesn't return anything, so you can't assign it to A
syntax is case sensitive, so it's if and else, not If and Else
you're missing a closing brace after the if clause
you're missing an endif
you're assigning to rem, use == to compare for equality

Go- Why Does My For Loop Throw "Unexpected Semi-colon or New-line"?

I'm writing a dice rolling function. In order to add the result of each die, I added to an output variable using a for loop. However, I'm getting an error thrown when I attempt to build;
syntax error: unexpected semicolon or newline, expecting {
This was thrown on the line initializing the for loop.
Here is my code:
for i := 0; i < [0]si; i++ {
output := output + mt.Intn([1]si)
}
si is simply an int array holding 2 values, and mt is the name I gave to math/rand when I imported it.
Your loop has several problems:
The use of square brackets is odd. Outside of type definitions, these go after slice/array names, e.g. x[i] would give you the ith element of the slice x.
There is no reference to i inside the loop body, and thus each iteration of the loop will do the same thing.
You should probably write output = output + ... without the colon. Otherwise, a new variable output is declared during each iteration of the loop, and forgotten immediately after so that the loop has no effect.
The compiler error is probably caused by the first of these problems.
You access an array with varname[idx] in Go, just like in most other languages. It's only when declaring a new array of type si that you use the [size]si prefix syntax. You're getting the error because your code is syntactically invalid. You're trying to compare i to an array of 0 si s.

Resources