Wolfram Optimization | Things to Try
Wolfram Optimization | Things to Try
Make edits and run any piece of code by clicking inside the code and pressing .
+
Model, Solve & Analyze Designs. Wolfram Optimization provides a comprehensive set of tools to find the best design or make the best decision given constraints, fully integrated with highly automated machine learning, statistics, immediately computable built-in data and more.
Find Local or Global Extrema
Find Local or Global Extrema
Plot a function that has both global and local extrema: |
In[]:=
Plot[+-5,{x,-3,3}]
4
x
3
x
2
x
Search for a local minimum near x1 |
In[]:=
FindMinimum[+-5,{x,1}]
4
x
3
x
2
x
Now search for a local minimum near x-1.8 |
In[]:=
FindMinimum[+-5,{x,-1.8}]
4
x
3
x
2
x
In[]:=
Minimize[+-5,x]
4
x
3
x
2
x
Constraints can also be included to find the minimum value in a restricted domain: |
In[]:=
Minimize[{+-5,0<x<2},x]
4
x
3
x
2
x
In[]:=
NMinimize[{+-5,0<x<2},x]
4
x
3
x
2
x
Without a restricted domain, it aims to find global minima and return numeric results: |
In[]:=
NMinimize[+-5,x]
4
x
3
x
2
x
Visualize the global and local minima that were found: |
In[]:=
Plot+-5,{x,-3,3},Epilog->
4
x
3
x
2
x
Find Extrema in Higher Dimensions
Find Extrema in Higher Dimensions
The same functionality can be used in higher dimensions. Define an objective function and constraints that can be visualized in three dimensions: |
In[]:=
objective=3Sin[+]-x-2y;constraints=Norm[{x,y}]>=1&&+<=10;
2
x
2
(y-1)
4
x
4
y
Visualize the objective function over the constrained region: |
regionandobjective=ShowPlot3Dobjective,{x,-2,2},{y,-2,2},AxesLabel{x,y,z},,DiscretizeRegionImplicitRegion[constraints&&z-9,{{x,-2,2},{y,-2,2},z}],
Find the local minimum of the objective function near (0,1) |
In[]:=
{localmin3D,localminimizer3D}=FindMinimum[objective,{{x,0},{y,1}}]
Find the global minimum of the objective function in the constrained region: |
In[]:=
{globalmin3D,globalminimizer3D}=NMinimize[{objective,constraints},{x,y}]
Visualize the global minimum (shown in red) and the local minimum (shown in blue) that were found: |
In[]:=
Show[regionandobjective,Graphics3D[{PointSize[Large],Blue,Point[{x,y,localmin3D}]/.localminimizer3D,Red,Point[{x,y,globalmin3D}]/.globalminimizer3D}]]
Minimize an Objective Function Subject to Constraints
Minimize an Objective Function Subject to Constraints
Define regions in terms of constraints, intersections and built-in shapes: |
In[]:=
ℛ
1
,ImplicitRegion[2x-y>=3,{x,y}];
ℛ
2
,ImplicitRegion[2x-y<3,{x,y}];
Visualize an objective function over the previously defined regions: |
In[]:=
ShowPlot3D+,{x,y}∈,,Plot3D+,{x,y}∈,
3
(x+1)
2
(y-1)
ℛ
1
3
(x+1)
2
(y-1)
ℛ
2
Use |
In[]:=
NMinimize[+,{x,y}∈]
3
(x+1)
2
(y-1)
ℛ
1
Use |
In[]:=
{minvalue,minimizer}=Minimize[+,{x,y}∈]
3
(x+1)
2
(y-1)
ℛ
1
Show the constrained minimum found along with the objective function: |
In[]:=
ShowPlot3D+,{x,y}∈,,Plot3D+,{x,y}∈,,Graphics3D[{Red,PointSize[Large],Point[{x,y,minvalue}/.minimizer]}]
3
(x+1)
2
(y-1)
ℛ
1
3
(x+1)
2
(y-1)
ℛ
2
Solve Convex Optimization Problems
Solve Convex Optimization Problems
Define two implicit regions through constraints: |
In[]:=
1
2
x
2
y
2
2
(x-2)
2
(y-1)
Visualize the regions: |
In[]:=
RegionPlot[{,},{x,-1,3},{y,-1,3},PlotTheme->"Minimal",Axes->False]
1
2
Find the pair of points (one from each region) that minimizes the distance between them: |
In[]:=
closest2D=ConvexOptimization[Norm[p1-p2],{},{p1∈ImplicitRegion[,{x,y}],p2∈ImplicitRegion[,{x,y}]}]
1
2
Visualize the pair of points found and the distance between them: |
In[]:=
RegionPlot{,},{x,-1,3},{y,-1,3},PlotTheme->"Minimal",Axes->False,Epilog->
1
2
Quickly define 3D regions using built-in shapes: |
In[]:=
1
2
Visualize the shapes: |
In[]:=
Graphics3D[{,}]
1
2
Find the pair of points (one from each region) that minimizes the distance between the two: |
In[]:=
closest3D=ConvexOptimization[Norm[p1-p2],{},{p1∈,p2∈}]
1
2
Visualize the points found with a line connecting them: |
In[]:=
Graphics3D[{,,{Red,Thick,Line[{p1,p2}/.closest3D]}}]
1
2
Solve and visualize a dynamic version of the previous example: |
In[]:=
Manipulate[shape1=Dodecahedron[{0,0,0},{theta,phi}];shape2=Ellipsoid[{4,0,0},DiagonalMatrix[{2,3,1}]];dynamic3D=ConvexOptimization[Norm[p1-p2],{},{p1∈shape1,p2∈shape2}];Graphics3D[{shape1,shape2,{Red,Thick,Line[{p1,p2}/.dynamic3D]}},PlotRange->{{-1.5,5.5},{-2,2},{-1.4,1.4}}],{{theta,0,"Rotation about Z"},0,2Pi},{{phi,0,"Rotation about Y"},0,2Pi},ControlPlacement->Top,SaveDefinitions->True]
Use Optimal Fitting to Smooth Data
Use Optimal Fitting to Smooth Data
Find the Shortest Tour
Find the Shortest Tour
Obtain Exact Solutions to Symbolic Optimization Problems
Obtain Exact Solutions to Symbolic Optimization Problems
Obtain Parametric Solutions to Symbolic Optimization Problems
Obtain Parametric Solutions to Symbolic Optimization Problems