division by zero! touble...

Hello. I have an error that is division by zero.
The code is below.


sets
W /w1, w2/
T /t1, t2/
alias (W, W_w);

Parameter
p(W,T)
s(W,T)
vw(W)
vt(T);

Binary variable x(W,T);

The equation is
sum(W, vw(W) * prod(T, (1-s(W,T)/(sum(W_w, x(W_w, T))))**x(W,T))) =g= 10 ;

I want to

  1. sum(W_w, x(W_w, T)) = 0
    s(W,T) / (sum(W_w, x(W_w, T))) = s(W,T)
  2. sum(W_w, x(W_w, T)) = 1
    s(W,T) / (sum(W_w, x(W_w, T))) = s(W,T) / 1
  3. sum(W_w, x(W_w, T)) = 2
    s(W,T) / (sum(W_w, x(W_w, T))) = s(W,T) / 2

Help me please…!

Hi, you will need bunch of tricks using big-M constraints to correctly model this:

  1. Declare a binary variable p that will be 0 if sum(W_w, x(W_w, T))) is 0 and 1 otherwise.
    You can do this as follows:

sum(W_w, x(W_w, T))) =l= M*p;
p =l= sum(W_w, x(W_w, T))) ;

The second constraint will force p to be 0 when the sum is 0, the first constraint will force p to be 1 when sum is not zero.

  1. Define a variable v(w,t) that will be s(w, t) when p is 0 and s(w, t)/sum(W_w, x(W_w, T))) when p is 1. You can do this as follows:
    g(w,t) =l= s(w, t) + Mp and g(w,t) =g= s(w, t) - Mp
    These will force the condition when p is 0 and keep g(w,t) unbounded when p is 1.
    g(w,t) =l= s(w,t)/sum(W_w, x(W_w, T))) + M*(1-p)
    g(w,t) =g= s(w,t)/sum(W_w, x(W_w, T))) - M*(1-p)

Finally, now that you are sure that you would use the term sum(W_w, x(W_w, T))) in the denominator only when it is not zero, you can add a very small number to it so that the solver does not give you division by zero error.

  • Atharv

Thank you for your kind.

I understand your trick, Big-M, but that’s not the ultimate solution.
The Big-M trick’s logic is right, but GAMS cannot reflect the logic because of 2 equations.

Because eventually there exists a touble(division by zero) in GAMS
at this two equations.
g(W,T) =l= s(W,T)/(sum(W_w, x(W_w, T))) + M*(1-k(T))
g(W,T) =g= s(W,T)/(sum(W_w, x(W_w, T))) - M*(1-k(T)) ;

Hi, I explained the same in the last part of my answer that you can add a small number to the denominator e.g., sum(W_w, x(W_w, T))) + M*(1-k(T) + 0.0000001. Why did we do big-M trick then? Because for sum(W_w, x(W_w, T))) = 0, w(s,t)/(sum(W_w, x(W_w, T)))+ 0.0000001) would give us a very large number and you need it to be w(s,t).

  • Atharv

Thank you very much!
As you advised, the touble is solved.

But, the GAMS can not give a good solution, because I think it is NP-complete…

Thank you again! Have a nice day!^^*