cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Optimization problem using minimize function

Murarz
13-Aquamarine

Optimization problem using minimize function

Hello everyone!

This is my first post, I'm glad to be here. Let's cut to the chase. 

 

I have created  an objective function  which has 5 variables. I gave 5 constraints. Probably a typical problem. But when i try to optimize it with the minimize function mathcad spent all night calculating it and it did not come up with anything. I am new to this, so probably missing the point 🙂 I will be thankful for any piece of advice.opt.png

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:Murarz)

OK, as promised here is my speeded up version.

Hope the my first interpretation of the function Y is the one you need - otherwise we would have to invest some more work to make the solve block respect ALL constraints (if possible at all).

I also include a pdf-print of the sheet.

As you see your original Y-function evaluates in about 12 seconds. The 51 sec of what I wrote earlier came about the fact that my machine was busy with some video recoding which eat up nearly all my CPU power 😉

So your reduction from 583 points to somewhat over 400 seems not to be necessary at all.

Note that because of the various use of the time() function the sheet will recalculate every time before printing or saving which is annoying. You might want to get rid of tall the unnecessary regions as soon as possible.

 

View solution in original post

22 REPLIES 22

Attach please Mathcad-sheet!

Ans second!

Call please your objective function OF after the giving guess values of aa, ab etc!

Use please the Minimize function without constrains and than in put constrains step-by-step!

Good luck!

Murarz
13-Aquamarine
(To:ValeryOchkov)

Thanks for the reply! I have enclosed the mathcad sheet with excel files, since it reads directly from them. I switched OF function with exact values as you said, but still it doesn't wish to calculate 😄

Werner_E
24-Ruby V
(To:Murarz)

Have you ever tried to call your function OF just ONE TIME?

I did, using your initial values, and after e few minutes it was still calculating and I cancelled the operation.

When trying to minimize, Mathcads algorithm would change your five arguments very often ever so slightly and will then call OF every time. If even one single call of OF takes that long, you can't expect the solve block to finish within a reasonable time.

The first thing to do is to look closer at the objective function if you can find a way to avoid that many nested loops which cost that many time in calculation.

Debugging of your sheet (if someone would be willing to take the time) is made more difficult and hindered by the fact, that the definition of some vital variables (Vol11, T0,..) is not visible as its hidden in a locked area.

 

Werner_E
24-Ruby V
(To:Murarz)

Just one additional observation:

You used the same variable name i for a loop and a sum within this loop.

I guess thats not an error and that Mathcad can handle this correct but its sure bad style and irritating. You may want to change the name of one of those variables as seen in the attached picture.

 

Werner_E
24-Ruby V
(To:Murarz)

Here is a first suggestion for an optimization of your sheet.

The way you had defined OF, the function Y is called for every summand of your sum, meaning it is called 583 times!

One single call to Y takes 51 seconds on my machine, so one single call to OF would take approx. 8 and a half hour!!

With the modification you see in the attached picture Y is called only once a one single call to OF takes a bit more than 51 seconds now.

Not sure if this is enough to make the solve block finish in reasonable time - I did not try as I suppose that it still will take quite some time as OF would be called many many times when trying to minimize it. At least it will now be about 580 times faster 😉

Murarz
13-Aquamarine
(To:Werner_E)

Thank you very much ! I reduced the number from 583 to 470. I cannot go lower unfortunately. Now it should theoretically calculate the case in 2 hours, since OF for one data gives me the result in 15 seconds.

Werner_E
24-Ruby V
(To:Murarz)

Why do you think it should solve in 2 hours?

We do not know how many changes the algorithm used for "minimize" will make and so we don't know how often OF will be called by the algorithm.

 

I guess you simply divided the 8 hours I mentioned by 4.

But those 8 hours simply were a guess how long it would take for ONE call to OF with the way you defined OF originally.

 

We can hardly guess how long the solve block would need to finish. The number of iterations done there depends on the algorithm used and is depended on how fast the convergence will be.

 

Murarz
13-Aquamarine
(To:Werner_E)

I am sorry I blocked the  area in mathcad. I just had so many things there I thought It would be unreadable. I enclose it once again. But still from your experience having such a function with let's say 5 variables would it be possible to obtain the solution within 10 minutes for instance? 

Werner_E
24-Ruby V
(To:Murarz)


@Murarz wrote:

I am sorry I blocked the  area in mathcad. I just had so many things there I thought It would be unreadable. I enclose it once again. But still from your experience having such a function with let's say 5 variables would it be possible to obtain the solution within 10 minutes for instance? 


Yes it is possible.

I was just working on it and depending on the interpretation of your function Y it takes 1,5 to 3 minutes 😉

I was able to speed up your function Y significantly and in combination of the aforementioned speeded up function OF this means that OF will now work with a 50000 times faster speed.

 

I'll upload the modified worksheet in a few minutes - just have to clean up one thing or another.

 

I'll upload the modified file in a few minutes.

Werner_E
24-Ruby V
(To:Murarz)

OK, as promised here is my speeded up version.

Hope the my first interpretation of the function Y is the one you need - otherwise we would have to invest some more work to make the solve block respect ALL constraints (if possible at all).

I also include a pdf-print of the sheet.

As you see your original Y-function evaluates in about 12 seconds. The 51 sec of what I wrote earlier came about the fact that my machine was busy with some video recoding which eat up nearly all my CPU power 😉

So your reduction from 583 points to somewhat over 400 seems not to be necessary at all.

Note that because of the various use of the time() function the sheet will recalculate every time before printing or saving which is annoying. You might want to get rid of tall the unnecessary regions as soon as possible.

 

Murarz
13-Aquamarine
(To:Werner_E)

I don't know what to say, thank you very much 🙂

This the relation I had in mind

Werner_E
24-Ruby V
(To:Murarz)

This does not help as its bad math. "t" is used in three different meanings (two different variables of integration in nested integrals and then as limit of integration).

Its not clear which of the two expressions shown in the attached picture you mean. I guess the first one, but I can't be sure.

 

One additional thought:

Instead of using Minimize you can also use MinErr. Especially with vectors as in your case this may yield better results as it means that Mathcad is doing its own minimizing of the sum of squared errors and has access to the raw data (the vector) when doing so.

I gave it a try and calculation time was much longer (about 20 minutes) and the result did not respect two of the constraints as you can see in the attached picture.

But the fit looked much better to me (see the two attached pics, one with the values minimize gave and the other with the values of minerr.

One could try to weight those missed constraints a bit higher so the solution would respect them.

This means that instead of 1<a<b you can write 1*10^2<a*10^2<b*10^2. That way the absolute error in this constraint is 100 times higher and Mathcad will try to cope with that. But this possibly at the cost of another constraint.

 

 

Murarz
13-Aquamarine
(To:Werner_E)

These constraints are ok, but the a1 was wrong. It should be 0.39 instead of 0.467 . My bad. THank you very much again, you helped me a lot.

 

I will try with minerr as well, now i will be going deeper and deeper with mathcad, I just started 😄 You showed me many useful functions, 🙂

Murarz
13-Aquamarine
(To:Murarz)

As to the function because you made the calculations for both alternative equations , this is the integral relation i applied. ( the second version found in some papers) Y1 is here a1Bez tytułu.png

 

Murarz
13-Aquamarine
(To:Murarz)

( the second version found in some papers) Y1 is here a1.. so it seems the first the first relation indeed Bez tytułu.png

Werner_E
24-Ruby V
(To:Murarz)


@Murarz wrote:

( the second version found in some papers) Y1 is here a1.. so it seems the first the first relation indeed Bez tytułu.png


Yes, fortunately this corresponds to Y1.speed.

Murarz
13-Aquamarine
(To:Werner_E)

I am just curious now, is this solution a global or rather a local one? In many papers genetic algorithms were applied. Would a Minimize function/Minerr  suffice.?

Werner_E
24-Ruby V
(To:Murarz)

The numeric algorithms will only give you a local minimum depending on the initial guess values.

Mathcad uses Levenberg-Marquardt (not for minimize) or conjugate gradients or Quasi-Newton for nonlinear problems.

You can chose which algorithm Mathcad should use when you right-click on "Minimize" or "MinErr" and you also can change some settings there. Usually the default is OK.

Other algorithms like genetic ones or simulated annealing are not built-in.
In this forum ever so often users publish such algorithms, usually for specific problems, but I guess its hard to find the appropriate threads here and if, you would have quite some work adopting the routines for your needs.

 

I happened to remember that Richard once posted a worksheet dealing with finding minimas of a function using simulated annealing.  Maybe you can adopt it for your problem. Here is the link:

https://community.ptc.com/t5/PTC-Mathcad/Minimizing-a-Function-using-Simulated-Annealing/td-p/445213

 

Murarz
13-Aquamarine
(To:Werner_E)

Thanks 🙂

Werner_E
24-Ruby V
(To:Murarz)

Just for completeness sake here is the worksheet where you can compare the solutions from minimize with that from minerr.

I have weighted two constraints lightly and I got the impression that the solve block with MinErr even takes longer than before.

I hard coded the outcome of both so you don't have to let the solve blocks run and can compare the plots nonetheless.

Have fun!

Murarz
13-Aquamarine
(To:Werner_E)

Thank you :)))))

Top Tags