parameter def on dependent set

588 views
Skip to first unread message

Harvey Greenberg

unread,
Feb 25, 2011, 1:39:42 PM2/25/11
to AMPL Modeling Language
Sorry for such an elementary ques, but I cannot find right syntax for:

set A := 1..2;
set B := 1..2;
set C := 1..2;
set D{C} within A cross B default A cross B;
param f{D} default 1;

...I would like to continue with something like:
subject to constr{k in C}: sum{(i,j) in D[k]} f[i,j)*x[i,j] <=
rhs[k];

I get the error defining param as:
D needs to be subscripted.
context: param >>> f{D} <<< default 1;

I tried variations, including a for loop, which ampl does not allow in
defining params. How can I do this?

Robert Fourer

unread,
Feb 25, 2011, 5:34:48 PM2/25/11
to am...@googlegroups.com, Harvey Greenberg
Hi Harvey,

Since D is actually a collection of sets indexed over C, it's necessary to
specify the indexing of f (and x) as {k in C, D[k]}. Then f and x will have
3 subscripts and the constraint will look like this:

set A := 1..2;
set B := 1..2;
set C := 1..2;
set D{C} within A cross B default A cross B;

param rhs {C};
param f{k in C, D[k]} default 1;
var x {k in C, D[k]} >= 0;

subject to constr {k in C}:

sum {(i,j) in D[k]} f[k,i,j]*x[k,i,j] <= rhs[k];

Bob Fourer
4...@ampl.com

Harvey Greenberg

unread,
Feb 25, 2011, 6:07:08 PM2/25/11
to AMPL Modeling Language
I was afraid of that. The x must be original dimens. What I'm trying
to do is establish soln-exclusion constraints on binary variables,
where sets depend on previous solns. card(C) is actually the number
of solutions I want to obtain, and D[k] is the exclusion covering-
constraint with rhs[k]=1. (Inactive constraints that have not been
generated yet have rhs[k]=0.) My main script loops over C, solving a
0-1 MILP each time. I thus will get alternative optima first, then a
collection ordered by obj value. Is there some way to do this with
ampl?

Paul

unread,
Feb 26, 2011, 12:51:33 PM2/26/11
to am...@googlegroups.com
Harvey,

I posted an example in AMPL on my blog (http://orinanobworld.blogspot.com/2011/02/finding-multiple-solutions-in-binary.html).  Does that help?

Cheers,
Paul

Harvey Greenberg

unread,
Feb 26, 2011, 1:09:19 PM2/26/11
to AMPL Modeling Language
Thanks Paul. That does it. (I need to use params, rather than sets.)

On Feb 26, 10:51 am, Paul <parubi...@gmail.com> wrote:
> Harvey,
>
> I posted an example in AMPL on my blog (http://orinanobworld.blogspot.com/2011/02/finding-multiple-solutions-...).  

Prof. Dr. Michael Krätzschmar

unread,
Feb 27, 2011, 4:06:32 AM2/27/11
to am...@googlegroups.com
Hi Paul,
 
why (http://orinanobworld.blogspot.com/2011/02/finding-multiple-solutions-in-binary.html) do you have four capacities for only two knapsacks:
 
set KNAPSACKS := 1 2;
param capacity := 1 6 2 4;
 
Michael
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to am...@googlegroups.com.
To unsubscribe from this group, send email to ampl+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ampl?hl=en.

Robert Fourer

unread,
Feb 27, 2011, 9:58:05 AM2/27/11
to am...@googlegroups.com, Harvey Greenberg
The kind of loop that Paul wrote could also be done with sets, which permit
a more concise description (though which you prefer is a matter of taste).
Your model could be something like

param nSols integer default 0;
param maxSols integer > 0;

set A;
set B;
set Pos {1..nSols} within {A,B};

var x {A,B} binary;

subject to Exclude {k in 1..nSols}:
sum {(i,j) in Pos[k]} (1-x[i,j]) +
sum {(i,j) in {A,B} diff Pos[k]} x[i,j] >= 1;

along with your objective and constraints and any other sets and parameters
you need to describe them. Then the loop can have the form

repeat {
solve;
# <test & examine the solution>
let nSols := nSols + 1;
let Pos[nSols] := {i in A, j in B: x[i,j] > .5};
} until nSols = maxSols;

Similarly to Paul's loop, the number Exclude constraints -- and in this
case, also the number of Pos sets -- increases by one each time nSols is
stepped, which happens each time a new solution is found. To specify each
Exclude constraint properly it suffices to assign the new Pos set to contain
the indices of all the 0-1 variables that were 1 in the latest solution.

Bob Fourer
4...@ampl.com


Robert Fourer

unread,
Feb 27, 2011, 10:24:31 AM2/27/11
to am...@googlegroups.com

It only appears this way because the knapsacks are numbered.  The meaning of "param capacity := 1 6 2 4;" in this case is that capacity[1] is 6 and capacity[2] is 4.

 

Bob Fourer

4...@ampl.com

 

 

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

On Behalf Of Prof. Dr. Michael Krätzschmar [kraetz...@mathematik.fh-flensburg.de]
Sent: Sunday, February 27, 2011 3:07 AM
To: am...@googlegroups.com
Subject: Re: [AMPL 4387] Re: parameter def on dependent set

Harvey Greenberg

unread,
Feb 27, 2011, 4:37:03 PM2/27/11
to 4...@ampl.com, am...@googlegroups.com
Thanks Bob.  I see what I did wrong, and both solutions work.

Harvey
Reply all
Reply to author
Forward
0 new messages