Syntax error + ORIG is not a set

199 views
Skip to first unread message

fatemeh.h...@gmail.com

unread,
Apr 24, 2018, 3:58:29 PM4/24/18
to AMPL Modeling Language
Hi,

I'm new to AMPL, I have tried to define my model like this as myModel.mod:

set k := 2;
set e := 0.1;
set C := 1;
set L:= 42;

set ORIG ordered;
set DEST ordered;
set ODs = {member(i, ORIG), member(j, DEST)};

set PATHS := 0..k;
set LINKS := 0..L;
set INDICATOR = {l in LINKS, p in PATHS, ij in ODs: indicator[l,p,ij]>=0};
set MEAN_DEMAND; 
set VARIANCE_DEMAND;

var alpha >= 0;
var x{ij in ODs,p in PATHS} >= 0, <= 1;
var y{ij in ODs,l in LINKS} >= 0, <= 1;
var epsilon{l in LINKS} >= 0 , <= 1;

minimize objective: alpha;
s.t. Flow_Reservation {ij in ODs}:
     sum {p in PATHS} x[ij,p] = 1;
s.t. y_ik {l in LINKS, ij in ODs}:
     sum {p in PATHS} x[ij,p]*indicator[l,p,ij] = y[ij,l];
s.t. E2E {ij in ODs, p in PATHS}:
     sum {l in LINKS} indicator[l,p,ij]*epsilon[l] <= e;
s.t. Allocation {l in LINKS}:
     2*log(1/epsilon[l])*(sum {ij in ODs} member(ij,var_demand)*member(ij,var_demand)*y[ij,l]*y[ij,l]) <= (alpha*C-(sum{ij in ODs} member(ij,mu_demand)*y[ij,l]))*(alpha*C-(sum{ij in ODs} member(ij,mu_demand)*y[ij,l]));


I am using amplpy (python API) to solve my model. In python code, I have define a dataframe for ORIG:

            df = DataFrame('ORIG')
            df.setColumn('ORIG', Origins)            
            ampl.setData(df, 'ORIG')
Origins is a 1 dimensional simple array of numbers. I get following error, I appreciate any help.

Traceback (most recent call last):
  File "myModel.py", line 518, in <module>
    main(len(sys.argv), sys.argv)
  File "myModel.py", line 437, in main
    ampl.setData(df, 'ORIG')
  File "C:\Program Files\Python36\lib\site-packages\amplpy\ampl.py", line 629, in setData
    self._lock
  File "C:\Program Files\Python36\lib\site-packages\amplpy\utils.py", line 14, in lock_and_call
    result = call()
  File "C:\Program Files\Python36\lib\site-packages\amplpy\ampl.py", line 628, in <lambda>
    lambda: self._impl.setData(data._impl, setName),
RuntimeError: file -
line 1 offset 10
ORIG is not a set





Thanks,
Fatemeh


AMPL Google Group

unread,
Apr 25, 2018, 7:11:07 AM4/25/18
to Ampl Modeling Language
Hi Fatemeh,

We were not able to reproduce this issue. The following code works as expect:
from amplpy import AMPL, DataFrame
ampl = AMPL()
ampl.eval('''
''')


df = DataFrame('ORIG')
df.setColumn('ORIG', [1, 2, 3, 4, 5])
ampl.setData(df, 'ORIG')
ampl.eval('display ORIG;')

The output is the following:
Error: syntax error
Error: syntax error
Error: syntax error
Error: syntax error
Warning: i is not defined
Error: ORIG is already defined
Error: syntax error
Error: syntax error
Warning: indicator is not defined
Error: syntax error
Warning: indicator is not defined
Error: y is already defined
Warning: indicator is not defined
Error: syntax error
Error: syntax error
set ORIG := 1 2 3 4 5;

Please note that you have many syntax errors in your model, and they should be fixed before using the API.

Additionally, considering that you just want to set the values for a set, it may be worth it to use one of the following alternative methods:
ampl.set['ORIG'] = [1, 2, 3, 4, 5]
or
ampl.getSet('ORIG').setValues([1, 2, 3, 4, 5])

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:567977726-6043#}
--
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 https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



fatemeh.h...@gmail.com

unread,
Apr 25, 2018, 3:41:57 PM4/25/18
to AMPL Modeling Language
Hi,

You are right, I have corrected my syntax errors. I am using the model in a loop and solve the problem with different inputs.
But the problem is that parameters and sets hold the previous values. I want to reset data. How can I do that?
reset command will reset and delete everything which doesn't work for me. I just want to use the model in a loop with different data set.

Thanks,
Fatemeh

AMPL Google Group

unread,
Apr 26, 2018, 8:09:15 AM4/26/18
to Ampl Modeling Language
You can use the command "reset data" (see, e.g, page 209 of https://ampl.com/BOOK/CHAPTERS/14-command.pdf) as follows:

ampl = AMPL()
ampl.eval('''
set S;
param p{S};
''')
values = [
{1: 10, 2: 15, 3: 20},
{9: 3, 5:1},
{1: 1, 2:2, 10:10},
]
S = ampl.getSet('S')
p = ampl.getParameter('p')
for d in values:
ampl.eval('reset data S, p;')
S.setValues(d.keys())
p.setValues(d)
ampl.eval('display p;')


Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:567977726-6043#}

fatemeh.h...@gmail.com

unread,
Apr 26, 2018, 1:05:54 PM4/26/18
to AMPL Modeling Language
Thank you Filipe.

Fatemeh
Reply all
Reply to author
Forward
0 new messages