error in 1 subscript

19 views
Skip to first unread message

sahebe...@gmail.com

unread,
May 16, 2017, 3:37:00 PM5/16/17
to AMPL Modeling Language
Hi all,

How can I solve this problem? I want to define param avg in 2 dimension!! How can I get this? What is the problem with the code below?
Also I want the number of 4 avg equal in number of VS set because I use of this code in 2 dimension! Is it correct???

WBR

set VS:= {1..4};
param a default 2;
param avg{i in VS} default 0;
while (avg[a,i]-avg[a-1,i]>0.01) {
for {i in VS}{
repeat {
let minn[i]:= {j in VCH : distance[i,j] == min{ii in VS} distance [ii,j]};
let avg[a,i]:= (sum {j in minn[i]} X_CH[j] + sum {j in minn[i]} Y_CH[j]) / card(minn[i]);
}
}
let a:=a+1;
}

Robert Fourer

unread,
May 17, 2017, 11:01:44 AM5/17/17
to am...@googlegroups.com
Please see sections 13.2 and 13.3 of the AMPL book (http://ampl.com/BOOK/CHAPTERS/16-script.pdf) for the proper forms of the "for" and "repeat" statements. There is no "while" statement in AMPL.

It appears that you will want to define "param avg {1..a, VS} default 0;" so that you can write avg[a,i] in your AMPL script. Each time that you "let a:=a+1;" the first dimension of avg will increase in size.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Tuesday, May 16, 2017 2:37 PM
To: AMPL Modeling Language
Subject: [AMPL 14040] error in 1 subscript

How can I solve this problem? I want to define param avg in 2 dimension!! How can I get this? What is the problem with the code below?

Also I want the number of 4 avg equal in number of VS set because I use of this code in 2 dimension! Is it correct???

sahebe...@gmail.com

unread,
May 18, 2017, 4:57:54 AM5/18/17
to AMPL Modeling Language, 4...@ampl.com
I realized and end up writing code below. why do not end this code?? What is the problem with the code below?
Do this code get me 4 avg???
please help me!!


set VS:= {1..4};
param a default 2;
param avg {1..a, VS} default 0;

for {i in VS}{       ####     initialization of avg[1]

repeat {
let minn[i]:= {j in VCH : distance[i,j] == min{ii in VS} distance [ii,j]};
let avg[1,i]:= (sum {j in minn[i]} X_CH[j] + sum {j in minn[i]} Y_CH[j]) / card(minn[i]);
}}                     ####     initialization of avg[1]

for {i in VS,a????}{         #####please pay attention to this section

repeat {
let minn[i]:= {j in VCH : distance[i,j] == min{ii in VS} distance [ii,j]};
let avg[a,i]:= (sum {j in minn[i]} X_CH[j] + sum {j in minn[i]} Y_CH[j]) / card(minn[i]);
if (avg[a,i]-avg[a-1,i]>0.01) then break;
let a:=a+1;
}}                      #####please pay attention to this section

Robert Fourer

unread,
May 18, 2017, 12:46:22 PM5/18/17
to am...@googlegroups.com
It seems that you want to write

for {i in VS} {
let a := 2;
repeat {
...

However since you are the author of the code, you will need to do your own testing to see whether it is working as you intend. Note that you can set "option single_step 1;" to go through the commands one at a time, which is useful for debugging; see http://ampl.com/BOOK/CHAPTERS/16-script.pdf#page=14.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Thursday, May 18, 2017 3:58 AM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 14051] error in 1 subscript

I realized and end up writing code below. why do not end this code?? What is the problem with the code below?
Do this code get me 4 avg???

Message has been deleted
Message has been deleted

Robert Fourer

unread,
May 21, 2017, 7:14:26 PM5/21/17
to am...@googlegroups.com
When you define a param to equal an expression, as in

param X_S {0..a,VS} := L*Uniform01();

then the value of the param is determined by that expression in the rest of the model. Thus you cannot assign some other value to the param, and when you try to do that with "let X_S[1,i]:=avg_x[1,i];" you get an error message. If you want to set an initial value for a param that can be changed later, you can use "default" instead:

param X_S {0..a,VS} default L*Uniform01();

Or, just define "param X_S {0..a,VS};" in the model and initialize it later with a "let" statement.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of sahebe...@gmail.com
Sent: Friday, May 19, 2017 6:55 AM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 14065] error in 1 subscript

Thank for your answer. But I have problem yet!!
May you answer this question? Why I receive this error about code below?!

param avg_x {0..a, VS} default 0;
param X_S {0..a,VS }:=( L*Uniform01());
for {i in VS}{
repeat {
let distance [1,i,j]:=sqrt((X_S[1,i]-X_CH[j])^2+(Y_S[1,i]-Y_CH[j])^2);
let X_S[1,i]:=avg_x[1,i];
........}}
THE ERROR= Error at _cmdno 9 executing "let" command
(file Kmeans.mod, line 31, offset 843):

X_S has an = assignment in the model.


Reply all
Reply to author
Forward
0 new messages