WOLFRAM LANGUAGE TUTORIAL

Numerical Nonlinear Global Optimization Examples

Finding Multiple Optima, Method 1

Here is one way to get multiple minima: call NMinimize multiple times with different random seeds, which will cause different optimization paths to be taken.

This defines a function with a whole ring of minima.
In[1]:=
Click for copyable input
Out[2]=
This makes a table of solutions by using different random seeds.
In[3]:=
Click for copyable input
Out[3]=
This turns the solutions into points and plots them on the contour plot of the function.
In[4]:=
Click for copyable input
Out[7]=

Finding Multiple Optima, Method 2

Here is another way to get multiple minima: write the objective function in such a way as to make a list of every point that is visited, then select the points that have objective function values close to the final solution.

Define a function with a whole ring of minima.
In[18]:=
Click for copyable input
Out[20]=
Reap returns the solution from NMinimize and the points sown by EvaluationMonitor.
In[21]:=
Click for copyable input
Out[22]=
Find all the visited points that are close in objective function value to the final solution. Then color them white, color the final solution black, and show them on the contour plot of the function.
In[23]:=
Click for copyable input
Out[27]=

Finding a Nonlinear Fit of Data

This defines a model based on five random parameters.
In[28]:=
Click for copyable input
Out[30]=
Create a function from the model and parameters, and generate sample points over the interval .
In[31]:=
Click for copyable input
This plots the points and the solution from FindFit. The solution gets trapped by a local minimum due to the trigonometric functions.
In[35]:=
Click for copyable input
Out[37]=
This generates a sum of squares from the data, and uses NMinimize to find the minimum.
In[38]:=
Click for copyable input
Out[39]=
This plots the points and the solution from NMinimize.
In[40]:=
Click for copyable input
Out[41]=

Solve Example

Solve cannot work with this system of equations because they are highly nonalgebraic.
In[44]:=
Click for copyable input
Out[44]=
Give NMinimize a constant objective function, and the equations to be solved as constraints. It finds the solution.
In[43]:=
Click for copyable input
Out[43]=

Queens on a Chessboard

In[46]:=
Click for copyable input
is True if and only if is attacking .
In[47]:=
Click for copyable input
countAttacks[vec] converts the vector of real numbers into a permutation of the queens and counts the number of attacks.
In[48]:=
Click for copyable input
Given a permutation, this shows the arrangement.
In[49]:=
Click for copyable input
Use DifferentialEvolution to fit all the queens on the chessboard so that no queen is attacking another queen. Postprocessing is turned off because it is unlikely to help, given the discrete nature of the problem.
In[50]:=
Click for copyable input
Out[52]=
This shows the solution.
In[53]:=
Click for copyable input
Out[53]=