Status of if and when equations

9 posts / 0 new
Last post
jakesson
Offline
Joined: 2009-03-14
Status of if and when equations

 

Hi,

We have recently been working with if equations, below is a summary of the development status:

  • If equations are now supported, with some limitations, see below.
  • When equations are supported, also with some limitations, see below.

To illustrate the limitation with regards to if-equations, consider the example:

model M1
  function F
    input Real x;
    input Real y;
    output Real u;
    output Real v;
  algorithm 
    u :=2*x;
    v :=5*y + x;
  end F;
  Real x;
  Real y;
equation 
   if time>=1 then
     x = time;
     y = 2*time;
   else
     (x,y) = F(time,time*2);
   end if;
end M1;

This model gives a compliance error:

Error: in file 'IfTest.mo':
Compliance error at line 14, column 4:
  If equations that has non-parameter tests and contains function calls using multiple outputs are not supported
However, I have not came across this construct in the MSL, so it seems not be a significant limitation.

When it comes to when equations, elsewhen clauses are not supported:

model M2
  function F
    input Real x;
    input Real y;
    output Real u;
    output Real v;
  algorithm 
    u :=2*x;
    v :=5*y + x;
  end F;
  Real x;
  Real y;
equation 
  when sample(0,1) then
     (x,y) = F(time,time*2);
  elsewhen sample(0,0.5) then
      (x,y) = F(time*3,-2*time);
  end when;
end M2;

JModelica.org here gives a compliance error:

Error: in file 'IfTest.mo':
Compliance error at line 14, column 3:
  Else clauses in when equations are currently not supported
Also in this case, I have found no such constructs in the MSL and it seems that this limitation exist also in some commercial tools.

In summary, we are making good progress in closing the compliance gap for if and when equations as far as MSL goes.

When it comes to when statements and algorithm sections, we have some more work to do so stay tuned :-).
 

Best regards 

/Johan

 

roel
Offline
Joined: 2010-02-23
Nice

Thanks Johan and the whole team.  These are very useful developments.  
 
roel
 

janssen
Offline
Joined: 2012-04-17
Don't use the MSL as a compliance test

I'm glad to see you folks are working on if and when equations.
 
But I'd suggest not looking at the MSL as a testbed for JModelica's "compliance gap".  Much of it seems to have been developed for either old or odd versions of the Modelica language.  You might think of it as "necessary but not sufficient" -- you have to be able to support it, but supporting just it doesn't mean you have a working Modelica system.
 
Instead, generate your own set of tests directly from the Modelica language spec, and test compliance against that test set.
 

Paul
Offline
Joined: 2012-07-18
if equations

Dear Johan,
 
this sounds like you are advancing with the if-equations. I fact I think these are absolutely necessary as they are used in every second optimisation problem.
I for instance have a simple optimisation problem of a storage that has different prices for charging and discharging, but I cannot implement it due to the discrete step.
It is basically like this
model xy
parameter real load;
parameter real tariff1;
parameter real tariff2;
input real power;
real soc; //state of charge
real cost;
equation
if P<0 then
der(soc) = Power
der(cost) = Power * tariff1
else
der(soc) = Power
der(cost) = Power * tariff2
end if;
end xy;
Optimisation: Minimize the costs with constraints on soc, power etc.
 
It fully compiles but then gives me the message: The collocation optimisation does not support models with events.Please consider noEvent() operator...
 
Is there anything I can do... or is there a workaround?
Thanks for your support.
Best regards,
Paul
 
 

jakesson
Offline
Joined: 2009-03-14
Hi Paul,   Mixing

Hi Paul,
 
Mixing discontinuous switches and/or integer decision variables with non-linear and non-convex continous dynamics leads to intrincially difficult problems. The collocation algoriths as implemented in JModelica.org relies on all function being twice continously differentiable, a condition that is violated by the switch on P in the model. Setting this issue aside for a moment, you may rewrite your model into:
 
der(soc) = Power;
der(cost) = Power*noEvent(if Power<0 then tariff1 else tariff2);
 
While the compiler will accept this, you may run into problems when solving the optimization problem due to the discontinuity.
 
Possible solutions/workarounds include:

  • Introduce a smoothened version of the switch, e.g., by using sigmoid or arctan functions. This approach can be used with the current collocation implementation in JModelica.org.
  • If the switching sequence is known, then the problem can be cast as a multi phase problem. This class of optimization problems are not yet supported by JModelica.org, but it is on the road map.
  • If the dynamics is piecewise affine, then you may want to have a look at the multiparametric toolbox, which supports solution of such problems.
  • There are also algorithms for solving MINLPs, although I have little experience with using them.

 
Best
/Johan

Paul
Offline
Joined: 2012-07-18
sigmoid functions

Dear Johan,
 
thank you for your quick answer.
I tried to implement the equation with sigmoid functions... Both with atan and the logistic function it compiles and it even converges and claims to have found an optimal solution.
It takes very long to compile because the sigmoids need to be integrated and give quite some lenghty functions.
I will let you know whether it worked.
Paul

jakesson
Offline
Joined: 2009-03-14
Hi Paul,   I don't quite see

Hi Paul,
 
I don't quite see the reason why the models should take long time to compile - what is meant by "sigmoids need to be integrated"?
 
If you choose to have steep profiles, in the sigmoids, it is likely that convergence is slow, but that is normal and related to the fact that you are approaching discontinuous function.
 
Best
/Johan

Paul
Offline
Joined: 2012-07-18
sigmoid functions

Dear Johann,
I should have expressed myself better.
- Compilation is slower than for the very simple equations I used in other problems. I now have a very long equation to be solved and therefore it slows down. But that is only natural.
- Integration of sigmoids: What I meant is that I had expressed my orignal problem wrongly. To describe my problem correctly I actually needed to integrate the sigmoid functions I used. Thus the cost would be the integral under the switch function. But this integration was quite easy thanks to Wolfram Mathematica:)
I tried both the (1/(1+e^-x)) and actan(x). These however then need to be integrated for my problem. I have the impression that the first sigmoid function is a bit faster due to a easier integral. On the other hand it might run into problems as I put a constant to make the sigmoid "transition" very steep. When I then have higher x values the e^x becomes very large. The integral then has a term that is log(1+e^x) which might be difficult for Jmodelica to handle.
However, finally it all worked out, the optimisation is excellent. Is is the first time that I had a problem for which Jmodelica needed more than 70 iterations with even lower tolerance settings, but the result is more than satisfying.
Once analysed, I will share with the result of the two sigmoids in the forum.
Luckily, I do not have a problem with several step in the function... I would have no idea how to translate that into sigmoids, therefore I still hope for the if and when-functions:)
 
Thanks again for the very good support and the also thanks to the whole Jmodelica team.
Paul

Paul
Offline
Joined: 2012-07-18
sigmoid functions

Dear all,
just as a final conclusion:
The implementation of a step function with the sigmoid function (I used atan) works perfectly.
However, it is quite complicated in particular when you need to perform further mathematics (like an integration) on top of the sigmoid function.
Furthermore, I would not know what to do if you have more than one switch in your function (for instance 3 price levels instead of two).
Therefore it would still be very helpful if the simple if or when expressions can be used.
Best regards,
Paul

Login or register to post comments