Nelder-Mead simplex optimization using only worksheet functions -> done!

charlielaub

New Member
Joined
Jun 5, 2014
Messages
9
I have experimented with looping/iterating structures in Excel in the past and have created a couple of nifty applications for my hobbies that come close to what you get with programming (e.g. VBA) but using only worksheet functions. I decided to see if I could do something a little more challenging, so I've managed to come up with a way using only worksheet functions to implement the Nelder-Mead simplex (non-linear) optimization method. This took a lot of head scratching and analysis of the algorithm, but I did figure out how to make it work.

Some key elements include a variable that tracks the current "line" of the program (borrowing that terminology from high level programming), and a decision table that is consulted at each step to see what to do next - this can include terminating the iterations, branching to a new "line", or stepping on to the next line. The table also aids with "if..then" types of decisions that are often employed in programming.

I have included a link to the spreadsheet below (works in Excel 2003 or later) if you would like to check it out. I would love to know if anyone else out there is interested in this kind of thing! How far could one push this approach? I just made up this implementation out of the blue, but I would guess that one could come up with a structured way to implement all sorts of high level programming in this way.

The Excel file at the link below is set up to solve DeJong's function in 5 variables and the solution vector to this problem is: 1, -2, 3, 4, 5. I have nicknamed the method "Loopy" as a play on "to iterate" and the fact that I must have been loopy to do this! To run the optimization/minimization, go to the LoopyControl worksheet. Enter "0" in cell A3. Next press F9 to "reset" the program. Next enter "1" in cell A3. Again press F9 to execute the program. The spreadsheet it set to iterate 10,000 times and this should be sufficient to find the solution. The iteration will stop when the error falls below the "STOP ERROR" (cell A15 in the LooptControl sheet). Worksheet functions are not fast, so please be patient unless you have a new machine. There may be an iteration counter in the bottom bar of Excel that counts up as the algorithm marches along. After the minimization has completed there is a plot of the progress of the minimization that can be found in the ResultPlot sheet. The sheet "InputOutput" contains cells for a guess at the solution (you can enter most anything) and the "best fit" result will appear there when the program execution completes. There is no protection or hidden cells - everything is shown. This means that you can easily break the spreadsheet, so only change the cells that are in bold, blue type unless you really know what you are doing.

Loopy can easily be modified to solve other 5-dimension (only) non-linear problems that can be described with a single valued "error" like a sum-of-squares-error or similar. In the Calculations worksheet, there is a blue box containing a few lines that form part of the objective function. The values of the 5 variables are taken from cells A15 through A19. The program tries to optimize the value in cell A47. To use a new function, use the area in the blue box (insert new rows as necessary) to calculate it and code the resulting error into cell A47. For instance I use another incarnation of this spreadsheet to perform a 5-variable nonlinear data fitting problem, on 100 data points.

I'm happy to help people out if you encounter questions while checking out Loopy. FYI: this seems to only work in Excel (not OO or LO Calc or other Excel clones where Err:523 pops up).

-Charlie

Download Loopy:
http://claub.net/Loopy/Loopy_v2.0.xls
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Actually, since this is a help forum, if anyone can help me get this working in OpenOffice Calc or Libre Office Calc I would really appreciate it!

I have just tried to use their non-linear "solvers" and frankly they are really, really awful. I would prefer to use my worksheet function coded solver.

As I mentioned, I am getting these ERR:523 errors in lots of cells (although there are many dependencies). If you are skilled in ferreting out these problems, I would be very grateful if you could work with me to clear the errors.

Thanks,

-Charlie
 
Upvote 0
Thanks, Charlie. The field is not one I'm familiar with, unfortunately. I tend more to simple than simplex. :)

Your post reminded me of something I saw a little while ago, I wonder if it might be of interest to you?
Data Tables & Monte Carlo Simulations in Excel – A Comprehensive Guide | Chandoo.org - Learn Microsoft Excel Online

regards, Fazza

Thanks for your reply.

The info at the page to which you linked was interesting. I've certainly done Monte Carlo analysis in Excel before: if you can compute your value(s) once in spreadsheet cells you can just copy those cells 1000 times (down) to amass sufficient data for a crude MC analysis. This is great for showing how a process responds to some randomness of its input variables, for instance. I use it for circuit design and analysis, where the components in the circuit have some tolerance within which the component value (resistance, capacitance, etc.) can vary. By performing MC analysis, the distribution in the output of the circuit can easily be described. But this does not necessarily require iteration.

The Mandelbrot calculation that is shown in teh linked web page is a very simple one. It does use iteration, but lacks other aspects of high level programming. I've successfully used Excel and Calc to step through the equivalent of a for (i=0;i<iend;i++) {} C++ loop, where the main calculation done at each step was a complex integral. This is relatively easy compared to the logic flow used in the simplex spreadsheet.
 
Upvote 0
Charlie,

LibreOffice calc does not warn the user if it needs iterative calculations, like Excel does. LibreOffice simply makes the self-dependent cells errors and thats it.

Try switching iteration on in the options as see if it works then.

In any case, you may use the following formula:
Code:
=IF(NOT(ISERROR(ERRORTYPE(1/0))),"LibreOffice",IF(NOT(ISERROR(ERROR.TYPE(1/0))),"Excel","neither Excel nor LibreOffice"))

This formula displays "LibreOffice" in LibreOffice, and displays "Excel" if used in Excel. You can modify it to write a single formula which works in two different ways, depending on the spreadsehet in which you open the file.

Best,

J.Ty.
 
Upvote 0
Charlie,

LibreOffice calc does not warn the user if it needs iterative calculations, like Excel does. LibreOffice simply makes the self-dependent cells errors and thats it.

Try switching iteration on in the options as see if it works then.

In any case, you may use the following formula:
Code:
=IF(NOT(ISERROR(ERRORTYPE(1/0))),"LibreOffice",IF(NOT(ISERROR(ERROR.TYPE(1/0))),"Excel","neither Excel nor LibreOffice"))

This formula displays "LibreOffice" in LibreOffice, and displays "Excel" if used in Excel. You can modify it to write a single formula which works in two different ways, depending on the spreadsehet in which you open the file.

Best,

J.Ty.
Thanks for this tip about ERRORTYPE and ERROR.TYPE. I will investigate this possibility for targeted formulas.

Sorry if I was not clear - I explicitly choose "Iteration" in Calc and Excel when running this spreadsheet. That state of calculation is saved in the workbook, and it should open that way.
 
Upvote 0
I tried the formula that you offered using ERRORTYPE and ERROR.TYPE and discovered some problems occur when the file is saved. Here is what I did:
I first opened LO Calc. I entered the formulas by pasting it into the formula bar and when executed it works as you describe: the cell displays "LibreOffice". I then saved the file as in Excel 2003 format (xls).
Next I opened the file in Excel (2013). The formula was replaced with "=N/A". I re-pasted the formula into the formula bar and when executed it worked as you describe, returning "Excel". I then saved the file with the xls extension.
I re-opened the file in Calc. The cell containing the formula displayed the value "#NAME" and the formula was changed to:
Code:
=IF(NOT(ISERROR((#NAME?,1/0))),"LibreOffice",IF(NOT(ISERROR(ERRORTYPE(1/0))),"Excel","neither  Excel nor LibreOffice"))

Can you give me any tips on why this is happening and how to prevent Excel and Calc from editing formulas when files are opened???

Thanks,

-Charlie
 
Upvote 0
I saved a small spreadsheet with extension .xlsx and it works without problems.

J.Ty.
 
Last edited:
Upvote 0
I saved a small spreadsheet with extension .xlsx and it works without problems.

J.Ty.

Doesn't work for me. I just installed both Excel and Calc so I have the latest Windows versions.

When I enter the formula in a cell on either spreadsheet it works. As soon as I save and re-open the spreadsheet in the other program it no longer works.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top