How to set the lower and upper bounds of a single variable in a single equation in gams - gams-math

Is this possible?:
boundary_of_x..
19.0 =l= x =g= 22.1;
where x is a positive variable and boundary_of_x is an equation. Or do I have to do this in two equations?

You cannot do this in one equation, you need two, or (better) use the .lo and .up attribute of the variable:
x.lo = 19.0; x.up = 22.1;

Related

Inline index addition in gams

I want to use an index equation to iterate over a tensors, whereas I always want to extract the value at index i and index i+1. An example:
Variable x; x.up = 10;
Parameter T /1=1,2=2,3=3,4=4,5=5/;
Set a /1,2,4/;
equation eq(a); eq(a).. x =g= T[a+1];
*x ist restricted by the values of T at the indices 2,3 and 5.
Model dummy /all/;
solve dummy min x use lp;
I am aware that gams sees the indices as string-keys rather than numerical ones, so the addition is not intended. Is this possible anyway? This e.g. can be solved by defining another tensor, unfortunaly my given conditions require the index operation inline (i.e. I am not allowed to define additional parameters or sets.
Does this work for you?
Variable x; x.up = 10;
Set aa /1*6/;
Parameter T(aa) /1=1,2=2,3=3,4=4,5=5/;
Set a(aa) /1,2,4/;
equation eq(a); eq(a(aa)).. x =g= T[aa+1];
*x ist restricted by the values of T at the indices 2,3 and 5.
Model dummy /all/;
solve dummy min x use lp;

Interpret the LP optimization result from Gurobi

I want to use Gurobi to solve for a very simple LP:
minimize z
s.t. x + y <= z
where x, y, z are decision variables generated by gp.Model().addVar() which should be the default variable. The objective of the model is set to be m.setObjective(1.0*z, GRB.MINIMIZE).
Then I solved the model, and the program returns the optimal value for z is 0.000. I don't understand why this is the optimal value? Is there any constraint on the default decision variables of Gurobi, like they are non-positive. Otherwise, why 0.0 is the optimal value for this LP when x, y, and z are unbounded?
The convention for Gurobi and other LP/MIP solvers are that decision variables have a lower bound of zero. If you want another lower bound, then either set the LB attribute, or define it when you call Model.addVar(), ex:
m = Model()
x = m.addVar(lb=-20, name='x')

GAMS modelation: how do i set an identifier as the last value of a set (index.last) on an equation

I'm modeling a VRP in GAMS language and one of my equations would ideally be:
SUM(i, x(i,n+1)) =e= 0;
with "n+1" being the last value of the set i /0*4/ (so it's 4)
I can't type x(i,"4") because this number (4) is just an example.
The software doesn't recognize this equation. the error says "unknown identifier set as index, which i understand is because "n" isn't a set.
so i put n as a set, just like i did with i, but then I'd have to give it a number (3, so that n+1 = 4) and i don't want that.
I just need a way to put "n+1" as a valid index for x(i,n+1)
Assuming that x is declared as x(i,i), you can do something like this:
Alias (i,i2);
Equation eq;
eq.. SUM((i,i2)$i2.last, x(i,i2)) =e= 0;

GAMS: Setting one variable to be equal to the second smallest of another vector variable

SET i /i1 * i10/ ;
VARIABLES
x(i)
y
;
I have an optimization (mip) problem where i need my control variable y to be equal to the second smallest number of x. How can I create an equation to do that?
EQUATIONS
myconstraint ;
myconstraint .. y =E= (second smallest element of x) ;

Using the sum function in GAMS to sum over a subset of variables

I am working with maximazation problems in GAMS where I will choose
X=(x_1,x2,...,x_n) such that f(X)=c_1*x_1+...c_n*x_n is maximized. The c's are known scalars and I know n (10 in my case). I want my constraints to be such that the first (n-1)=9 x's should sum up to one and the last one should be less than 10. How do I use the sum to do so?
This is what I have tried:
SET C / c1 .... c2 /;
ALIAS(Assets,i)
Parameter Valuesforc(i) 'C values'/
*( here are my values typed in for all the C1)
POSITIVE VARIABLES
x(i);
EQUATIONS
Const1 First constraint
Const1 Second constraint
Obj The Object;
* here comes the trouble:
Const1 .. x(10) =l= 10
Const2 .. sum((i-1),x(i)) =e= 1
The code is not done all the way but I believe the essential setup is typed in. How do you make the summation to find x_1+x_1 + .... x_(n-1) and how do you refer to x_10?
Try this:
Const1 .. x('10') =l= 10;
Const2 .. sum(i$(ord(i)<card(i)),x(i)) =e= 1;
Edit: Here are some notes to explain what happens in Const2, especially in the "$(ord(i) < card(i))" part.
The "$" starts a condition, so it excludes certain elements of i from the sum (see: https://www.gams.com/latest/docs/UG_CondExpr.html#UG_CondExpr_TheDollarCondition)
The operator ord returns the relative position of a member in a set (see: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheOrdOperator)
The operator card returns the number of elements in a set (see: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_TheCardOperator)
So, all in all, there is a condition saying that all elements of i should be included in the sum except for the last one.