How to declare variable depends on other variable as a constraint in AMPL? - 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].

Related

Gekko Variable Definition - Primary vrs. Utility Decision Variable

I am trying to formulate and solve an optimization problem based on an article. The authors introduced 2 decision variables. Power of station i at time t, P_i,t, and a binary variable X_i,n which is 1 if vehicle n is assigned to station i.
They introduced some other variables, called utility variables. For instance, energy delivered from station i up to time t for vehicle n, E_i,t,n which is calculated based on primary decision variables and a few fix parameters.
My question is should I define the utility variables as Gekko variables? If yes, which type is more appropriate?
I = 4 # number of stations
T = 24 # hours of simulation
N = 5 # number of vehicles
p = m.Array(m.Var,(I,T),lb=0,ub= params.ev.max_power)
x = m.Array(m.Var,(I,N),lb=0,ub=1, integer = True)
Should I define E as follow to solve these equations as an example? This introduces extra variables that are not primary decision variables and are calculated based on other terms that depend on the primary decision variable.
E = m.Array(m.Var,(I,T,N),lb=0)
for i in range(I):
for n in range(N):
for t in range(T):
m.Equation(E[i][t][n] >= np.sum(0.25 * availability[n, :t] * p[i,:t]) - (M * (1 - x[i][n])))
m.Equation(E[i][t][n] <= np.sum(0.25 * availability[n, :t] * p[i,:t]) + (M * (1 - x[i][n])))
m.Equation(E[i][t][n] <= M * x[i][n])
m.Equation(E[i][t][n] >= -M * x[i][n])
All of those variable definitions and equations look correct. Here are a few suggestions:
There is no availability[] variable defined yet. If availability is a function of other decision variables, then it is generally more efficient to use an m.Intermediate() definition to define it.
As the total number of total decision variables increase, there is often a large increase in computational time. I recommend starting with a small problem initially and then scale-up to the larger sized problem.
Try the gekko m.sum() instead of sum or np.sum() for potentially more efficient calculations. Using m.sum() does increase the model compile time but generally decreases the optimization solve time, so it is a trade-off.

Limit values ​of a variable in Julia JUMP

How can I write a constraint that indicates that my variable can only take values ​​that are multiples of 35?
using JuMP, Gurobi
m = Model(Gurobi.Optimizer)
#variable(m, x>=0, Int)
y = 35x
Now you can use y as any other variable in your optimization model.
If you want to have such constraint on the value of the objective (as the link that points to the deleted question) you could do (I reuse x from the previous example):
#variable(m,z1)
#variable(m,z2)
ob = 3z1 + 2z2
#constraint(m, ob == 35x)
#objective(m, Max, ob)

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;

Summation under null set

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;