Assign Parameter Values to 2D Array Python

163 views
Skip to first unread message

Eric Geerts

unread,
Feb 23, 2022, 10:19:36 AM2/23/22
to AMPL Modeling Language
Hi,

I hope you are well.

I have parameters in my model which I declare and assign values to with ease:
param types; param classSize;
ampl.getParameter('type').set(3)
ampl.getParameter('classSize').set(6) However with my 2d parameter: param learn{i in 1..classSize, k in 1..types}; I have 6 x 3 array of values (shown in attached screenshot). When I try to assign the values with: ampl.getParameter('learn').set(arr) I get the error: TypeError: Wrong number or type of arguments for overloaded function 'Parameter_set'.
Possible C/C++ prototypes are: ampl::Parameter::set(ampl::Variant) ampl::Parameter::set(double) ampl::Parameter::set(char const *)
ampl::Parameter::set(ampl::Tuple,ampl::Variant) When i use setValues or set_values I get an error with no message. How do I assign the 2d paramter values using an array? Thanks for the help. SIncerely,
Eric Geerts
Screen Shot 2022-02-22 at 11.00.28 PM.png

Eric Geerts

unread,
Feb 23, 2022, 6:15:10 PM2/23/22
to AMPL Modeling Language
This is solved, please close.

For the future, you use ampl.param['learn'][6,3] = 1
You need to index with [x,y] not [x][y]
 

AMPL Google Group

unread,
Feb 24, 2022, 10:14:49 AM2/24/22
to AMPL Modeling Language
Hi Eric,

Parameter.set is for scalar parameter and Parameter.setValues is for indexed parameters. You mentioned, however, that with setValues it had not worked without showing any error, but the following should have worked:

from amplpy import AMPL
ampl = AMPL()
ampl.eval(r'''
param types; param classSize;

param learn{i in 1..classSize, k in 1..types};
''')
ampl.getParameter('types').set(3)
ampl.getParameter('classSize').set(5)
ampl.getParameter('learn').setValues(list(range(15)))
ampl.eval('display learn;')

The output is:

learn :=
1 2 1
1 3 2
2 1 3
2 2 4
2 3 5
3 1 6
3 2 7
3 3 8
4 1 9
4 2 10
4 3 11
5 1 12
5 2 13
5 3 14
;

And the following is equivalent:

from amplpy import AMPL
ampl = AMPL()
ampl.eval(r'''
param types; param classSize;

param learn{i in 1..classSize, k in 1..types};
''')
ampl.param['types'] = 3
ampl.param['classSize'] = 5
ampl.param['learn'] = list(range(15))
ampl.eval('display learn;')

You can also assign values using a dictionary as follows:

from amplpy import AMPL
ampl = AMPL()
ampl.eval(r'''
param types; param classSize;

param learn{i in 1..classSize, k in 1..types};
''')
ampl.param['types'] = 3
ampl.param['classSize'] = 5
ampl.param['learn'] = {(i+1, j+1): i*j for i in range(5) for j in range(3)}
ampl.eval('display learn;')

The output is:

learn :=
2 2 1
2 3 2
3 2 2
3 3 4
4 2 3
4 3 6
5 2 4
5 3 8
;


--
Filipe Brandão
am...@googlegroups.com
{#HS:1796410758-108811#}
On Wed, Feb 23, 2022 at 11:15 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
This is solved, please close.

For the future, you use ampl.param['learn'][6,3] = 1
You need to index with [x,y] not [x][y]

On Wed, Feb 23, 2022 at 3:19 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hi,

I hope you are well.

I have parameters in my model which I declare and assign values to with ease:
param types; param classSize;
ampl.getParameter('type').set(3)
ampl.getParameter('classSize').set(6) However with my 2d parameter: param learn{i in 1..classSize, k in 1..types}; I have 6 x 3 array of values (shown in attached screenshot). When I try to assign the values with: ampl.getParameter('learn').set(arr) I get the error: TypeError: Wrong number or type of arguments for overloaded function 'Parameter_set'.
Possible C/C++ prototypes are: ampl::Parameter::set(ampl::Variant) ampl::Parameter::set(double) ampl::Parameter::set(char const *)
ampl::Parameter::set(ampl::Tuple,ampl::Variant) When i use setValues or set_values I get an error with no message. How do I assign the 2d paramter values using an array? Thanks for the help. SIncerely,
Eric Geerts
--
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/53708479-36dc-4cfa-acb3-40eae3c3a73dn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages