if (sum {j in P} y[j] > 0 then A=1 else 0
A is a parameter, and y[j] is a variable
I've tried many times but always show a syntax error.
I appreciate you help
let A := (if sum {j in P} y[j] > 0 then 1 else 0);
or
if sum {j in P} y[j] > 0
then let A := 1;
else let A := 0;
You cannot write
param A = (if sum {j in P} y[j] > 0 then 1 else 0);
as this defines a parameter in terms of a variable. On the other hand you
can legally write
var A = (if sum {j in P} y[j] > 0 then 1 else 0);
to make A's value dependent on y[j], but if this variable is used in an
objective or constraint then attempts to solve the resulting model will be
unsuccessful, as the current solvers can't deal with this kind of function.
Bob Fourer
4...@ampl.com
subject to C {(i,j) in links}:
Y[i,j] = 1 ==> X[i] + X[j] = 2;
where ==> is the AMPL operator for "implies". This works since for two
binary variables, their sum is 2 if and only if they are both equal to 1.
You can also disaggregate this into two separate constraints, Y[i,j] = 1 ==>
X[i] = 1 and Y[i,j] = 1 ==> X[j] = 1; such a formulation might give better
performance, though whether it does so for any particular problem is best
resolved by running some experiments.
For any MIP solver (including CPLEX) you can also reformulate these as
linear constraints:
subject to C {(i,j) in links}:
2 * Y[i,j] <= X[i] + X[j];
since if Y[i,j] is 1 this forces X[i] and X[j] to be 1, while if Y[i,j] is 0
it imposes no additional restrictions on X[i] or X[j]. Again you can
disaggregate this, to Y[i,j] <= X[i] and Y[i,j] <= X[j]. Whether this
linearization would work better or worse than using "==>" is again something
that is best resolved by testing on your particular problem.
Bob Fourer
4...@ampl.com
> -----Original Message-----
> From: am...@googlegroups.com [mailto:am...@googlegroups.com]
> On Behalf Of Melisa
> Sent: Monday, December 12, 2011 11:31 AM
> To: AMPL Modeling Language
> Subject: [AMPL 5304] Re: translate if then sentence into AMPL
>
> Hello, I have a query similar to the above question. I got some
> troubles to write the following constraint. The model use two binary
until here the problem works well, when add the "localization"
constraints the resulting X vector is 0.
The variable descriptions are: if Y[i,j] = 1 the link is in the
shortest path, 0 is not.then I use the binary variable Xj to indicate
if a facilitie is located at node j or not. So, thats the reason
because I want that when Yij = 1 then Xi = 1 and Xj = 1, but I have
problem yet.
I really appreciate your advice very much.
# Model
param numNodes;
set Nodes := 1..numNodes;
set E within {Nodes,Nodes};
param Cost{E};
param demand{i in Nodes};
var Y{E} binary; # if the arc (i,j) is in the shortest path
var X{Nodes} binary; # binary variable for location
#Objective Function
minimize trip: sum{(i,j) in E} Y[i,j] * Cost[i,j];
#Constraint
subject to conservation{i in Nodes}:
sum{j in Nodes: (j,i) in E} Y[j,i] - sum{h in Nodes: (i,h) in
E} Y[i,h]= demand[i];
subject to Indicator1 {i in Nodes, j in Nodes: (i,j) in E}:
Y[i,j] <= X[i];
subject to Indicator2 {i in Nodes, j in Nodes: (i,j) in E}:
Y[i,j] <= X[j];
-----------------------------------------------------------
Finally, I use other option, I replace the constraints Indicador1 and
Indicator1 by:
subject to indicator1{ i in Nodes: i != t}:
sum{j in Nodes: (i,j) in E}Y[i,j] = X[i];
subject to fixedest: X[t]=1;
where t is the destination node and fixed to 1. In this way, it work.
but, I don't no why the first don't work well. I'm interesting in
avoid fixed the destination node to 1 in the model (don't use use
constraint fixedest).
This is the result when using Indicator1 and Indicator2:
Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;
X [*] :=
1 1
2 1
3 1
4 1
;
Thanks for your advises!
The results follows:
Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;
X [*] :=
1 1
2 1
3 1
4 1
;
Yes, I get the same result for the vector X using these data:
# Data
param numNodes = 4;
param demand :=
1 -1
2 0
3 0
4 1;
set E := (1,2) (1,3) (1,4) (2,3) (2,4) (3,4);
param: Cost:=
1 2 0.1513
1 3 0.4518
1 4 0.6279
2 3 0.4513
2 4 0.4518
3 4 0.1513;
And my interest is that only the nodes that are on the shortestpath
are 1. I replace the constraints Indicador1 and
Indicator1 by indicator1 and fixedest (described above) to obtain:
Y :=
1 2 1
1 3 0
1 4 0
2 3 0
2 4 1
3 4 0
;
X [*] :=
1 1
2 1
3 0
4 1
;
but still I don't know why using Indicador1 and Indicator2 the model
don't work as i hope...
Note that in the second formulation one cannot simply set all the
X-variables to 1, as setting any X[j] to 1 forces at least one Y[i,j] to 1
-- which does make a difference to the conservation constraints and the
objective.
It is not entirely clear from your description what are the parameters and what are the variables in your expression. That makes a big difference to how you should write your condition. I imagine that you are looking for something like
sum {k in CITIES: route[i,j,k]>=handling_cost[2]} handling[k,2]
but that works only if route and handling_cost are parameters. For more help, I suggest posting the AMPL definitions of CITIES, route, handling_cost, and handling_cost_city.
Bob Fourer
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.
CITIES is a setrest are parameters.
--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/BlolphCk0Pw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.
--Regards,Sagar Agarwal--------------------------------------------------General SecretaryAerospace Engineering DepartmentIIT Bombay--------------------------------------------------
var X{i in ...}:= Y[i];subject to
A_1{i in ...}:X[i] = Y[i];
or
A_1{i in ...}:X[i] - Y[i] = 0;set I:={'N1','N2','N3','N4'...}; # List of enterprises.var X {i in I}> = 0; # Quantity at each ('N1', 'N2', 'N3', 'N4' ...) facility
var Y {i in I}> = 0; # Quantity in each ('N1', 'N2', 'N3', 'N4' ...) distribution centersubject to A_1 {i in I}: X [i] = Y [i]; # Conditions apply to all i in I ('N1', 'N2', 'N3', 'N4' ...)
if i = j then it is advisable to leave only i
set I:={'N1','N2','N3','N4'...}; # List of enterprises.
Each facility has a distribution center with the same name;
var X {i in I}> = 0; # Quantity at each ('N1', 'N2', 'N3', 'N4' ...) facilityvar Y {i in I}> = 0; # Quantity in each ('N1', 'N2', 'N3', 'N4' ...) distribution center
subject to A_1 {i in I}: X = Y ; # Conditions apply to all i in I ('N1', 'N2', 'N3', 'N4' ...)
On Mon, Nov 9, 2020 at 3:30 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:Thank you for your reply. Let say, X is the quantity in the facility and
Y[j] is the quantity in the distribution center(DC). I want to get the same
quantity in both facility and DC. For example, If the facility chooses
X[1]=20 units then the DC must have to choose Y[1]=20 UNITS. I am getting
the result by writing n number of constraints shown below.
subject to1: X[1]=Y[1];
subject to2: X[2]=Y[2];
subject to3: X[3]=Y[3];
subject to4: X[4]=Y[4];
.
. .......... n.
Here I need to write n number of constraints*. But I want to write these in
one or two constraints AMPL*. I appreciate your kind help.
On Mon, Nov 9, 2020 at 2:41 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hello. If i = j then you can write:
var X{i in ...}:= Y;
or use constraint:
subject to
A_1{i in ...}:X = Y;orA_1{i in ...}:X - Y = 0;
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/73c447d7-f7f1-4709-97ad-fd7c90c71897o%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/bdc25237-df4d-49fe-8bc7-f3b488acb6ean%40googlegroups.com.