division by 0

Problems with modeling
Post Reply
sandy12310627
User
User
Posts: 8
Joined: 1 month ago

division by 0

Post by sandy12310627 » 1 week ago

Hi,

I am wondering how to eliminate the problem of division by 0 in the objective function.
I consider that the denominator is not 0 but it shows the problem.
Also, is it possible for changing this non-linear programming to linear programming?
I am appreciated for your reply!

set
t period /1*2/
i parts /i1,i2/;

variable
total
meanofX

avgq
DCvariable
BWE
sumofall
sumofallq
DD(t)
;

free variable z;

parameter
it(t)
totalt
u(t)
X(t)

X(t) /1 70,2 100/;
totalt=card(t);
it(t)=ord(t)-1;
u(t)=uniform(0,1);

SCALAR

gamma/0.4/;

equation
bweX1
bweX2
bweX3
bweX4

varq1
varq2
varq3
obj;

bweX1..sumofall=E=sum(t,X(t));
bweX2..total=e=totalt;
bweX3..meanofX=e=(sumofall/totalt);
bweX4..BWE=e=sum(t,sqr(X(t)-meanofX))/totalt;

varq1..sumofallq=e=sum(t,DD(t));
varq2..avgq=e=(sumofallq/totalt);
varq3..DCvariable=e=sum(t,sqr(DD(t)-avgq))/totalt;

obj..z=e=DCvariable/BWE; <<it shows the problem here.

model LP/
all/;
Solve LP using NLP maximizing BWE;
Display BWE.L,meanofX.L,total.L;

User avatar
Renger
Posts: 496
Joined: 3 years ago

Re: division by 0

Post by Renger » 1 week ago

Hi
If you don't initialize your model variables, GAMS assumes as a start value zero. The best way to avoid this is by setting all your variables to a value different from zero:
e.g. BWE.L = 10;

Cheers

Renger
PS. Next time you post code, put them in a code block (the </> button in the editor.
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

sandy12310627
User
User
Posts: 8
Joined: 1 month ago

Re: division by 0

Post by sandy12310627 » 1 week ago

Thank you Renger~
I tried what you said. But there is another question.
I calculated and declared Xvariable.L=606.6816; however, I do not think the calculation is correct.
I think the problem can be traced to "sumofall", if I calculated by myself, it will not be zero. But the output of the GAMS shows sumofall is 0. Thus, I considered that the problem of BWE got 0 is because of "sumofall"
BWE.txt
(6.92 KiB) Downloaded 8 times

User avatar
Renger
Posts: 496
Joined: 3 years ago

Re: division by 0

Post by Renger » 1 week ago

Hi
You can set any level you want for this variable, as long as it solves.
Your second problem formulation doesn't solve (it is infeasible). Perhaps some of your constraints do now allow for an optimal solution.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

sandy12310627
User
User
Posts: 8
Joined: 1 month ago

Re: division by 0

Post by sandy12310627 » 5 days ago

Hi~

I tried to put Xvariable.lo=1 in the equation to avoid division of 0; however, the result of Xvariable is 1.
I do not think the answer is 1 because the equaton:Xvariable=e=sum(t,sqr(X(t)-avgX))/totalt
totalt=card(t) in this case, totalt should be 3
sumofall=E=sum(t,X(t)) in this case, sumofall should be 1609
X LOWER LEVEL UPPER MARGINAL

1 503.4349 503.4349 503.4349 EPS
2 542.8836 542.8836 542.8836 EPS
3 562.6930 562.6930 562.6930 EPS
avgX=e=(sumofall/totalt) in this case, avgX should be 536.33
sum(t,sqr(X(t)-avgX)) should be 606.68
Xvariable should be 202.22

However, here is the result of the question which I post in the last reply

LOWER LEVEL UPPER MARGINAL

---- VAR total . 3.0000 +INF EPS
---- VAR avgX . . +INF EPS
---- VAR sumofall . . +INF EPS
---- VAR Xvariable 1.0000 1.0000 +INF EPS
---- VAR avgq . . +INF EPS
---- VAR sumofallq . . +INF EPS
---- VAR DCvariable . . +INF EPS
---- VAR avgin . . +INF EPS
---- VAR sumofallin . . +INF EPS
---- VAR INvariable . . +INF EPS
---- VAR BWE . . +INF EPS
---- VAR orderbwe . . +INF EPS
---- VAR invenbwe . . +INF EPS
---- VAR z -INF . +INF EPS

I could not find where is the problem.
I appreciate that you help me a lot!

User avatar
Renger
Posts: 496
Joined: 3 years ago

Re: division by 0

Post by Renger » 5 days ago

Hi
If you set a lower bound of 1, the variable is not allowed to be below 1. It is a constraint. To avoid division by zero, you have to set the starting value (e.g. X.L and not X.LO).
GAMS uses the level for a variable as a starting point, the final value can be different. If you set X.LO = 1, it takes 1 as starting value, but will not get below 1.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

sandy12310627
User
User
Posts: 8
Joined: 1 month ago

Re: division by 0

Post by sandy12310627 » 5 days ago

Hi!

I see! thank you so much!
Thus, I am wondering what is the problem of my equation.
the result shows there is an equation is inconsistent with other equations in the pre-triangular part of the model.
image.png
image.png
But I have no idea about it. I search the guild manual and it said it is the conflict of the result.
image.png
image.png (4.87 KiB) Viewed 170 times
here is my equation. I would like to get CR(t) from X(t) and get CA(t+1) from CR(t) and so on.
Thank you for helping me!

User avatar
Renger
Posts: 496
Joined: 3 years ago

Re: division by 0

Post by Renger » 3 days ago

Hi
The only thing I can think of (but this is not my field of expertise) that somewhere in your model you also relate CU and CA (something like CU = q*CA and this conflicts with the two equations defining CU and CA (one being d times and the other 1-d times CR). Or you relate X(t) with those variables and that leads to an inconsistency.
CHeers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

sandy12310627
User
User
Posts: 8
Joined: 1 month ago

Re: division by 0

Post by sandy12310627 » 2 days ago

I am trying to find the reason and finding the problem that existed when I changed LP to NLP.
However, I have the multiple and division in variable so I need to use NLP.
I am wondering if any way I can use LP in NLP problem.

Also, I checked my answer and found an answer is negative. but I set the POSITIVE variable.
Is it possible for this error?
Thank you for patiently answering me!

Sincerely,
Sandy

Post Reply