Separate the objective and constraints into loops

21 views
Skip to first unread message

Yongning Zhao

unread,
Jan 20, 2017, 11:34:15 AM1/20/17
to AMPL Modeling Language
Hi everyone, I have a high dimensional formulation, I want to solve the objectives separately with their corresponding constraints in a for loop. To make it clear to you, I simplify the original formulation:

param a {i in 1..N};
param b
{i in 1..N};

minimize fun
: sum { i in N } (x[i]-y[i]);

subject to x_upper
{ i in N }: x[i]<=a[i];
subject to y_upper
{ i in N }: y[i]<=b[i];



Now I want to break up the sum function and solve the objective function separately for each i, as follows:


minimize fun
[1]: x[1]-y[1];
subject to x_upper
: x[1]<=a[1];
subject to y_upper
: y[1]<=b[1];



minimize fun
[2]: x[2]-y[2];
subject to x_upper
: x[2]<=a[2];
subject to y_upper
: y[2]<=b[2];



minimize fun
[3]: x[3]-y[3];
subject to x_upper
: x[1]<=a[3];
subject to y_upper
: y[3]<=b[3];
.....
......

minimize fun
[N]: x[N]-y[N];
subject to x_upper
: x[N]<=a[N];
subject to y_upper
: y[N]<=b[N];

I know how to separate the objective function and solve them in a loop, the model file is as follows

param a {i in 1..N};
param b
{i in 1..N};

minimize fun
{i in 1..N}:  (x[i]-y[i]);

and then write a script as follows:

for{i in 1..N}{
objective fun
[i];
solve
;
}

But how to separate the constraints? How to make sure that in each loop only the constraints corresponding to index i are invoked?

Robert Fourer

unread,
Jan 21, 2017, 10:11:47 AM1/21/17
to am...@googlegroups.com
You can use AMPL's "named problem" feature for this situation. For example, define

problem P {i in 1..N}: x[i], y[i], fun[i], x_upper[i], y_upper[i];

and then solve with

for {i in 1..N} {
problem P[i];
solve;
}

Optionally you can combine "problem P[i]; solve;" into "solve P[i];". For a complete description of named problems see sections 14.4 and 14.5 in the AMPL book (beginning at http://ampl.com/BOOK/CHAPTERS/17-solvers.pdf#page=30).

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Yongning Zhao
Sent: Friday, January 20, 2017 10:34 AM
To: AMPL Modeling Language
Subject: [AMPL 13404] Separate the objective and constraints into loops

Hi everyone, I have a high dimensional formulation, I want to solve the objectives separately with their corresponding constraints in a for loop. To make it clear to you, I simplify the original formulation:

param a {i in 1..N};
param b {i in 1..N};

minimize fun: sum { i in N } (x[i]-y[i]);

subject to x_upper { i in 1..N }: x[i]<=a[i];
subject to y_upper { i in 1..N }: y[i]<=b[i];

.......

Yongning Zhao

unread,
Jan 23, 2017, 9:22:05 AM1/23/17
to AMPL Modeling Language, 4...@ampl.com
Thank you, Robert, I have solved my problem.

在 2017年1月21日星期六 UTC+1下午4:11:47,Robert Fourer写道:
Reply all
Reply to author
Forward
0 new messages