Summation under null set - gams-math

I have a few collection ,and the intersection of these collections gives me a few new collection.
I want to have summation under these intersections , but some of these are null. And I get an error for my summation, for example
Set I/1*3/;
Set j/1*3/;
Set s(I,j)
1.(2,3)
2.(1,3)
3.(2);
Alias (I,i1,j);
Set intersection (I,i1,j);
Intersection (I,i1,j)= s(I,j)*s(i1,j);
Variable x(j) ,z;
Binary variable x;
Equation c1,c2;
C1(I)..sum(j$s(I,j),x(j))=e=z;
C2(I,i1)..sum(j$ intesection(i,I1,j),x(j))=g=1;
Model test /all/;
Solve test using lp minimizing z;
I have error for constraint 2 because intersection(2,3) is null ,and I have 0> 1
How can I write this summation?

I do not really understand, what you model here, but this way it runs without an error (there is still no feasible solution though because of equation C1('3')):
Set I/1*3/;
Set j/1*3/;
Set s(I,j) / 1.(2,3)
2.(1,3)
3.(2) /;
Alias (I,i1);
Set intersection (I,i1,j);
Intersection (I,i1,j)= s(I,j)*s(i1,j);
Variable x(j) ,z;
Binary variable x;
Equation c1,c2;
C1(I).. sum(j$s(I,j),x(j))=e=z;
C2(I,i1)$sum(j$ Intersection(i,I1,j),1)..
sum(j$ Intersection(i,I1,j),x(j))=g=1;
Model test /all/;
Solve test using mip minimizing z;

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;

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;

How to set up simple two domain constraint on a variable in Pyomo?

I am working on a supply chain optimisation problem using Pyomo and I need to set up a constraint on specific variables in the model. The constraint is that the variable should be within the set (0,1) or (200, to infinity). However, when I try to set up that constraint I am getting a TypeError, here is my code:
def rail_rule(model):
for route in routes:
if "rail" in route[0].lower():
dest = route[0]
dg = route[1]
sg = route[2]
site = route[3]
return model.x[dest, dg, sg, site]>=200 or model.x[dest, dg, sg, site]<=1
model.railconst = Constraint(rule = rail_rule)
I get this error when I run it :
TypeError: Relational expression used in an unexpected Boolean context.
The inequality expression:
200.0 <= x[RAIL - KENSINGTON,8,8,BROCKLESBY]
contains non-constant terms (variables) that were evaluated in an
unexpected Boolean context at
File '<ipython-input-168-901363ebc86f>', line 8:
return model.x[dest, dg, sg, site]>=200 or model.x[dest, dg, sg, site]<=1
Evaluating Pyomo variables in a Boolean context, e.g.
if expression <= 5:
is generally invalid. If you want to obtain the Boolean value of the
expression based on the current variable values, explicitly evaluate the
expression using the value() function:
if value(expression) <= 5:
or
if value(expression <= 5):
So my understanding is that I cant give Pyomo a boolean expression as a constraint, but I am quite new to Pyomo and not too sure if that's what my issue is or if I am doing it correctly.
This constraint could also be implemented in the variable intialisation as boundaries, but I cant find a way to set up two boundaries on a single variable in Pyomo.
Thanks !
There are different ways to handle this:
(1) Use binary variables. Assume you have a good upper bound on x, i.e., x ∈ [0, U]. Then formulate the constraints
x ≤ 1 + (U-1) δ
x ≥ 200 δ
δ ∈ {0,1} (binary variable)
This is the easiest way.
(2) If you don't have a good upper bound on x, you can use a SOS1 set. (SOS1 means Special Ordered Set of type 1). Assume x,s1,s2 ≥ 0.
x ≤ 1 + s1
x ≥ 200 - s2
s1,s2 ∈ SOS1 (s1,s2 form a SOS1 set)
(3) Use disjunctive programming.

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

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;

How to declare variable depends on other variable as a constraint in AMPL?

How to declare variable depends on other variable as a constraint in AMPL?
I'm trying to solve the minimize the difference between "maximum number of variable - minimum number of variable"
So, my objective equation is
minimize max{t in 0..T}production[t] + min{t in 0..T}production[t];
(t is index and T is time periods parameter and production is decision variable.)
However, it is not linear algebra.
Thus, I'm trying to declare 'max{t in 0..T} production[t]' as a variable 'y'.
So, I would like to write 'var y >= all production'.
But it's not working.
The constraint
s.t. max_production{t in 0..T}: y >= production[t];
will ensure that y is greater or equal than production[t] for all t in 0..T. If you minimize y then it will be exactly max{t in 0..T} production[t].