help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] if...then...else


From: glpk xypron
Subject: Re: [Help-glpk] if...then...else
Date: Fri, 25 Jun 2010 03:13:04 +0200

Hello Jeff

see the example below.

> if w[s] = 0 then inc[s] = 0 else inc[s] = 1

Your "if" is modelled as

s.t. indicator{s in S} :
  w[s] <= M * inc[s];

M should be chosen as small as is possible without restricting
the solution.

Best regards

Xypron



# some set
set S := {1..10};
# the prize of one unit
param prize{S} := Uniform01();
# the benefit of one unit
param benefit{S} := Uniform01();
# variable indicating unit is selected
var inc{S}, binary;
# variable indicating part used of selected unit
var w{S}, >= 0;
# big M
param M := 1;

minimize obj :
  sum{s in S} w[s] * prize[s];
  
s.t. indicator{s in S} :
  w[s] <= M * inc[s];

s.t. cardinality :
  sum{s in S} inc[s] <= 4;

s.t. minimumBenefit :
  sum{s in S} w[s] * benefit[s] >= 2;

solve;

printf {s in S : inc[s] = 1}
  "chosen element  : %s, weight      : %f, benefit      : %f\n", 
  s, w[s], benefit[s] * w[s];
printf
  "number of chosen: %i, total weight: %f, total benefit: %f\n", 
  sum{s in S} inc[s], sum{s in S} w[s], sum{s in S} benefit[s] * w[s];

end;


-------- Original-Nachricht --------
> Datum: Fri, 25 Jun 2010 04:32:07 +0400
> Betreff: [Help-glpk] if...then...else

> I'm trying to add a cardinality constraint to an existing model and I'm
> getting stuck. 
> 
> var w{S} ;
> param inc{S}, binary;
> 
> where I want to set the binary variable:
> 
> if w[s] = 0 then inc[s] = 0 else inc[s] = 1
> 
> s.t. IsIncluded {s in S}: if w[s] > 0 then inc[s]=1;
> 
> /* a cardinality constraint -- only include 4 in the solution */
> s.t. C6: sum {s in S} inc[s]  = 4;
> 
> 
> I must admit I'm a newbie at coding models with MathProg, so I think I'm
> suffering from the syntax. 
> 
> Basically, I would like to set the binary variable to 1 if w[s] > 0 and 0
> otherwise.
> 
> Is this the proper use of the if-then-else statement?
> 
> 
> Jeff Hamann

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



reply via email to

[Prev in Thread] Current Thread [Next in Thread]