Conditional min/max operator

369 views
Skip to first unread message

John Brouwer

unread,
Mar 26, 2012, 1:44:37 PM3/26/12
to ai...@googlegroups.com
Hello,
 
In a model that I am trying to make I have a parameter Distance(p1,p2) and variable Place(p2,t) that is binary. Now, in my objective variable I want to choose the smallest Distance(p1,p2) for a given p1 but only for the instance where Place(p2,t) is 1 for that p2 and a given t.
 
Is it in any way possible to do something like this: min(p2,Distance(p1,p2)*Place(p2,t)>0)
 
If not, I would like to make another variable called Activeplace(p2,t) that is 1 when Place(p2,t) = 1 and inf when Place(p2,t) = 0 so I can take the minimum very straightforward. How should I enter this in the definition of the variable? I have been trying to find the correct syntax for this but I cannot find it.
 
Thanks in advance.
 
Kind regards,
John Brouwer.

Luis Pinto

unread,
Mar 27, 2012, 3:11:09 AM3/27/12
to ai...@googlegroups.com
Hello,

A very common way to model min/max function in Linear Programming is to create a new variable that will represent that min/max.

If understood correctly you want a variable that is indexed over p1 and t, say x(p1,t).

If you use the constraint:

x(p1,t) >= sum(p2, Distance(p1,p2)*Place(p2,t))

and in your objective function use

Minimize x(p1,t)

The model should solve looking for the smallest Distance(p1,p2) where Place(p2,t)
But this only solves your problem if Place(p2,t) is 1 for 1 index of p2.

If not, you may have to combine more than one variable to get what you are looking for.

Hope it helps,

Cheers,

Luis Pinto

www.unisoma.com.br



--
You received this message because you are subscribed to the Google Groups "AIMMS - The Modeling System" group.
To view this discussion on the web visit https://groups.google.com/d/msg/aimms/-/ApOOiVc6sw0J.
To post to this group, send email to ai...@googlegroups.com.
To unsubscribe from this group, send email to aimms+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/aimms?hl=en.

John Brouwer

unread,
Mar 27, 2012, 1:59:14 PM3/27/12
to AIMMS - The Modeling System
Hello Luis,

Thanks for the reply.

In my case Place(p2,t) will be 1 for multiple indices. That is why in
the objective function I want for a given (p1,t) the short
Distance(p1,p2) of the indices p2 where Place(p2,t) = 1.

You said that I should combine more than one variable to get what I am
looking for, but I do not really know how to do this (beginner).

Maybe I should make more clear what I want by explaining what I am
modelling:
In a distribution center we have ten paths and multiple types of
pallets. When an empty pallet gets replaced by a full one the empty
pallet should be put on a pile with only that type of pallet. What I
want to know is which type should get a pile in which path. For that I
minimize:
sum((p1,p2,t),Quantities(p1,t)*Distance(p1,p2)*Place(p2,t))
Where Quantities is the amount of pallet type t in path p1, Distance
the distance from path p1 to p2 and Place the binary decision variable
that type t should be put in path p2.

This works fine when we have ten different types of pallets because
for every type there should be one pile. However, when I have less
types of pallets and some types get multiple stacks I am double-
counting because for a t there will be multiple Place variables with
value 1. Now I only want to count the distance to the stack of that
type with the shortest distance.

It would be very much appreciated if you could explain a little more
how to do this by combining multiple of those variables.

Kind regards,
John Brouwer.

On 27 mrt, 09:11, Luis Pinto <luisf...@gmail.com> wrote:
> Hello,
>
> A very common way to model min/max function in Linear Programming is to
> create a new variable that will represent that min/max.
>
> If understood correctly you want a variable that is indexed over p1 and t,
> say x(p1,t).
>
> If you use the constraint:
>
> x(p1,t) >= sum(p2, Distance(p1,p2)*Place(p2,t))
>
> and in your objective function use
>
> Minimize x(p1,t)
>
> The model should solve looking for the smallest Distance(p1,p2) where
> Place(p2,t)
> But this only solves your problem if Place(p2,t) is 1 for 1 index of p2.
>
> If not, you may have to combine more than one variable to get what you are
> looking for.
>
> Hope it helps,
>
> Cheers,
>
> Luis Pinto
>
> www.unisoma.com.br
>

Luis Pinto

unread,
Mar 28, 2012, 6:07:39 AM3/28/12
to ai...@googlegroups.com
Hello,

Let me just see if I fully understand.
You objective function is then actually:

minimize:
sum((p1,t),Quantities(p1,t)* Min(p2 | Place(p2,t) = 1,Distance(p1,p2)*Place(p2,t)))

Now that is a challenge.

Are the distances similar?
The only thing I can think of right of the bat is to "compensate" the Place(p2,t) that are not the minimum by the average.

Something like:

minimize:
sum((p1,p2,t),
   Quantities(p1,t)
   *
   ( (Distance(p1,p2) * Place(p2,t)
        - (1 - sum(p2b, Place(p2b,t)) * AverageDistance(p1) )
   )


The only good thing is that if you only have 1 Place(p2,t) that is = 1, it will behave perfectly, but if you have none for that t, then it will give a bizarre negative result.
Come to think of it, it isn't a very good suggestion :(

Not sure if you can build something like a "comparing variable":

X(p2a,p2b,t) >= Place(p2a,t) - Place(p2b,t)

X(p2a,p2b,t) >= 0

and follow on from that.
Still not a good suggestion.

I'm stuck!

Guido Diepen

unread,
Mar 28, 2012, 11:08:48 AM3/28/12
to ai...@googlegroups.com
Hi John,


not 100% sure yet, as I have not completely verified everything and almost sure about what you want to achieve :), but maybe the additions below will achieve what you need.

Add binaray variable:
combination_is_min_selected(p1, p2, t) 
Value 1 denotes place(p2,t) = 1 AND (p1, p2) is the minimum distance 
Value 0 denotes otherwise


Add constraint:
sum(p1, combination_is_min_selected(p1,p2,t)) <= card(p1) * place(p2, t)   forall (p2,t)

These constraints will ensure that combination_is_min_selected(p1,p2,t) will be 0 for all p1 in case place(p2,t) is 0. I use card(p1) to allow for multiple p1 to have combination_is_min_selected(p1,p2,t) with value 1.


Add constraint:
sum( (p1,p2) , combination_is_min_selected(p1,p2,t)) = 1    forall (t)

These constraints will ensure that for each t, there must be a (p1,p2) such that combination_is_min_selected(p1,p2,t) has the value 1. Without this constraint, the solver could choose the value 0 for all combination_is_min_selected(p1,p2,t)


Now in objective, use this new variable:

minimize
sum( (p1,p2,t) , Quantity(p1,t)
* Distance(p1,p2)
* combination_is_min_selected(p1,p2,t)
   )  

Because you are multiplying the combination_is_min_selected with the distance, the solver will be directed to choose the (p1,p2) combination that has minimal distance.

Hope this is indeed the solution to your problem, as mentioned I did not have time to thoroughly verify it.

Guido Diepen
AIMMS Specialist

> > For more options, visit this group at
> >http://groups.google.com/group/aimms?hl=en.

--
You received this message because you are subscribed to the Google Groups "AIMMS - The Modeling System" group.
To post to this group, send email to ai...@googlegroups.com.
To unsubscribe from this group, send email to aimms+unsubscribe@googlegroups.com.

> > For more options, visit this group at
> >http://groups.google.com/group/aimms?hl=en.

--
You received this message because you are subscribed to the Google Groups "AIMMS - The Modeling System" group.
To post to this group, send email to ai...@googlegroups.com.
To unsubscribe from this group, send email to aimms+unsubscribe@googlegroups.com.

Luis Pinto

unread,
Mar 28, 2012, 11:26:22 AM3/28/12
to ai...@googlegroups.com
Nicely put Guido.
I would only suggest you use


sum( (p1,p2) , combination_is_min_selected(p1,p2,t)) <= 1    forall (t)

since, from what I gathered you could actually have a t with no Place selected, right?

Cheers,

Luis Pinto

www.unisoma.com.br

To view this discussion on the web visit https://groups.google.com/d/msg/aimms/-/cIEyfFBPr3sJ.

To post to this group, send email to ai...@googlegroups.com.
To unsubscribe from this group, send email to aimms+un...@googlegroups.com.

Guido Diepen

unread,
Mar 28, 2012, 1:38:00 PM3/28/12
to ai...@googlegroups.com
Thanks Luis,

I am not sure if it still works if you use the <= 1 instead of = 1. Because the combination_is_min_selected has a positive component in the objective, there would be no incentive to make any of them get the value 1. You must ensure that at least one gets the value 1.

From what I understood, you could have a place t where you are not collecting the pallets, but you could not have a place where you are collecting two types of pallets. My assumption is that you have to collect all of the pallets somewhere.

Now that I am thinking about it, this constraint might actually be wrong the way I stated it: I think you have to make the constraint for all (p1,t)
and use
sum( p2, combination_is_min_selected(p1,p2,t) ) = 1, stating that for each combination of (p1,t), there must be exactly one closest p2 that you will select.

Guido Diepen
AIMMS Specialist


On Wednesday, March 28, 2012 5:26:22 PM UTC+2, Luis Pinto - UniSoma wrote:
Nicely put Guido.
I would only suggest you use

sum( (p1,p2) , combination_is_min_selected(p1,p2,t)) <= 1    forall (t)

since, from what I gathered you could actually have a t with no Place selected, right?

Cheers,

Luis Pinto

www.unisoma.com.br

To unsubscribe from this group, send email to aimms+unsubscribe@googlegroups.com.

Luis Pinto

unread,
Mar 28, 2012, 1:52:20 PM3/28/12
to ai...@googlegroups.com
Since we don't know the model as a whole, I just imagined that the other constraints would imply in some kind of condition that you actually need to have the Place(p2,t) = 1 for some of the combinations, but you are quite right.

Cheers,

Luis Pinto

To view this discussion on the web visit https://groups.google.com/d/msg/aimms/-/YbPpCLWLITkJ.

To post to this group, send email to ai...@googlegroups.com.
To unsubscribe from this group, send email to aimms+un...@googlegroups.com.

John Brouwer

unread,
Mar 28, 2012, 3:37:39 PM3/28/12
to AIMMS - The Modeling System
First of all, thanks a lot for the replies!

The distances are 1 to every path that's next to each other, 2 for
combinations of (p1,p2) where there's one path in between, etc.

About the constraints: I have two constraints. One that says that
sum(t,Place(p2,t))=1 for all p2 so that exactly one pallet type gets a
pile in every path. In the 'exactly identified' case (x paths, x
pallet types) I also had the condition that sum(p2,Place(p2,t))=1 so
that every pallet type gets exactly one pile. When I have less than x
pallet types the condition would of course be sum(p2,Place(p2,t))>=1.

In short, every p2 has exactly one t and every t has at least one p2.

Guido said:
"Add constraint:
sum(p1, combination_is_min_selected(p1,p2,t)) <= card(p1) * place(p2,
t)
forall (p2,t)

These constraints will ensure that
combination_is_min_selected(p1,p2,t)
will be 0 for all p1 in case place(p2,t) is 0. I use card(p1) to allow
for
multiple p1 to have combination_is_min_selected(p1,p2,t) with value 1.
"
I think the card(p1) thing should be removed to agree with exactly one
pallet per path (my first restriction).

"I think you have to make the constraint for all (p1,t)
and use
sum( p2, combination_is_min_selected(p1,p2,t) ) = 1, stating that for
each
combination of (p1,t), there must be exactly one closest p2 that you
will
select."
Sounds like this is also the right restriction for the problem,
because there will probably be a pallet of type t in every path p1, so
for all those combinations the needs to be a minimal destination path
p2.

The objective function sounds logical..

I entered all this and now I am getting the error the after some
iterations CPLEX could not find an integer solution to my mathematical
program...

Kind regards,
John Brouwer.

On 28 mrt, 19:52, Luis Pinto <luisf...@gmail.com> wrote:
> Since we don't know the model as a whole, I just imagined that the other
> constraints would imply in some kind of condition that you actually need to
> have the Place(p2,t) = 1 for some of the combinations, but you are quite
> right.
>
> Cheers,
>
> Luis Pinto
>
> On 28 March 2012 19:38, Guido Diepen <Guido.Die...@aimms.com> wrote:
>
>
>
> > Thanks Luis,
>
> > I am not sure if it still works if you use the <= 1 instead of = 1.
> > Because the combination_is_min_selected has a positive component in the
> > objective, there would be no incentive to make any of them get the value 1.
> > You must ensure that at least one gets the value 1.
>
> > From what I understood, you could have a place t where you are not
> > collecting the pallets, but you could not have a place where you are
> > collecting two types of pallets. My assumption is that you have to collect
> > all of the pallets somewhere.
>
> > Now that I am thinking about it, this constraint might actually be wrong
> > the way I stated it: I think you have to make the constraint for all (p1,t)
> > and use
> > sum( p2, combination_is_min_selected(p1,p2,t) ) = 1, stating that for each
> > combination of (p1,t), there must be exactly one closest p2 that you will
> > select.
>
> > Guido Diepen
> > AIMMS Specialist
>
> > On Wednesday, March 28, 2012 5:26:22 PM UTC+2, Luis Pinto - UniSoma wrote:
>
> >> Nicely put Guido.
> >> I would only suggest you use
>
> >> sum( (p1,p2) , combination_is_min_selected(**p1,p2,t)) <= 1    forall (t)
>
> >> since, from what I gathered you could actually have a t with no Place
> >> selected, right?
>
> >> Cheers,
>
> >> Luis Pinto
>
> >>www.unisoma.com.br
>
> >> On 28 March 2012 17:08, Guido Diepen <> wrote:
>
> >>> Hi John,
>
> >>> not 100% sure yet, as I have not completely verified everything and
> >>> almost sure about what you want to achieve :), but maybe the additions
> >>> below will achieve what you need.
>
> >>> Add binaray variable:
> >>> combination_is_min_selected(**p1, p2, t)
> >>> Value 1 denotes place(p2,t) = 1 AND (p1, p2) is the minimum distance
> >>>  Value 0 denotes otherwise
>
> >>> Add constraint:
> >>> sum(p1, combination_is_min_selected(**p1,p2,t)) <= card(p1) * place(p2,
> >>> t)   forall (p2,t)
>
> >>> These constraints will ensure that combination_is_min_selected(**p1,p2,t)
> >>> will be 0 for all p1 in case place(p2,t) is 0. I use card(p1) to allow for
> >>> multiple p1 to have combination_is_min_selected(**p1,p2,t) with value 1.
>
> >>> Add constraint:
> >>> sum( (p1,p2) , combination_is_min_selected(**p1,p2,t)) = 1    forall (t)
>
> >>> These constraints will ensure that for each t, there must be a (p1,p2)
> >>> such that combination_is_min_selected(**p1,p2,t) has the value 1.
> >>> Without this constraint, the solver could choose the value 0 for all
> >>> combination_is_min_selected(**p1,p2,t)
>
> >>> Now in objective, use this new variable:
>
> >>> minimize
> >>> sum( (p1,p2,t) , Quantity(p1,t)
> >>>  * Distance(p1,p2)
> >>>  * combination_is_min_selected(**p1,p2,t)
> >>>    )
>
> >>> Because you are multiplying the combination_is_min_selected with the
> >>> distance, the solver will be directed to choose the (p1,p2) combination
> >>> that has minimal distance.
>
> >>> Hope this is indeed the solution to your problem, as mentioned I did not
> >>> have time to thoroughly verify it.
>
> >>> Guido Diepen
> >>> AIMMS Specialist
>
> >>> On Wednesday, March 28, 2012 12:07:39 PM UTC+2, Luis Pinto - UniSoma
> >>> wrote:
>
> >>>> Hello,
>
> >>>> Let me just see if I fully understand.
> >>>> You objective function is then actually:
>
> >>>> minimize:
> >>>> sum((p1,t),Quantities(p1,t)* Min(p2 | Place(p2,t) =
> >>>> 1,Distance(p1,p2)*Place(p2,t))****)
> >>>>  On 27 March 2012 19:59, John Brouwer <johnbrouwer...@gmail.com> wrote:
>
> >>>>> Hello Luis,
>
> >>>>> Thanks for the reply.
>
> >>>>> In my case Place(p2,t) will be 1 for multiple indices. That is why in
> >>>>> the objective function I want for a given (p1,t) the short
> >>>>> Distance(p1,p2) of the indices p2 where Place(p2,t) = 1.
>
> >>>>> You said that I should combine more than one variable to get what I am
> >>>>> looking for, but I do not really know how to do this (beginner).
>
> >>>>> Maybe I should make more clear what I want by explaining what I am
> >>>>> modelling:
> >>>>> In a distribution center we have ten paths and multiple types of
> >>>>> pallets. When an empty pallet gets replaced by a full one the empty
> >>>>> pallet should be put on a pile with only that type of pallet. What I
> >>>>> want to know is which type should get a pile in which path. For that I
> >>>>> minimize:
> >>>>> sum((p1,p2,t),Quantities(p1,t)*****Distance(p1,p2)*Place(p2,t))
> >>>>> > > min(p2,Distance(p1,p2)*Place(**p**2,t)>0)
>
> >>>>> > > If not, I would like to make another variable called
> >>>>> Activeplace(p2,t)
> >>>>> > > that is 1 when Place(p2,t) = 1 and inf when Place(p2,t) = 0 so I
> >>>>> can take
> >>>>> > > the minimum very straightforward. How should I enter this in the
> >>>>> definition
> >>>>> > > of the variable? I have been trying to find the correct syntax for
> >>>>> this but
> >>>>> > > I cannot find it.
>
> >>>>> > > Thanks in advance.
>
> >>>>> > > Kind regards,
> >>>>> > > John Brouwer.
>
> >>>>> > > --
> >>>>> > > You received this message because you are subscribed to the Google
> >>>>> Groups
> >>>>> > > "AIMMS - The Modeling System" group.
> >>>>> > > To view this discussion on the web visit
> >>>>> > >https://groups.google.com/d/**m**sg/aimms/-/ApOOiVc6sw0J<https://groups.google.com/d/msg/aimms/-/ApOOiVc6sw0J>
> >>>>> .
> >>>>> > > To post to this group, send email to ai...@googlegroups.com.
> >>>>> > > To unsubscribe from this group, send email to
> >>>>> > > aimms+unsubscribe@**googlegroups**.com<aimms%2Bunsu...@googlegroups.com­>
> >>>>> .
> >>>>> > > For more options, visit this group at
> >>>>> > >http://groups.google.com/**grou**p/aimms?hl=en<http://groups.google.com/group/aimms?hl=en>
> >>>>> .
>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google
> >>>>> Groups "AIMMS - The Modeling System" group.
> >>>>> To post to this group, send email to ai...@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to aimms+unsubscribe@**
> >>>>> googlegroups**.com <aimms%2Bunsu...@googlegroups.com>.
> >>>>> For more options, visit this group athttp://groups.google.com/**group
> >>>>> **/aimms?hl=en <http://groups.google.com/group/aimms?hl=en>.
>
> >>> On Wednesday, March 28, 2012 12:07:39 PM UTC+2, Luis Pinto - UniSoma
> >>> wrote:
>
> >>>> Hello,
>
> >>>> Let me just see if I fully understand.
> >>>> You objective function is then actually:
>
> >>>> minimize:
> >>>> sum((p1,t),Quantities(p1,t)* Min(p2 | Place(p2,t) =...
>
> meer lezen »

Luis Pinto

unread,
Mar 28, 2012, 4:19:32 PM3/28/12
to ai...@googlegroups.com
Try running the Mathematical Program as a RMIP (relaxed mixed integer program).
Although you won't get the integer result you may more or less look the the results and see if anything is very off.

To unsubscribe from this group, send email to aimms+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/aimms?hl=en.


John Brouwer

unread,
Mar 28, 2012, 4:40:44 PM3/28/12
to AIMMS - The Modeling System
Now it says it is infeasible and the results don't agree with the
restrictions.. The data I am using:
t = {1,2}
p1/p2 = {A1,A2,A3}
Quantities(p1,t) = [5 1; 2 3; 1 5]
Distance(p1,p2) = [1 2 3; 2 1 2; 3 2 1]

The solution should of course be that pallet type 1 gets path A1 and
pallet type 2 gets path A2 and A3.

Guido Diepen

unread,
Mar 29, 2012, 8:23:36 AM3/29/12
to ai...@googlegroups.com
Hi,

how to read the input data you stated is not clear to me...

Regarding the infeasibility, what you can do is to instruct AIMMS to write the constraint listing to the listing file (see http://www.aimms.com/knowledgebase/kb000017 for more information on how to do this). 

Because your test problem is very small, it might help you to see whether the generated model contains constraints you don't expect. You also have the possibility of the solver/AIMMS generating an IIS (also explained in the KB article). This IIS contains the constraints that together are causing the infeasility (e.g. one constraint x+y >= 5 and another constraint x+y <= 3, which can never be feasible).

Guido Diepen
AIMMS Specialist

John Brouwer

unread,
Mar 29, 2012, 3:35:04 PM3/29/12
to AIMMS - The Modeling System
Hello Guido,

The input data is in MATLAB syntax. Quantities(p1,t) is a 3x2 matrix
with 5,1 on the first row, 2,3 on the second and 1,5 on the third.
Distance(p1,p2) is a three by three matrix with 1,2,3 on the first
row, 2,1,2 on the second and 3,2,1 on the third.

I ran the constraint listing but that did not give me very much
information. The IIS contains more useful info for me but the reason
for the problem being infeasible is still a littlebit vague to me
comparing the contraint causing the infeasibility and the values of
the variables AIMMS gives back when it stops. Maybe there's just a
little type somewhere in the constraints of the
combination_is_min_selected.

Now I do not really have the time to look into this more closely but
maybe I will during the weekend.

MIP still gives the error that an integer solution could not be found
which sounds very strange to me because I am only using integer
parameters and binary variables..

Kind regards,
John Brouwer.

John Brouwer

unread,
Mar 30, 2012, 4:02:15 AM3/30/12
to AIMMS - The Modeling System
Btw, it said that the problem was in sum(p1,
combination_is_min_selected(p1,p2,t)) <= place(p2,
t) forall (p2,t).

John Brouwer

unread,
Mar 30, 2012, 2:54:08 PM3/30/12
to AIMMS - The Modeling System
* John finds out about his stupid mistake, slams his head against the
desk 3 time, adjusts the constraint and finds out everything works
perfectly now *

Finally realized that dropping card(p1) made it infeasible because of
course multiple (p1,t)'s can have the same p2 as minimal distance.
What I decided to make as card(p1): sum(p1,1) so that Guido's first
constraint now is:
sum(p1, combination_is_min_selected(p1,p2,t)) <= sum(p1,1) * place(p2,
t)
forall (p2,t)

I could also have just taken a very large value but this is a little
more general.

Thanks a lot for all the help Luis and Guido!

Guido Diepen

unread,
Mar 31, 2012, 11:30:28 AM3/31/12
to ai...@googlegroups.com
Hi John,

glad to hear that it is working perfectly now ;) Hope you did not hit the desk too hard :)

You are right that it should be a large value (actually, a sufficiently large value). The card(p1) will actually give you exactly the same value as sum(p1,1): it just returns the number of elements p1.

Guido Diepen
AIMMS Specialist
Reply all
Reply to author
Forward
0 new messages