how to seperate a long line of ampl code into 2 short lines

33 views
Skip to first unread message

Tingting

unread,
Oct 7, 2016, 9:46:24 AM10/7/16
to AMPL Modeling Language
As the titles reads.
And can we color some parts of ampl codes?
Thanks  :)

Robert Fourer

unread,
Oct 8, 2016, 9:05:20 AM10/8/16
to am...@googlegroups.com
You can write an AMPL statement on any number of lines. For example, here is a constraint written on four lines:

subject to dreq {p in prod, t in first+1..last}:
Rprod[p,t] + Oprod[p,t] + Short[p,t] - Short[p,t-1]
+ sum {a in 1..life} (Inv[p,t-1,a] - Inv[p,t,a])
= dem[p,t] less iil[p,t-1];

The statement ends when the ; is reached.

Some editors can use color in displaying AMPL models; the AMPL IDE (http://ampl.com/products/ide/) does some coloring and we are working on an update that may use more colors. However the AMPL language processor does not pay any attention to the colors.

Bob Fourer
am...@googlegroups.com

=======
Message has been deleted

Robert Fourer

unread,
Oct 14, 2016, 7:48:23 PM10/14/16
to am...@googlegroups.com
The "no constraints or objectives" message appears when your AMPL model does not have any "minimize" or "maximize" or "subject to" statements in it. For example if your objective is a minimization, your .mod file could say

minimize Obj: sum {s in SetofNc, y in Block, m in Sectorofvessel,
b in Berth:originofsectorvessel[m] == origin[s]
and b <= berthamount-amountofberthoccupied[origin[s]]+1}
abs (locationberth[k] + locationofsectorvessel[m] - (locationblock[j] ) )* Store[s,y,m,b];

This appears to be a linear objective, so you are also going to need some constraints. Maybe they are also not being specified correctly with "subject to" statements in the .mod file.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of ???
Sent: Friday, October 14, 2016 9:45 AM
To: am...@googlegroups.com
Subject: Re: [AMPL 12738] how to seperate a long line of ampl code into 2 short lines

I run .mod and .dat flies and then got this error:
"No constraints or objectives"
Is it because some of my constraints and objectives are not linear?
take my objective for example.

.......

in .mod file, i code it as follows:

sum {s in SetofNc, y in Block, m in Sectorofvessel, b in Berth:originofsectorvessel[m] == origin[s]
and b <= berthamount-amountofberthoccupied[origin[s]]+1}
abs (locationberth[k] + locationofsectorvessel[m] - (locationblock[j] ) )* Store[s,y,m,b];

Do you know what should i do to get my objective workable in ampl?


Tingting

unread,
Oct 18, 2016, 9:08:51 AM10/18/16
to AMPL Modeling Language, 4...@ampl.com
Hi Bob,
I believe I coded them in the same way you suggest here. So by far the same error still stays.
In order to not bother you too much, I juts use t parts of my codes as follows as an example.
 --------------------- 
# OBJECTIVE
# ---------------------
minimize total_distance:
  sum {s in SetofNc, j in Block, m in Sectorofvessel, 
   k in Berth:originofsectorvessel[m] == origin[s] and k <= berthamount-amountofberthoccupied[origin[s]]+1} 
     abs (locationberth[k] + locationofsectorvessel[m] - (locationblock[j] + 0.5 * blockwidth) )* Store[s,j,m,k]
 + sum {i in Vessel, k in Berth} abs (locationberth[k] + 10-locationblockRc) * Startberthvessel[i,k] * demandRc[i];
# ---------------------
# CONSTRAINTS
# --------------------- 
subject to singlestartservicetimestep {i in Vessel}: 
   sum {t in Timestep: t <= timeamount-servicehour[i]+1 and t >= arrivaltime[i]} Startservicetimestep[i,t] = 1;  
   sum {j in Block, m in Sectorofvessel, k in Berth:originofsectorvessel[m] == origin[s] 
       and k <= berthamount-amountofberthoccupied[origin[s]]+1} Store[s,j,m,k] = 1;     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
All the parameters and variables has been defined already and Im using AMPL IDE. In its Console, I did these steps.
1  I input "model file.mod;"
    I got 1 warning"12.mod, line 3 (offset 38):Vessel is already defined  context:  set  >>> Vessel; <<<". 
    This line 3 is the first code in mod file. Does this mean just this line 3 has been defined?
2 I ignored this warning and input"data file.dat;" 
   I got 9 simliar feedback to say "12.dat, line 2 (offset 25): data for Vessel already read context:  set  >>> Vessel <<< := 0 1 2 3; ".
    in my dat flie,I have more than 9 data to read. Does this mean just these 9 has been read?
3 I ignored all these 10 warning, I input"solve;"
    then I got "No constraints or objectives."
--------------------------------------------------------------------------------------------------------------------------------------------

Robert Fourer

unread,
Oct 19, 2016, 9:27:07 AM10/19/16
to am...@googlegroups.com
You need to fix the error that is causing the "already defined" warnings first, because they may be causing the "no objectives or constraints" error.

Be sure to give the "reset;" command before "model file.mod;" so that previous definitions from your sessions are cleared. Then if you still see

Vessel is already defined context: set >>> Vessel; <<<"

try posting your model file to this forum, by attaching it to your email to am...@googlegroups.com.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Tingting
Sent: Tuesday, October 18, 2016 8:09 AM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 12761] how to seperate a long line of ampl code into 2 short lines

I believe I coded them in the same way you suggest here. So by far the same error still stays.
In order to not bother you too much, I juts use t parts of my codes as follows as an example.

.......
Reply all
Reply to author
Forward
0 new messages