How can I give an initial solution to AMPL

1,483 views
Skip to first unread message

Mahdi Moeini

unread,
Feb 6, 2009, 10:28:07 AM2/6/09
to am...@googlegroups.com
 
Hi everybody,
 
I want to use AMPL in order to solve a Mathematical program with a logarithmic objective function. The AMPL code of the problem is as follows:

#=======================================================
 set I;
 param n > 0;
 param A{I,I};
 param B{I,I};
 var x{I}, >= 0;
 minimize LogObj: log(sum{i in I, j in I} x[i]*B[i,j]*x[j]) - log(sum{i in I, j in I} x[i]*A[i,j]*x[j]);
 subject to Constr: sum{i in I} x[i] = 1;
 
 data;
 set I :=  1  2  3  4  5  ;
 param n := 5;
 
 param A :   1    2    3    4    5   :=
        1    1    2    2    2    2 
        2    2    5    6    6    6 
        3    2    6    9    10   10 
        4    2    6    10   13   14 
        5    2    6    10   14   17 
;
 param B :   1    2    3    4    5   :=
        1    1    0    0    0    0 
        2    0    1    0    0    0 
        3    0    0    1    0    0 
        4    0    0    0    1    0 
        5    0    0    0    0    1 
;
#=======================================================
 
The code is correct and AMPL solves the problem. But I want to convey any initial solution (starting point) that I like to the solver.
How can I do it? What would be the command for the following initial solution (for example):
x :=(  1.0 ,  0.0 ,  0.0 ,  0.0 , 0.0 )
Can I insert the commande(s) in the above model? if yes, where exactly must I write it?
 
Thank you in advance,
Mahdi MOEINI.
 
 

Robert Fourer

unread,
Feb 7, 2009, 9:49:54 AM2/7/09
to am...@googlegroups.com, Mahdi Moeini

You can specify initial values for a variable in the same way that you specify values for a parameter, following "data;".  For example you could add to your data the statement

 

   var x := 1 1  2 0  3 0  4 0  5 0 ;

 

or you could give the members of the set I together with the values for the variables in one statement:

 

   var: I: x :=

1  1

2  0

3  0

4  0

5  0 ;

 

I see also that you define param n and give it the value 5, but do not use it in the model.  If you want the set I to be the integers from 1 to n, then you can declare it by

 

   param n integer > 0;

   set I = 1..n;

 

and then you do not have to give values for I in the data.

 

Bob Fourer

4...@ampl.com

 

 


From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of Mahdi Moeini [moeini...@gmail.com]
Sent: Friday, February 06, 2009 9:28 AM
To: am...@googlegroups.com
Subject: [AMPL 2211] How can I give an initial solution to AMPL

lopez.ca...@gmail.com

unread,
Aug 18, 2017, 7:23:58 AM8/18/17
to AMPL Modeling Language
Hello Dr. Fourer,

How can I do it with AMPL API interface?
I need to provide an initial solution of a variable of 11×168 dimension.

Thanks for your help.

Filipe Brandão

unread,
Aug 19, 2017, 10:11:37 AM8/19/17
to AMPL Modeling Language
With the API you can provide an initial solution by creating a DataFrame with a column named after the variable whose initial value you want to set. For instance, using the Python API you can do this as follows:
from amplpy import AMPL, DataFrame
ampl
= AMPL()
ampl
.eval('var x{1..11, 1..168};')
df
= DataFrame(('i','j'),('x'))
n
, m = 11, 168
for i in range(1, n+1):
   
for j in range(1, m+1):
        df
.addRow(i, j, 10)
ampl
.setData(df)
This code will initialize x[i,j] with the value 10 for every i,j. For other languages, please consult the documentation of the corresponding API.

Best regards,
Filipe

Carlos J Lopez

unread,
Aug 20, 2017, 9:48:19 PM8/20/17
to am...@googlegroups.com

Thanks Filipe.

I used the code below (Matlab) and it worked fine:

===================================

df = DataFrame(2, 'GENS', 'PERIODS', 'u');
for i=1:10
    for t=1:168
        df.addRow(i,t, U(i,t));
    end
end
ampl.setData(df);

==================================

However, when I read the variable before the  solve command, I don't see the provided values. Is this the expected behaviour?

Kind regards,

Carlos 

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/6hnr16t7Rew/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Filipe Brandão

unread,
Aug 21, 2017, 7:00:39 AM8/21/17
to AMPL Modeling Language
Hi Carlos,

I tested the code bellow with a recent AMPL version (20170724) and the values were updated after ampl.setData(df);. Are you using an older AMPL version? You can check your AMPL version with "ampl -v" or with "option version;" in the AMPL prompt.
U = ones(10, 168);
ampl
.eval('var u{1..10,1..168};');

df
= DataFrame(2, 'GENS', 'PERIODS', 'u');
for i=1:10
 
for t=1:168
    df
.addRow(i,t, U(i,t));
 
end
end

ampl
.eval('display {i in 1..10, j in 1..168} u[i,j].val;'); %# displays 0's
ampl
.setData(df);  
ampl
.eval('display {i in 1..10, j in 1..168} u[i,j].val;'); %# displays 1's

Best regards,
Filipe

Walton P. Coutinho

unread,
Apr 11, 2019, 3:52:18 PM4/11/19
to AMPL Modeling Language
Hi Filipe,

Would you know what to do when we want to set up an initial guess 
for a single variable?

Let's say I have the declaration below in my .mod file:

var x;

How to define an initial guess for x using a dataFrame?

Many thanks!

AMPL Google Group

unread,
Apr 11, 2019, 5:07:30 PM4/11/19
to Ampl Modeling Language
Hi Walton,

Since in that case x is a scalar variable you do not need a dataframe. You case set the initial value for x as follows:

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

Best regards,
Filipe

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

Walton P. Coutinho

unread,
Apr 12, 2019, 1:32:22 PM4/12/19
to AMPL Modeling Language
Hi Filipe!

Many thanks for your prompt reply. Cheers!

Abraço,
Walton
To unsubscribe from this group and stop receiving emails from it, send an email to am...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages