warm start in AMPL API

36 views
Skip to first unread message

mahraz amini

unread,
Apr 22, 2018, 9:47:45 AM4/22/18
to AMPL Modeling Language
Hi all

suppose I have a variable 

var Pcharge  {BUS , Time};


and


set BUS := {1 2};

set Tiem := { 1 2 3};


I am wondering in AMPL API how I can give a matrix like Z as an initial solving point to the solver


Z = [ 1 2 3 ; 4 5 6];


Thanks.

mahraz amini

unread,
Apr 22, 2018, 1:27:14 PM4/22/18
to AMPL Modeling Language
I should add that in the ampl api documentation it is said that I can do this like

    ampl.eval('var x;');
    x = ampl.getVariable('x');
    x.setValue(4);

so when I do the same I get this error:

    Error using VariableBase/setValue (line 296)
    No method 'setValue' with matching signature found for class 'com.ampl.Variable'.

    Error in Entity/subsref (line 73)
                    builtin('subsref',self,key);

Can anyone tell me how I can fix this please?

Thanks.

AMPL Google Group

unread,
Apr 23, 2018, 6:42:51 AM4/23/18
to Ampl Modeling Language
Regarding your first question, you can provide an initial solution as follows:
BUS = {1, 2};
Time = {1, 2, 3};
df = DataFrame(2, 'BUS', 'Time', 'Pcharge');

Z = [1 2 3 ; 4 5 6]
df.setMatrix(Z, BUS, Time);
ampl.setData(df)
ampl.eval('display Pcharge;')

Regarding your second question, we were not able to reproduce this issue. Are you using the latest version of the API?
>> ampl = AMPL()

>> ampl.eval('var x;');
>> x = ampl.getVariable('x');
>> x.setValue(4);
>> ampl.eval('display x;')
x = 4

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:566357284-5856#}
--
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.



mahraz amini

unread,
Apr 23, 2018, 9:16:02 AM4/23/18
to AMPL Modeling Language
Hi

thanks for you response. let me clear a little bit what I said before. the example that I posted works fine

ampl = AMPL()
ampl.eval('var x;');
x = ampl.getVariable('x');
x.setValue(4);
ampl.eval('display x;')
x = 4


 but when instead of the one variable I used vector variable it can not do the same

ampl.eval('var x {1..4};')
x = ampl.getVariable('x');
x.setValue([1 2 3 4])
Error using VariableBase/setValue (line 296)
No method 'setValue' with matching signature found for class 'com.ampl.Variable'.


Error in Entity/subsref (line 73)
                    builtin('subsref',self,key);


so is there any way that we can initialize variables with size more than 1, in the same way?

Thanks

Mahraz

AMPL Google Group

unread,
Apr 23, 2018, 1:24:00 PM4/23/18
to Ampl Modeling Language
According to the documentation, the correct method for indexed entities would be setValues instead of setValue. However, Variable.setValues is apparently missing from the current implementation of the API and a new release is necessary to fix this issue. Nevertheless, you can do the same by creating a DataFrame with two columns, one for the index and another for the values (named after the variable you want to set), and set the variable values with ampl.setData as follows:

ampl.eval('var x {1..4};');
index = {1; 2; 3; 4};
values = {-5; -9; 2; 24};
df = DataFrame(1, 'index', 'x'); % the second column is named after the variable you want to set
df.setMatrix(values, index)
ampl.setData(df)


Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:566357284-5856#}
On Mon, Apr 23, 2018 at 1:16 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Hi

mahraz amini

unread,
Apr 23, 2018, 3:14:54 PM4/23/18
to AMPL Modeling Language
thank you very much for your response. 

if instead of a vector of size 4, we have a vector for example with size of 1000, then I wonder if there is a way to avoid writing {1;2;3;4;5;6;7;8;9;...;1000} and same for the values.

Thanks,

Mahraz

AMPL Google Group

unread,
Apr 24, 2018, 7:43:19 AM4/24/18
to Ampl Modeling Language
You can use the MATLAB function num2cell (see, e.g., https://www.mathworks.com/help/matlab/ref/num2cell.html) to convert an array to a cell array and do the following:

ampl.eval('var x {1..1000};');
index = num2cell(1:1000);
values = num2cell([-4, -3, -2, -1, ...]);

df = DataFrame(1, 'index', 'x');
df.setMatrix(values, index)
ampl.setData(df)

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com
{#HS:566357284-5856#}
On Mon, Apr 23, 2018 at 7:15 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
thank you very much for your response.

if instead of a vector of size 4, we have a vector for example with size of 1000, then I wonder if there is a way to avoid writing {1;2;3;4;5;6;7;8;9;...;1000} and same for the values.

Thanks,

Mahraz



On Mon, Apr 23, 2018 at 5:23 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
According to the documentation, the correct method for indexed entities would be setValues instead of setValue. However, Variable.setValues is apparently missing from the current implementation of the API and a new release is necessary to fix this issue. Nevertheless, you can do the same by creating a DataFrame with two columns, one for the index and another for the values (named after the variable you want to set), and set the variable values with ampl.setData as follows:

ampl.eval('var x {1..4};');
index = {1; 2; 3; 4};
values = {-5; -9; 2; 24};
df = DataFrame(1, 'index', 'x'); % the second column is named after the variable you want to set
df.setMatrix(values, index)
ampl.setData(df)

Best regards,
Filipe

--
Filipe Brandão
am...@googlegroups.com


Reply all
Reply to author
Forward
0 new messages