How to express objective on final state?

12 posts / 0 new
Last post
janpeter
Offline
Joined: 2010-09-11
How to express objective on final state?

Hi

I have gone through the example in the manual and wonder how one express optimization where the objective is:

1) Maximize the final state

2) Maximize a function of the control variables at each instant

Would appreciate some input

Jan Peter

 

borlum
Offline
Joined: 2016-09-22
Maximizing final state

Hi Jan Peter,

Maximizing final state is possible, by simply maximizing your state at 'finalTime'. Thus letting your objective be:

objective = -x(finalTime)

Not quite sure I understand (2) :-)

- Joakim

janpeter
Offline
Joined: 2010-09-11
Hi,Thanks for your answer.

Hi,

Thanks for your answer. It works formally. But numerically a disaster though. Perhaps some options must be tuned. Let me explain.

The state variable that I want to maximize increase with time or can come to a halt for some time but never decrease, whatever the control variables. The goal is actually to maximize the increase of X (given some constraint) at each time instant. Due to the fact that X never decrease I thought I could translate the problem to maximise the final value (at a given time). In my first try I only knew how to formulate an integral objective function and I thought if I use say 10-X as my "objectiveIntegrand" it would also work (and X never goes over 10).  And I got the expected solution. Then I thought I clean this up by choosing a more proper objective function like X(finalState) but that give to me a strange solution. 

The formulation of objective function closest to the real problem is alternative 2). Try to explain it better. I would like to maximize a function of the control variables (given some constraints g) at each time instant. This function f is a function of control variables u and v and f(u,v) controls the growth of X. Thus the problem is something like:

optimization Example (objective....,  startTime=0, finalTime=10)                                   

   ...

   input Real u(min=0.0), v(min=0.0);

equation

    f = a*u + b*v;

    g = c*u + d*v;

    der(x) = f*x;

constraint

    g <=g_max;

end Example

 - - - 

The real code is somewhat longer with more state, but I think the above shows the core of the problem. If it is a help I can enclose the code, of course. This problem is by purpose made very small and can be solved by making an analytical solution of what combination of u and v gives maximal f given the contraint g and with this knowledge ad Modelica model could easilty be formed. However, for more realistic problems the analtyical solution of f is hard to calculate and therefore it would be good to use Optimica to express the model in this more free way.

I would actually be glad to get this right, soon.

Jan Peter

 

Update: Now I better understand the difference between using objectiveIntegrand and objective on final state. Since the problem with my parameters the optimal growth stops before final time. This solution I get when using the objectiveIntegrand kind since you get a penalty of reaching the max value later. Both solutions this gives the same final value fo x, but different trajectories. 

To formulate the problem along alternative 2) where we want to maximize f given constraings g is thus my main question now!

borlum
Offline
Joined: 2016-09-22
Derivative of cost

Okay, trying to deduce a bit; by having f as large as possible at each time t, then are you not just looking to maximize the integral of f?

 

janpeter
Offline
Joined: 2010-09-11
I just tried now to use

I just tried now to use objectiveIntegrand = f_max - f, but that give similar solution as objective = -x(finalTime).

That means no pressure to reach maximal x fast, but you get it in the end. 

If I try objective = f_max-f,  that just does not work "java error occurred, details were printed"

Do not understand that either.

fredrik
Offline
Joined: 2011-08-26
What is the problem with

What is the problem with objectiveIntegrand = f_max - f? It sounds like exactly what you want. Also, you can remove f_max, it won't make a difference.

For objective, you should probably do objective = f_max - f(finalTime).

janpeter
Offline
Joined: 2010-09-11
      The way the problem is

 

 

 

The way the problem is formulated originally is that f should be maximized (at all time instances or how I shouild put it).

But then I get Runtime error "java error occurred, datails were printed"...  - perhaps we should look more here?

To circumvent that I tried "objective -x" but gives the same error.

If take "objectiveIntegrand - x" then it works.

---

The problem for me is perhaps on a conceptual level. 

When you describe dynamical systems it is sometimes convenient to express the model 

in terms of some "energy" or similar to be minimized given some constraints, and not to take the burden to write down 

the ODE to be solved. In my case it is about maximize "f" given some constraints.

I thought JModelica could handle this kind of description. 

--

Thus, I wouild like to use optimization for a sub-system to simplify description of dynamics.

When this subsystem is part of a larger system, I may want to formulate an optimization problem for the total problem.

Something like that.

Jan Peter

PS. Does it help if I post some code?  - the examples is not much bigger than described above.

fredrik
Offline
Joined: 2011-08-26
Let's focus on the actual

Let's focus on the actual case you want to solve. My understanding is that this is objectiveIntegrand = -f. What is the complete error that you get using this?

Code for reproduction might help, but for starters I would just like to see the full error message.

janpeter
Offline
Joined: 2010-09-11
 Ok. Using objectiveIntegrand

Ok. Using objectiveIntegrand = -f, gives a reasonable solution, but to be interesting there need to be a penalty also in the time it takes to reach the maximal point. Therefore objectiveIntegrand = -x works.

But the natural formulation of the problem is to use objective = -f, this gives a runtime error however with the key text: The class attribute 'objective' must have parameter or time variability.

In my eyes is f a function f(u,v) where u and v are time variable and shall be optimized, i.e. f varies with time. The problem perhaps is because there is no dynamics between u and v to f, as it is from u and v to x, which do work.

Enclosed a screen-dump of the error text.

Thanks for your help.

AttachmentSize
Screen Shot 2017-03-21 at 09.44.12.png 107,2 KB
fredrik
Offline
Joined: 2011-08-26
As I said before, to use

As I said before, to use objective, you should do objective=-f(finalTime). But I don't think this is what you want. It sounds like you just want objectiveIntegrand=-f.

But if you really want to also penalize the time it takes to reach the maximum, and if the maximal point f_max is known, you do

objectiveIntegrand=-w*f, objective=finalTime, finalTime(free=True)

constraint
f(finalTime) = f_max,

where w is a weight parameter of your choosing. If f_max is not known, then I'm not sure what the best way to solve it is... It sounds like a bilevel optimization problem, which can't be naturally formulated with Optimica, but you could still solve it with some scripting (and a lot of computational time).

If you only want to reach f_max as fast as possible, remove objectiveIntegrand from the above.

janpeter
Offline
Joined: 2010-09-11
Hi   In my previous post I

Hi

 

In my previous post I said that the natural formulation of the problem is with objective = -f. (i.e. not objectiveIntegrand = -f)

 

I go an error message that I do not understand. Do you understand it?

 

 

The documentation of Optimica in the JModelica manual is sparse.

Is there some other document available?

 

 

What I try to do with this example is to formulate a simple example where I can do one model formulation where I can use an explicit solution of the optimisation of “f” so to say, since it in this case easy. And then I try to formulate a model in a different way where I ask optimica to do the optimisation of f at each time instant instead. 

 

Simulation results of the two model should be the same.

 

The wider perspective is that here is a wide class of problems formulated where f is not so easy or possible to make an analytical solution for, and here you badly need optimisation at each time instant. 

 

The point with optimica is to formulate the problem very close to how the user think of the problem, as I understand it. Therefore I am not satisfied with using “tricks” like instead of objective =-f, do have objectiveIntegrand = -x, although I understand that it will give the right solution to. 

 

There is a Python package called PyFBA that do this kind of job for large problems

http://journal.frontiersin.org/article/10.3389/fmicb.2016.00907/full

I have not used PyFBA and know very little about it but I think I understand the most basic underlying idea.

 

My idea was to try use the same idea for smaller problems in JModelica with Optimica.

 

 

fredrik
Offline
Joined: 2011-08-26
The error message is telling

The error message is telling you that the objective can't have continuous variability, which is Modelica speak for that it can't be a function (of time), because that wouldn't make sense. I guess that you want to maximize some norm/functional of f, but it's not clear what norm you want. Do you have some equations for the natural formulation of your problem?

The longest description of Optimica can be found in https://lup.lub.lu.se/search/publication/599261

Login or register to post comments