Hi All,

I would like to know what is the ampl equivalent of the following:

case: i = 1 to N-1

{

 XXX; 
 XXX;

}

case i = N

{

 XXX;
 XXX;

}

case i = N+1 to Z

{

 XXX;
 XXX;

}

Thanks in advance

asked 25 Jan '12, 01:28

Pavan's gravatar image

Pavan
3002621
accept rate: 0%


I'm not sure about the availability of "case" or "switch" statements in AMPL. Meanwhile, if your conditions for selecting commands are solely based on numbers, you might use multiple "for" or "repeat" statements.

link

answered 25 Jan '12, 03:20

Ehsan's gravatar image

Ehsan ♦
4.8k31122
accept rate: 16%

Thanks for responding @Ehsan. What I did was,

for{i in 1..Z}

{

if(i<N-1)

{

XXX; XXX;

}

if(i := N)

{

XXX; XXX;

}

if(i>N && i<=Z)

{

XXX; XXX;

}

}

Still I get an error with second 'if' condition saying that one is a syntax error. Not sure what might have gone wrong here?

(25 Jan '12, 03:30) Pavan

got it! in the second if loop, it should be "==" and not":="

(25 Jan '12, 03:37) Pavan
1

@Pavan: I think you need "then" after each condition in a "if" statement. You should also use "else if" instead of multiple "if"s structure to avoid unnecessary condition checkings.

(25 Jan '12, 03:54) Ehsan ♦

@Ehsan: yeah I have then after if condition its just I forgot to type. I will make it "else if" -- thanks for that.

(25 Jan '12, 04:19) Pavan
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×127
×29
×28

Asked: 25 Jan '12, 01:28

Seen: 1,727 times

Last updated: 25 Jan '12, 04:19

OR-Exchange! Your site for questions, answers, and announcements about operations research.