a simple example of my gnarly IF THEN ELSE problem (bug?)

Use this forum to post Vensim related questions.
Post Reply
sjgenco
Member
Posts: 25
Joined: Mon Sep 26, 2022 10:52 pm
Vensim version: PLE

a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by sjgenco »

I notice over 40 views of my original post, but not one reply. I've continued to dig around, and the problem is looking more like a bug than user error to me, but of course I'm ready to stand corrected if this is simply a "you forgot to put a comma here" problem.

Here's a really simple model that I think illustrates the issue. I've attached the model (testIF.mdl).
It attempts to calculate an outcome variable [result] from 4 simple input variables, a=1, b=2, c=3, and d=4.
I only get three attachments, so you'll have to open the attached model to see the View.

Here's the equation for {result]. This is the same logic as in my original post, but with simpler variables so the structure is clear:
testIF2.jpg
testIF2.jpg (274.65 KiB) Viewed 664 times
As can be seen at the bottom, when I "Check Syntax", I get the same "Expecting an operator" error.

I figured it must have something to do with those IF statements, maybe I wasn't doing the :AND: logic correctly.
So, I created [result2] by doing a copy-paste on [result], modified it (deleted the inputs from a and b) and removed all the IF THEN ELSE statements . Certainly that should work.

Here is the [result2] equation. It's [result] stripped down to one operation:
testIF3.jpg
testIF3.jpg (247.01 KiB) Viewed 664 times
Yet, this simple equation (can't get simpler!) also fails with the "Expecting an operator" error. I thought maybe duplicating [result] had carried over some hidden code to [result2] that somehow stuck around after I deleted all the IF statements. To test this, I deleted [result2] and added it back into the model as a fresh new variable, setting its equation to the same simple 100 followed by c * d. Got the same result, "Expecting an operator".

I hope this example makes the issue clearer than in my previous post.
Attachments
testIF1.jpg
testIF1.jpg (43.52 KiB) Viewed 664 times
sjgenco
Member
Posts: 25
Joined: Mon Sep 26, 2022 10:52 pm
Vensim version: PLE

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by sjgenco »

Looks like I ran out of attachments. Here is the model.
Attachments
testIF.mdl
(2.29 KiB) Downloaded 65 times
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by tomfid »

This kind of looks like procedural code in the equation field. You can't do that, by design.

100
c*d

is read as

100 c*d

(after condensing the whitespace).

This would have to be

100 <operator> c*d

where <operator> is + - * / ^ in order to make sense.
sjgenco
Member
Posts: 25
Joined: Mon Sep 26, 2022 10:52 pm
Vensim version: PLE

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by sjgenco »

Thanks Tom. That makes sense. So you can assign a value to a variable in an equation, but only if you don't include any more code after the assignment?

I thought maybe changing [result] and [result2] to Levels instead of Auxiliaries might do the trick. It does fix [result2] (removing the 100 in the equation and adding it as the Initial Value) ... but it doesn't work with [result]. Removing the 100 and adding it as an Initial Value still gets the error.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by Administrator »

Think of how you do IF statements in Excel, it's the same in Vensim.

I was going to try and give you an example of how to calculate C, but I'm not clear on how to. But if you used the first IF THEN ELSE statement (IF THEN ELSE( a > b , c=1, c=0 ) {false, so c=0}), this in Vensim would look like

c = IF THEN ELSE( a > b , 1, 0 )

Hope this helps.

PS. Replies are slow at the moment, we've been travelling for the System Dynamics conference.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
sjgenco
Member
Posts: 25
Joined: Mon Sep 26, 2022 10:52 pm
Vensim version: PLE

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by sjgenco »

I tried your suggestion as follows:

Code: Select all

{inputs: a=1, b=2, c=3, d=4}

100                                                  {initial value for result}
  
c = IF THEN ELSE( a > b , 1, 0 )                     {false, so c=0}

c = IF THEN ELSE( c=0 :AND: d <= 3 , 1 , 0 )         {false, so c=0}

c = IF THEN ELSE( c=0 :AND: d > 3 ,  10 ,  20 )      {true, so c=10}

c * d                                                {should = 10 * 5, so result = 40}
This still gets the error. Perhaps an issue with trying to use [c] both inside and outside the IF statement?
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by tomfid »

The only assignment in Vensim is to the left hand side variable. If you want to make multiple assignments, you can use distinct variables, or subscript elements. You can't make multiple assignments within a single equation.
sjgenco
Member
Posts: 25
Joined: Mon Sep 26, 2022 10:52 pm
Vensim version: PLE

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by sjgenco »

OK, now I know what the problem is (is this mentioned in the documentation anywhere, if so, I couldn't find it).
I'm using PLE so don't have subscripts, so I'll try to work it out with multiple variables.
This is a good (if painful) way to learn these little gotchas in Vensim.

Also, a followup question if I may ... Do you have to have something in the ELSE clause?
Is there a way to leave out the ELSE clause, e.g.,
IF THEN ELSE( _cond_ , _ontrue_ , ) -or-
IF THEN ELSE( _cond_ , _ontrue_ ) -without last comma-

I'm guessing NO, right?
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: a simple example of my gnarly IF THEN ELSE problem (bug?)

Post by tomfid »

Correct - no - because then there would be no value to assign to the LHS.
Post Reply