## division by 0

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

### division by 0

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?

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;

Renger
Posts: 496
Joined: 3 years ago

### Re: division by 0

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 Posts: 8
Joined: 1 month ago

### Re: division by 0

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

Renger
Posts: 496
Joined: 3 years ago

### Re: division by 0

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 Posts: 8
Joined: 1 month ago

### Re: division by 0

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!

Renger
Posts: 496
Joined: 3 years ago

### Re: division by 0

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 Posts: 8
Joined: 1 month ago

### Re: division by 0

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. But I have no idea about it. I search the guild manual and it said it is the conflict of the result. 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!

Renger
Posts: 496
Joined: 3 years ago

### Re: division by 0

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 Posts: 8
Joined: 1 month ago

### Re: division by 0

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.8389012,"math_prob":0.9437569,"size":9211,"snap":"2020-10-2020-16","text_gpt3_token_len":2681,"char_repetition_ratio":0.15086347,"word_repetition_ratio":0.8643761,"special_character_ratio":0.30441862,"punctuation_ratio":0.15152986,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9878506,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-06T20:20:39Z\",\"WARC-Record-ID\":\"<urn:uuid:5a5ff157-a7ac-4138-a127-5344569b31d8>\",\"Content-Length\":\"52516\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b9e55233-31ea-4bef-b754-d888e98f3595>\",\"WARC-Concurrent-To\":\"<urn:uuid:886ee0ca-c28a-4a72-9c91-746134e8bdd4>\",\"WARC-IP-Address\":\"185.119.172.190\",\"WARC-Target-URI\":\"https://forum.gamsworld.org/viewtopic.php?f=9&t=11301&p=26699\",\"WARC-Payload-Digest\":\"sha1:KFYPASUCCYT7HJMWHQY5C6ASMIS37YHR\",\"WARC-Block-Digest\":\"sha1:N6H5APC6MKOOXODENGYNSTRQOOYQAKYL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585371660550.75_warc_CC-MAIN-20200406200320-20200406230820-00087.warc.gz\"}"}