--
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/bc82a815-4acc-4cd8-a869-05485f61b5e8o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
for {j in Y, i in X}{
if (i mod 2 = 1 and j = ceil(i/2) ) then
if (A[i, j] = A[i+1, j] and A[i, j] = 1) then {
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
};
} ;for {j in Y, i in X} {
if (i mod 2 = 1 and j = ceil(i/2) ) then
if (A[i, j] = A[i+1, j] and A[i, j] = 1) then
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
else;
}Thank you very much for your assistance on that last part. I have a further question: if I only want to check if the two successive rows in certain columns of a param A = {X,Y} are not both 1 (for example, no two 1s in rows 1 and 2 of column 1, no two 1s in rows 3 and 4 of column 2, etc), then is the logic of the following nested IF correct? Do I need parentheses like ( ) or { } around some of the outer IF...then?
param n;
set X := 1..n ;
set Y := 1....(2*n+1) ;
#generate a random matrix with entries = 0 or 1
param A {X, Y} := floor(Uniform(0,2)) ;
for {j in Y, i in X}{
if (ord(i) mod 2 = 1 and ord(j) = ceil(ord(i)/2) ) then
if (A[i, j] = A[i+1, j] and A[i, j] = 1) then
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
} ;
On Sun, Jun 28, 2020 at 4:43 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I already commented on an earlier version of this question. In this version you can't write "let (A[i, j] = 0 or A[i+1, j]=0" since that does not tell AMPL which assignment to make. (Also, AMPL's "let" statements use the := operator rather than the = operator.) Instead you could randomly assign one or the other element to zero:
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
You need to correct a lot of simple syntax errors as well, such as writing { instead of ( to begin a set expression, adding a semicolon at the end of every statement, etc.
If you have any other questions, respond only to this answer.
--
Robert Fourer
am...@googlegroups.com
On Sun, Jun 28, 2020 at 11:54 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
I am trying to generate a random matrix A = {X, Y} such that each entry of A is either 0 or 1, and no two entries of A in the same column and two successive rows, with the first row is always odd-numbered (e.g. entries in rows 1 & 2, rows 3&4, rows 5&6, etc), are both equal to 1 (they can be both 0 though). Below is my attempt to do this:--param n;set X = 1..n ;set Y = 1....(2*n+1) ;#generate a random matrix with entries = 0 or 1param A {X, Y} := ceil(Uniform(0,2))-1for (j in Y, i in X} {if (ord(i) mod 2 == 1) thenif (A[i, j] == A[i+1, j] and A[i, j] == 1) then let (A[i, j] = 0 or A[i+1, j]=0);} ;display A;Question. Is this code correct to achieve my goal above (especially the nested IF lines within the `for` loop?) I tried putting this into a data file and set param n := 5 : but I keep getting the error message as in the attached file. Can someone please help me overcome this issue, and/or help verify the code above?
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 unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
set X ordered ;
set DONORS ordered ;
set Z := X ordered ;
param ARCS {DONORS, X}; #denote an arc
param n;Â
Now, I have the data file as:
param n := 100000;
set X := 1..n ;Â
set DONORS := 1..2*n ;Â
param ARCS := floor(Uniform(0,2));Â
for {j in X, i in DONORS} {
if (ord(i) mod 2 = 1 and ord(j) = ceil(ord(i)/2)) then
if (ARCS[i, j] = ARCS[i+1, j] and ARCS[i, j] = 1) then {
if Uniform(0,1) <= 0.5 then let ARCS[i, j] := 0;
             else let ARCS[i+1, j] := 0;
} ;
} ;
i kept getting the error in the images, but I don't understand why they were errors. Like, how should we *correctly* declare a two-dimensional table array with random entries of 0 and 1?
To unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
So I ran into a problem that is so weird logically. First, part of my model file is as follows:
set X ordered ;
set DONORS ordered ;
set Z := X ordered ;
param ARCS {DONORS, X}; #denote an arc
param n;
Now, I have the data file as:
param n := 100000;
set X := 1..n ;
set DONORS := 1..2*n ;
param ARCS := floor(Uniform(0,2));
for {j in X, i in DONORS} {
if (ord(i) mod 2 = 1 and ord(j) = ceil(ord(i)/2)) then
if (ARCS[i, j] = ARCS[i+1, j] and ARCS[i, j] = 1) then {
if Uniform(0,1) <= 0.5 then let ARCS[i, j] := 0;
else let ARCS[i+1, j] := 0;
} ;
} ;
i kept getting the error in the images, but I don't understand why they were errors. Like, how should we *correctly* declare a two-dimensional table array with random entries of 0 and 1?
On Tue, Jun 30, 2020 at 1:09 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thank you very much for your help. I have a follow-up question though: keeping the same constraints, if I need to constrain on the number of 1s appearing in matrix A (for example, number of 1 to number of 0 is 1:9 for a given dim(A) = dim(Y) x dim(X) , how can I achieve that? The only possible way I can think of is to build a count variable to count the total number of 1 after a matrix A is randomly generated, and then evaluate the ratio of that to dim(A) = (2n+1)*n to see if the ratio is 0.1 (most likely not). If not, then change some 0s to 1 until the ratio is equal to 0.1. But this seems to be way too difficult to achieve.
For the first step (after A is generated), I can have like:
param rat ;
param fixed_rat := 1/9 ; #pre-defined ratio of 1 to 0
let count := 0 ;
for {i in X, j in Y} {
if(A[i, j] = 1) then count = count + 1;
}
rat = count/(card(X)*card(Y)) ;
if (rat <> fixed_rat) then {how do to this?}
On Mon, Jun 29, 2020 at 12:43 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You can write the loop like this,
for {j in Y, i in X}{ if (i mod 2 = 1 and j = ceil(i/2) ) then if (A[i, j] = A[i+1, j] and A[i, j] = 1) then { if Uniform(0,1) <= 0.5 then let A[i, j] := 0; else let A[i+1, j] := 0; }; } ;
or like this,for {j in Y, i in X} { if (i mod 2 = 1 and j = ceil(i/2) ) then if (A[i, j] = A[i+1, j] and A[i, j] = 1) then if Uniform(0,1) <= 0.5 then let A[i, j] := 0; else let A[i+1, j] := 0; else; }
--
Robert Fourer
am...@googlegroups.com
On Sun, Jun 28, 2020 at 10:58 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Thank you very much for your assistance on that last part. I have a further question: if I only want to check if the two successive rows in certain columns of a param A = {X,Y} are not both 1 (for example, no two 1s in rows 1 and 2 of column 1, no two 1s in rows 3 and 4 of column 2, etc), then is the logic of the following nested IF correct? Do I need parentheses like ( ) or { } around some of the outer IF...then?
param n;
set X := 1..n ;
set Y := 1....(2*n+1) ;
#generate a random matrix with entries = 0 or 1
param A {X, Y} := floor(Uniform(0,2)) ;
for {j in Y, i in X}{
if (ord(i) mod 2 = 1 and ord(j) = ceil(ord(i)/2) ) then
if (A[i, j] = A[i+1, j] and A[i, j] = 1) then
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
} ;
On Sun, Jun 28, 2020 at 4:43 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
I already commented on an earlier version of this question. In this version you can't write "let (A[i, j] = 0 or A[i+1, j]=0" since that does not tell AMPL which assignment to make. (Also, AMPL's "let" statements use the := operator rather than the = operator.) Instead you could randomly assign one or the other element to zero:
if Uniform(0,1) <= 0.5 then let A[i, j] := 0;
else let A[i+1, j] := 0;
You need to correct a lot of simple syntax errors as well, such as writing { instead of ( to begin a set expression, adding a semicolon at the end of every statement, etc.
If you have any other questions, respond only to this answer.
--
Robert Fourer
am...@googlegroups.com
On Sun, Jun 28, 2020 at 11:54 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
I am trying to generate a random matrix A = {X, Y} such that each entry of A is either 0 or 1, and no two entries of A in the same column and two successive rows, with the first row is always odd-numbered (e.g. entries in rows 1 & 2, rows 3&4, rows 5&6, etc), are both equal to 1 (they can be both 0 though). Below is my attempt to do this:--param n;set X = 1..n ;set Y = 1....(2*n+1) ;#generate a random matrix with entries = 0 or 1param A {X, Y} := ceil(Uniform(0,2))-1for (j in Y, i in X} {if (ord(i) mod 2 == 1) thenif (A[i, j] == A[i+1, j] and A[i, j] == 1) then let (A[i, j] = 0 or A[i+1, j]=0);} ;display A;Question. Is this code correct to achieve my goal above (especially the nested IF lines within the `for` loop?) I tried putting this into a data file and set param n := 5 : but I keep getting the error message as in the attached file. Can someone please help me overcome this issue, and/or help verify the code above?
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 unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
Thanks to your great explanation and example file, I was finally able to make it run and solve the optimization problem.
I have two additional questions related to the param ARCS and n in the model file:
1. How to adjust the resulting randomly-generated param ARCS with one additional constraint: the # of 1 to # of 0, in the randomly-generated matrix, satisfies a pre-defined ratio (for example, (# of 1s): (# of 0s) = 1:10) ?? I posted the question and my best attempt already, but I think you were too busy that you overlooked it. Note that this would be placed at the bottom of your Test2.run above.
let fixed_rat := 1/9 ; #pre-defined ratio
let count := 0 ;
for {j in X, i in DONORS} {
if(ARCS[i, j] = 1) then count = count + 1;
}
let rat := count/(card(X)*card(Y)) ;
if (rat <> fixed_rat) then {how do I continue here??}
2. I would like to conduct a simulation on the objective values and cplex solver's time for different values of n (for example, run the current code for n from 500 to 4500 with an incremental step size = 500), how can we do this without changing the entire code in the model file? It seems to me I have to declare set n = 500..T by 500 in model file, but this does not work because n is used in the definition of set X and Y. Rewriting the entire definition of X and Y in terms of the indices of fails to work either.
On Wed, Jul 1, 2020 at 5:06 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:Thanks to your great explanation and example file, I was finally able to make it run and solve the IP!! I cannot believe choosing between the model file and the run files to put the correct syntax, all for the declaration and value initialization purposes, into matters that much in AMPL (?!!) The result obtained is quite surprising (and weird) to me though, but I sincerely appreciate your help on this, Dr. Fourer. Below is what I did in the run file based on your hint:
model chain.mod;
data sample2.dat;
option randseed 0;
let {j in X, i in DONORS} ARCS[i,j] := floor(Uniform(0,2));
for {j in X, i in DONORS} {
if (ord(i) mod 2 = 1 and ord(j) = ceil(ord(i)/2)) then
if (ARCS[i, j] = ARCS[i+1, j] and ARCS[i, j] = 1) then {
if Uniform(0,1) <= 0.5 then let ARCS[i, j] := 0;
else let ARCS[i+1, j] := 0;
} ;
} ;
option solver cplex, solution_round 6;
solve;
option omit_zero_rows 1; #supress all zero-rows
option omit_zero_col 1; #supress all zero-columns
display _solve_time;
Finally, I have two additional questions related to the param ARCS and n in the data file:
1. How to adjust the resulting randomly-generated param ARCS with one additional constraint: the ratio of 1 to 0, in the resulting random matrix, must follow certain pre-defined ratio (e.g. # of 1s/# of 0s = 1:10) ?? I posted the question and my best attempt already, but I think you were too busy that you overlooked it. Note that this would be placed at the bottom of your Test2.run above.
let fixed_rat := 1/9 ; #pre-defined ratio
let count := 0 ;
for {j in X, i in DONORS} {
if(ARCS[i, j] = 1) then count = count + 1;
}
let rat := count/(card(X)*card(Y)) ;
if (rat <> fixed_rat) then {how do I continue here??}
2. I would like to conduct a Monte Carlo simulation on the objective values and cplex solver's time for different values of n (for example, run the current code for n from 500 to 4500 with an incremental step = 500), how can we do this without changing the entire code in the model file? It seems to me I have to declare set n = 500..T by 500 in model file, but this will mess up with the cardinality of set X and set Y in the current working code (because set X:= 1..n, so n should be an integer, rather than a set. But rewriting the entire definition of set X and Y in terms of the indices of n is way too complicated for me, and I am not sure if it works).
3. My current AMPL code runs out of memory usage when n:= 5000 (my laptop is MacAir 2014, and the AMPL's Cplex took 2.5 - 3 hours to solve when n = 4000. The obtained result is straightforward, and the same as other cases with smaller value of n). But I have not tried using all 12 threads available in MacAir (I am not sure how cplex solver solves my Integer Programming problem, and how many threads it uses to solve primal, dual and barrier, if any). Do you think if I specify 'option cplex_options 'concurrentopt,' this would solve the memory overload issue?
For your convenience and concrete reference, attached is my fully working code. I apologize for having so many questions.
On Tue, Jun 30, 2020 at 4:17 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
AMPL expressions are not accepted in data files. To define a set or parameter by an expression, give the expression in the set's definition in the model file instead:
param n;
set X = 1..n ordered;
set DONORS = 1..2*n ordered;
param ARCS {DONORS, X} = floor(Uniform(0,2));
Then you only need to give "param n := 100000;" in the data file. In your situation, however, this is not going to work for ARCS, because you want to change some its values; when you try to execute a statement like "let ARCS[i, j] := 0;" you will get an error like this:
ARCS has an = assignment in the model.
So instead your model should define only
param ARCS {DONORS, X};
and then later you should use "let" to initialize the ARCS values:
let {j in X, i in DONORS} ARCS[i,j] := floor(Uniform(0,2));
for {j in X, i in DONORS} { . . . change some ARCS values . . . }
I put all of this together in the attached file.
--
Robert Fourer
am...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
x [*] :=
p01Â 1
p02Â 1
p03Â 1
p04Â 0
p05Â 1
y [*,*]
: Â p01 p02 p03 p04 p05Â Â :=
d01 Â 0 Â 1 Â 0 Â 0 Â 0
d02 Â 0 Â 1 Â 0 Â 0 Â 0
d03 Â 1 Â 0 Â 0 Â 0 Â 0
d04 Â 1 Â 0 Â 0 Â 0 Â 0
d05 Â 0 Â 0 Â 1 Â 0 Â 0
d06 Â 0 Â 0 Â 0 Â 0 Â 1
d07 Â 0 Â 0 Â 0 Â 0 Â 0
d08 Â 0 Â 0 Â 0 Â 0 Â 0
d09 Â 0 Â 0 Â 1 Â 0 Â 0
d10 Â 0 Â 0 Â 0 Â 0 Â 1
E :=
p03 p04 d05 d07 Â 0
p04 p03 d07 d05 Â 0
To unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
ampl: include sol.run; CPLEX 12.10.0.0: optimal integer solution; objective 4 0 MIP simplex iterations 0 branch-and-bound nodes ampl: display E.slack; E.slack := p03 p04 d05 d07 0 p04 p03 d07 d05 0 ;
On Thu, Jul 2, 2020 at 1:20 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
1. If there are too many 1s, pick an element of ARCS at random, and if it is a 1 that is eligible to be a 0, change it to 0. Repeat until the number of 1s is reduced to the desired number. If there are not enough ones, use the same approach but change 0s to 1s. An AMPL "while" loop can be useful for this purpose; the general outline for the "too many 1s" case would be
while (number of 1s in ARCS is > desired number)
choose random i and j
if ARCS[i,j] = 1 and it is allowed to be zero
then let ARCS[i,j] := 0;
}
2. Increasing n will automatically increase the size of your problem. So instead of setting n in a data file, you could consider a loop like this:
for {size in 500..4500 by 500} {
let n := size;
generate a new ARCS matrix
solve;
record results
}
--
Robert Fourer
am...@googlegroups.com