writing AMPL constraint that can give 2 values - ampl

I want to write a constraint that goes
variable[i] is equal to 0 or parameter[i]
does anyone know how to do that?
I'm using AMPL

Something like this should do it:
set S; # your index set
param p{S};
var x{S};
var x_indicator{S} binary;
s.t. c1{i in S}: x[i]=p[i]*x_indicator[i];

Related

Accessing the last element of a variable in GAMS

I have a set:
Set t /t1*t6/;
Let us consider there is a variable called var. I have a constraint that the last element of var is less than 20.
Variable var(t);
Equation const;
const..
var('t6') < 20;
I would like to replace 't6' in the last line by something like card(t), so that if the size of t changes then I do not have to change it manually.
You can use a dollar condition to limit the equation to the last period:
const(t)$(ord(t) = card(t)).. var(t) < 20;
Or you could define a singleton subset for your end condition like so:
singleton set tEnd(t) /t6/;
const.. var(tEnd) < 20;
You could also define an upper bound with the help of the "last" attribute of the set t:
Set t /t1*t6/;
Variable var(t);
var.up(t)$(t.last) = 20;
Best,
Lutz

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].

Sum the binary variables in GLPK

I am new in GLPK. This is some of my code:
set I := setof{(i,r,p,d) in T} i;
var Y{I,I}, binary;
s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;
I want to have only six values in Y that are 1. Can anyone tell me how to do it in proper way? Because s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;always produces an error.
Thank you.
This is just a syntax problem. The constraint should look like the following:
s.t. c1: sum{i in I, j in I}(Y[i,j]) = 6;
The first brackets after the name of your constraints imply that the constraint is applied to every single [I, I]. What you want is to fix the sum of all Y in your problem, so you need the constraint to only apply once to your problem (so delete these brackets).
In the sum-syntax don't put the variable you want to sum in the brackets, they belong after them. Inside the brackets you can define the range of the sum.

Initialize 3 dimension variable in AMPL

I have a variable called Rest defined as:
var Rest{I,J,T} >= 0;
where T is the set of time periods and I and J the arcs. I need to define that every value for I and J where T = 0 must be 0. I is the set of supply nodes, and J the set of demand nodes.
I've tried:
let Rest[*,*,0] default 0;
but it got me syntax error. I tried this in both the .dat and .mod file using both := and :
I also tried to put this in the .dat file
var Rest default 0:=
[*,*,0] 1 City1 0;
but it gave me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11443):
error processing constraint Constraint1[1,'Leveaniemi',1]:
invalid subscript Rest[1,'City1',0]
Thanks in advance!
EDIT:
I now use:
var Rest default 0 :=
[*,*,0] 1 Leveaniemi 0;
which give me the error
Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11438):
error processing constraint Constprocessing commands.
Executing on neos-3.neos-server.org
Error (2) in /opt/ampl/ampl -R amplin
(I am using NEOS server, Gurobi solver). What does this even mean? Also if I declare a Variable Rest like that will it cause every Rest solution to become 0? Or does the compiler interpret it as a start value?
EDIT:
I've tried to implement the solution provided by vitaut. It did not work however, as expressed in the comments below that reply. I figured that since I've defined T as:
set T := 1 2 3 ... 15;
and since I wanted to do a let statement at t = 0, I have to account for that and define Rest as:
var Rest{I,J,TimeT};
where TimeU is T union a set with only a 0 element, i.e. TimeU is interpreted as:
TimeU := 0 1 2 3 ... 15;
With these fixed however, the compiler complains that all my variables and parameters are already defined.
The correct syntax of a let command is
let {i in I, j in J} Rest[i, j, 0] := 0;
However, it will assign starting values to the variables which can change during the optimization process. If you want to make Rest[i, j, 0] always equal to zero, then you should use a constraint instead:
s.t. c{i in I, j in J} Rest[i, j, 0] = 0;

negative values in integer programming model

I'm new at using the glpk tool, and after writing a model for certain integer problem and running the solver (glpsol) i get negative values in some constraint that shouldn't be negative at all:
No.Row name Activity Lower bound Upper bound
8 act[1] 0 -0
9 act[2] -3 -0
10 act[2] -2 -0
That constraint is defined like this:
act{j in J}: sum{i in I} d[i,j] <= y[j]*m;
where the sets and variables used are like this:
param m, integer, > 0;
param n, integer, > 0;
set I := 1..m;
set J := 1..n;
var y{j in J}, binary;
As the upper bound is negative, i think the problem may be in the y[j]*m parte, of the right side of the inequality.. perhaps something with the multiplication of binarys? or that the j in that side of the constrait is undefined? i dont know...
i would be greatly grateful if someone can help me with this! :)
and excuse for my bad english
thanks in advance!
Sounds like you have an overflow problem. Which values of m and n produced the output shown?